fix(component): useConfirmModal can't be closed automatically when 'onConfirm' is non-async (#6439)

This commit is contained in:
CatsJuice committed 2024-04-02 08:23:27 +00:00
1 parent 3c01d944fb
commit 381be8a982
2 files changed
+23 -2

No files matched your search

@@ -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>;
};
@@ -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());
};