mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 09:26:06 +08:00
fix: hook modal not trigger onCancel (#28063)
This commit is contained in:
parent
79123e65cc
commit
e063a3a139
@ -62,4 +62,42 @@ describe('Modal.hook', () => {
|
||||
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('hooks modal should trigger onCancel', () => {
|
||||
let cancelCount = 0;
|
||||
const Demo = () => {
|
||||
const [modal, contextHolder] = Modal.useModal();
|
||||
|
||||
const openBrokenModal = React.useCallback(() => {
|
||||
modal.info({
|
||||
okType: 'default',
|
||||
maskClosable: true,
|
||||
okCancel: true,
|
||||
onCancel: () => {
|
||||
cancelCount += 1;
|
||||
},
|
||||
content: 'Hello!',
|
||||
});
|
||||
}, [modal]);
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
{contextHolder}
|
||||
<div className="open-hook-modal-btn" onClick={openBrokenModal}>
|
||||
Test hook modal
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const wrapper = mount(<Demo />);
|
||||
|
||||
wrapper.find('.open-hook-modal-btn').simulate('click');
|
||||
wrapper.find('.ant-modal-confirm-btns .ant-btn').first().simulate('click');
|
||||
expect(cancelCount).toEqual(1); // click cancel btn, trigger onCancel
|
||||
|
||||
wrapper.find('.open-hook-modal-btn').simulate('click');
|
||||
wrapper.find('.ant-modal-wrap').simulate('click');
|
||||
expect(cancelCount).toEqual(2); // click modal wrapper, trigger onCancel
|
||||
});
|
||||
});
|
||||
|
@ -32,8 +32,12 @@ const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> =
|
||||
const prefixCls = getPrefixCls('modal');
|
||||
const rootPrefixCls = getPrefixCls();
|
||||
|
||||
function close() {
|
||||
function close(...args: any[]) {
|
||||
setVisible(false);
|
||||
const triggerCancel = args.some(param => param && param.triggerCancel);
|
||||
if (innerConfig.onCancel && triggerCancel) {
|
||||
innerConfig.onCancel();
|
||||
}
|
||||
}
|
||||
|
||||
React.useImperativeHandle(ref, () => ({
|
||||
|
Loading…
Reference in New Issue
Block a user