-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: add confirm action component via prop (#3231)
This PR adds a `ConfirmAction` component, rendered alongside the child steps of `Wizard` when `confirmActionSettings` is set. The component intercepts the `onSubmit` action and displays a modal which can be customised in its content an behaviour such as disabling the submit button, passing a component as its description, and customisable `onCancel` and `onConfirm` callbacks. <img width="495" alt="image" src="https://github.com/user-attachments/assets/077e0f63-c849-4827-929b-c51062f21957" /> ### Implementation sample ``` const Sample = () => { return ( <Wizard {...}> <FirstStep name="First" confirmActionSettings={{ title: "Confirm Action", description: "Are you sure you want to continue?", }} /> <SecondStep name="Second" /> </Wizard> ); }; ``` ### Wizard Component Enhancements: * Added a new `ConfirmAction` component to handle confirmation dialogs within the wizard. This component includes properties for title, description, action labels, and handlers for confirm and cancel actions. * Updated the `WizardContext` to include state and setter for `confirmActionOpen` to manage the visibility of the confirmation dialog. * Modified the `Wizard` component to integrate the `ConfirmAction` component and manage its state. This includes adding `confirmActionSettings` to wizard child props and updating the `onSubmit` handler to trigger the confirmation dialog if required. [[1]](diffhunk://#diff-fb5a2ff0894b9e7ddb45ec4d09e32f61843c333aff5fbc7ebf30371eaeefa373R52) [[2]](diffhunk://#diff-fb5a2ff0894b9e7ddb45ec4d09e32f61843c333aff5fbc7ebf30371eaeefa373R129) [[3]](diffhunk://#diff-fb5a2ff0894b9e7ddb45ec4d09e32f61843c333aff5fbc7ebf30371eaeefa373R169-R175) [[4]](diffhunk://#diff-fb5a2ff0894b9e7ddb45ec4d09e32f61843c333aff5fbc7ebf30371eaeefa373R197-R200) [[5]](diffhunk://#diff-fb5a2ff0894b9e7ddb45ec4d09e32f61843c333aff5fbc7ebf30371eaeefa373R230-R232) ### AppLayout Component Updates: * Refactored the `styled` import to use a local styled module instead of `@emotion/styled` in `frontend/packages/core/src/AppLayout/index.tsx`.
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from "react"; | ||
import { | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
Typography, | ||
useWizardContext, | ||
} from "@clutch-sh/core"; | ||
|
||
export interface ConfirmActionProps { | ||
title: string; | ||
description: React.ReactNode | string; | ||
actionLabel?: string; | ||
onConfirm?: () => void; | ||
onCancel?: () => void; | ||
submitDisabled?: boolean; | ||
} | ||
|
||
const ConfirmAction: React.FC<ConfirmActionProps> = ({ | ||
title, | ||
description, | ||
actionLabel, | ||
onConfirm, | ||
onCancel, | ||
submitDisabled = false, | ||
}) => { | ||
const { confirmActionOpen: open, setConfirmActionOpen: setOpen, onSubmit } = useWizardContext(); | ||
|
||
const handleConfirm = () => { | ||
onSubmit(); | ||
setOpen(false); | ||
if (onConfirm) { | ||
onConfirm(); | ||
} | ||
}; | ||
|
||
const handleCancel = () => { | ||
setOpen(false); | ||
if (onCancel) { | ||
onCancel(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Dialog title={title} open={open} onClose={handleCancel}> | ||
<DialogContent> | ||
{typeof description === "string" ? ( | ||
<Typography variant="body1">{description}</Typography> | ||
) : ( | ||
description | ||
)} | ||
</DialogContent> | ||
<DialogActions> | ||
<Button text="Cancel" onClick={handleCancel} variant="neutral" /> | ||
<Button | ||
text={actionLabel || "Confirm"} | ||
onClick={handleConfirm} | ||
variant="danger" | ||
disabled={submitDisabled} | ||
/> | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default ConfirmAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters