Skip to content

Commit

Permalink
fixing api
Browse files Browse the repository at this point in the history
  • Loading branch information
fneves committed Nov 13, 2023
1 parent a762faf commit 71c9b4b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ const App = () => {
<ConfirmationDialog
title="Confirmation Dialog Example"
message="This is a simple dialog that will be reused across the application"
onPrimaryActionClick={() => {
onConfirm={() => {
console.log("close");
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ const ConfirmationDialogComponent = ({
title,
message,
primaryActionLabel,
onPrimaryActionClick,
onConfirm,
secondaryActionLabel,
onOpenChange,
onCancel,
}: ConfirmationDialogProps) => (
<GridCenter>
<ConfirmationDialog
open={open}
title={title}
message={message}
onOpenChange={onOpenChange}
onCancel={onCancel}
primaryActionLabel={primaryActionLabel}
onPrimaryActionClick={onPrimaryActionClick}
onConfirm={onConfirm}
secondaryActionLabel={secondaryActionLabel}
>
</ConfirmationDialog>
Expand Down
16 changes: 8 additions & 8 deletions src/components/ConfirmationDialog/ConfirmationDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe("Dialog Component", () => {
{
title = "Dialog Title",
message = "Are you sure you want to proceed?",
onPrimaryActionClick = () => {
onConfirm = () => {
void undefined;
},
onOpenChange,
onCancel,
open,
primaryActionLabel = "Confirm",
secondaryActionLabel = "Cancel",
Expand All @@ -24,10 +24,10 @@ describe("Dialog Component", () => {
<ConfirmationDialog
title={title}
message={message}
onPrimaryActionClick={onPrimaryActionClick}
onConfirm={onConfirm}
primaryActionLabel={primaryActionLabel}
secondaryActionLabel={secondaryActionLabel}
onOpenChange={onOpenChange}
onCancel={onCancel}
open={open}
children={children}
/>
Expand Down Expand Up @@ -70,7 +70,7 @@ describe("Dialog Component", () => {
title,
primaryActionLabel,
open,
onOpenChange: (b: boolean) => open = b
onCancel: () => open = false
});

expect(queryAllByText(title).length).toEqual(1);
Expand All @@ -83,12 +83,12 @@ describe("Dialog Component", () => {
let counter = 0;
const title = "Dialog Title";
const primaryActionLabel = "PrimaryAction";
const onPrimaryActionClick = () => counter++;
const onConfirm = () => counter++;

const { getByText } = renderDialog({
title,
primaryActionLabel,
onPrimaryActionClick,
onConfirm,
open: true,
});

Expand All @@ -113,7 +113,7 @@ describe("Dialog Component", () => {
title,
secondaryActionLabel,
open,
onOpenChange: (b: boolean) => open = b
onCancel: () => open = false
});

expect(queryAllByText(title).length).toEqual(1);
Expand Down
19 changes: 9 additions & 10 deletions src/components/ConfirmationDialog/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ type DialogPrimaryAction = "primary" | "danger";

export interface ConfirmationDialogProps {
open?: boolean;
onOpenChange?: (b: boolean) => void;
onCancel?: () => void;
title: string;
primaryActionType?: DialogPrimaryAction;
primaryActionLabel?: string;
secondaryActionLabel?: string;
message?: string;
children?: ReactNode;
loading?: boolean;
onPrimaryActionClick?: (() => void) | (() => Promise<void>);
onConfirm?: (() => void) | (() => Promise<void>);
}

const ActionsWrapper = styled.div`
Expand All @@ -25,14 +25,14 @@ const ActionsWrapper = styled.div`

export const ConfirmationDialog = ({
open,
onOpenChange,
onCancel,
title,
message,
loading,
primaryActionType = "primary",
primaryActionLabel = "Confirm",
secondaryActionLabel = "Cancel",
onPrimaryActionClick,
onConfirm,
children,
}: ConfirmationDialogProps): ReactElement => {
if (children && message) {
Expand All @@ -44,7 +44,9 @@ export const ConfirmationDialog = ({
return (
<Dialog
open={open}
onOpenChange={onOpenChange}
onOpenChange={(open: boolean) => {
!open && onCancel && onCancel();
}}
>
<Dialog.Content title={title}>
{children ? children : <Text>{message}</Text>}
Expand All @@ -57,11 +59,8 @@ export const ConfirmationDialog = ({
type={primaryActionType}
label={primaryActionLabel}
onClick={() => {
if (onPrimaryActionClick) {
onPrimaryActionClick();
}
if (onOpenChange) {
onOpenChange(false);
if (onConfirm) {
onConfirm();
}
}}
/>
Expand Down

0 comments on commit 71c9b4b

Please sign in to comment.