Skip to content

Commit

Permalink
fix(component): useConfirmModal can't be closed automatically when 'o…
Browse files Browse the repository at this point in the history
…nConfirm' is non-async (#6439)
  • Loading branch information
CatsJuice committed Apr 2, 2024
1 parent 3c01d94 commit 381be8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions packages/frontend/component/src/ui/modal/confirm-modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ export const UsingHook = () => {

return <Button onClick={showConfirm}>Show confirm</Button>;
};

export const AutoClose = () => {
const { openConfirmModal } = useConfirmModal();

const onConfirm = () => {
openConfirmModal({
cancelText: 'Cancel',
confirmButtonOptions: {
children: 'Confirm',
},
title: 'Confirm Modal',
children: 'Are you sure you want to confirm?',
onConfirm: () => console.log('Confirmed'),
onCancel: () => {
console.log('Cancelled');
},
});
};

return <Button onClick={onConfirm}>Show confirm</Button>;
};
4 changes: 2 additions & 2 deletions packages/frontend/component/src/ui/modal/confirm-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export const ConfirmModalProvider = ({ children }: PropsWithChildren) => {

const onConfirm = () => {
setLoading(true);
_onConfirm?.()
?.then(() => onSuccess?.())
return Promise.resolve(_onConfirm?.())
.then(() => onSuccess?.())
.catch(console.error)
.finally(() => autoClose && closeConfirmModal());
};
Expand Down

0 comments on commit 381be8a

Please sign in to comment.