mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 09:26:06 +08:00
chore: simplify code
This commit is contained in:
parent
23fe5d6508
commit
8b0eee8237
@ -196,7 +196,7 @@ describe('Modal.hook', () => {
|
||||
const Demo = () => {
|
||||
const [modal, contextHolder] = Modal.useModal();
|
||||
|
||||
const openBrokenModal = React.useCallback(() => {
|
||||
const openBrokenModal = () => {
|
||||
const instance = modal.info({
|
||||
title: 'Light',
|
||||
});
|
||||
@ -209,7 +209,7 @@ describe('Modal.hook', () => {
|
||||
...ori,
|
||||
content: 'Little',
|
||||
}));
|
||||
}, [modal]);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
@ -229,6 +229,39 @@ describe('Modal.hook', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('support update config', () => {
|
||||
const Demo = () => {
|
||||
const [modal, contextHolder] = Modal.useModal();
|
||||
|
||||
const openBrokenModal = () => {
|
||||
const instance = modal.info({
|
||||
title: 'Light',
|
||||
content: 'Little',
|
||||
});
|
||||
|
||||
instance.update(() => ({
|
||||
title: 'Bamboo',
|
||||
}));
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
{contextHolder}
|
||||
<div className="open-hook-modal-btn" onClick={openBrokenModal}>
|
||||
Test hook modal
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const { container } = render(<Demo />);
|
||||
fireEvent.click(container.querySelector('.open-hook-modal-btn')!);
|
||||
expect(document.body.querySelector('.ant-modal-confirm-title')!.textContent).toEqual('Bamboo');
|
||||
expect(document.body.querySelector('.ant-modal-confirm-content')!.textContent).toEqual(
|
||||
'Little',
|
||||
);
|
||||
});
|
||||
|
||||
it('destroy before render', () => {
|
||||
const Demo = () => {
|
||||
const [modal, contextHolder] = Modal.useModal();
|
||||
|
@ -50,13 +50,11 @@ const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> =
|
||||
destroy: close,
|
||||
update: (newConfig) => {
|
||||
setInnerConfig((originConfig) => {
|
||||
if (typeof newConfig === 'function') {
|
||||
return newConfig(originConfig);
|
||||
}
|
||||
const nextConfig = typeof newConfig === 'function' ? newConfig(originConfig) : newConfig;
|
||||
|
||||
return {
|
||||
...originConfig,
|
||||
...newConfig,
|
||||
...nextConfig,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
@ -39,26 +39,17 @@ function useModal(): readonly [instance: HookAPI, contextHolder: React.ReactElem
|
||||
const holderRef = React.useRef<ElementsHolderRef>(null);
|
||||
|
||||
// ========================== Effect ==========================
|
||||
const [actionQueue, setActionQueue] = React.useState<
|
||||
[React.RefObject<HookModalRef | null>, VoidFunction][]
|
||||
>([]);
|
||||
const [actionQueue, setActionQueue] = React.useState<VoidFunction[]>([]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (actionQueue.length) {
|
||||
const cloneQueue = [...actionQueue];
|
||||
const nextQueue: typeof actionQueue = [];
|
||||
|
||||
cloneQueue.forEach((actionInfo) => {
|
||||
const [ref, action] = actionInfo;
|
||||
|
||||
if (ref.current) {
|
||||
action();
|
||||
} else {
|
||||
nextQueue.push(actionInfo);
|
||||
}
|
||||
cloneQueue.forEach((action) => {
|
||||
action();
|
||||
});
|
||||
|
||||
setActionQueue(nextQueue);
|
||||
setActionQueue([]);
|
||||
}
|
||||
}, [actionQueue]);
|
||||
|
||||
@ -108,7 +99,7 @@ function useModal(): readonly [instance: HookAPI, contextHolder: React.ReactElem
|
||||
if (modalRef.current) {
|
||||
destroyAction();
|
||||
} else {
|
||||
setActionQueue((prev) => [...prev, [modalRef, destroyAction]]);
|
||||
setActionQueue((prev) => [...prev, destroyAction]);
|
||||
}
|
||||
},
|
||||
update: (newConfig) => {
|
||||
@ -119,7 +110,7 @@ function useModal(): readonly [instance: HookAPI, contextHolder: React.ReactElem
|
||||
if (modalRef.current) {
|
||||
updateAction();
|
||||
} else {
|
||||
setActionQueue((prev) => [...prev, [modalRef, updateAction]]);
|
||||
setActionQueue((prev) => [...prev, updateAction]);
|
||||
}
|
||||
},
|
||||
then: (resolve) => {
|
||||
|
Loading…
Reference in New Issue
Block a user