Skip to content

Commit

Permalink
fix(dialog): close confirmation modal on confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamTraoreee committed Sep 9, 2022
1 parent ea91ec5 commit 1051a26
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
13 changes: 11 additions & 2 deletions libs/shared/ui/src/lib/components/confirmation/confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,28 @@ export function Confirmation(props: ConfirmationProps) {
onConfirm,
} = props;
const [confirm, setConfirm] = useState('');
const [isOpen, setIsOpen] = useState(false);

const handleInputChange = (e: React.ChangeEvent) => {
setConfirm((e.target as HTMLInputElement).value);
};

const handleConfirm = () => {
if (confirm === word) {
onConfirm?.();
onConfirm && onConfirm();
setIsOpen(false);
}
};

return (
<Dialog trigger={trigger} title={title}>
<Dialog
trigger={trigger}
title={title}
open={isOpen}
onOpenChange={(open) => {
setIsOpen(open);
}}
>
<p>{text}</p>
<p className="mb-3">
{confirmText}: <b className="font-bold text-primary-400">{word}</b>
Expand Down
10 changes: 9 additions & 1 deletion libs/shared/ui/src/lib/components/dialog/dialog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ export default {

const Template: ComponentStory<typeof Dialog> = (args) => <Dialog {...args} />;

let open = false;

export const Primary = Template.bind({});
Primary.args = {
trigger: <Button>Trigger dialog</Button>,
title: 'Dialog title',
children: <p>Dialog content !</p>,
onOpenChange: (isOpen) => (open = isOpen),
children: (
<div>
<p>Dialog content !</p>
</div>
),
open: open,
};
42 changes: 32 additions & 10 deletions libs/shared/ui/src/lib/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import * as DialogLib from '@radix-ui/react-dialog';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import Button, { ButtonColor, ButtonSize } from '../button/button';

export interface DialogProps {
trigger: React.ReactNode;
triggerContainerClassName?: string;
title: string;
children: React.ReactNode;
buttons?: React.ReactNode;
open?: boolean;
open: boolean;
onOpenChange?: (open: boolean) => void;
}

export function Dialog(props: DialogProps) {
const { trigger, title, children, buttons, open = false } = props;
const {
trigger,
title,
children,
buttons,
open,
triggerContainerClassName = '',
onOpenChange,
} = props;
const [isOpen, setIsOpen] = useState(open);

const handleOpenChange = (open: boolean) => {
onOpenChange && onOpenChange(open);
};

useEffect(() => {
setIsOpen(open);
}, [open]);

return (
<DialogLib.Root defaultOpen={isOpen} onOpenChange={(o) => setIsOpen(o)}>
<DialogLib.Trigger className="w-full">{trigger}</DialogLib.Trigger>
<DialogLib.Root open={isOpen} onOpenChange={handleOpenChange}>
<DialogLib.Trigger asChild>
<div className={triggerContainerClassName}>{trigger}</div>
</DialogLib.Trigger>
<DialogLib.Portal>
<DialogLib.Overlay className="w-full h-full bg-dark-500 opacity-70 fixed top-0 left-0 z-10" />
<DialogLib.Content
Expand All @@ -27,11 +47,13 @@ export function Dialog(props: DialogProps) {
{title}
</DialogLib.Title>
<DialogLib.Close asChild>
<Button
buttonIcon="close-line"
size={ButtonSize.Small}
color={ButtonColor.Dark}
/>
<div>
<Button
buttonIcon="close-line"
size={ButtonSize.Small}
color={ButtonColor.Dark}
/>
</div>
</DialogLib.Close>
</div>
<div className="mb-3">{children}</div>
Expand Down

0 comments on commit 1051a26

Please sign in to comment.