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

This commit is contained in:
CatsJuice
2024-04-02 08:23:26 +00:00
parent 3c01d944fb
commit 381be8a982
2 changed files with 23 additions and 2 deletions

View File

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

View File

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