Skip to content

Commit

Permalink
feat(Modal): add destroyAll to close all method modals
Browse files Browse the repository at this point in the history
  • Loading branch information
ZxBing0066 committed Dec 3, 2021
1 parent 7159bda commit 1eaafc0
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/components/Modal/method.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ import Modal from './Modal';
const methodWarning = () => warning(`You're using a method to call a Modal, this may cause a lot of problems.`);
const destroyWarning = () => warning(`Wrong name of destory, please use destroy to instead`);

let i = 0;
const getCallUID = () => '_modal_id_' + i++;
const queueMap = {};

const cleanModal = id => {
const modal = queueMap[id];
if (modal && modal.destroy) {
modal.destroy();
}
delete queueMap[id];
};
const addModal = modal => {
const id = getCallUID();
queueMap[id] = modal;
return id;
};
const cleanAllModal = () => {
Object.keys(queueMap).forEach(cleanModal);
};

const ContextWrap = ({ children }) => {
return (
<ThemeProvider theme={getRuntimeTheme()}>
Expand Down Expand Up @@ -59,12 +79,15 @@ const pop = props => {
ReactDOM.render(<ModalWrap {...props} reportUpdate={_update => (update = _update)} />, container);

methodWarning();

const callID = addModal({ destroy });

return {
destory: () => {
destroy();
cleanModal(callID);
destroyWarning();
},
destroy,
destroy: () => cleanModal(callID),
update
};
};
Expand All @@ -82,8 +105,10 @@ const openModal = modal => {
ReactDOM.render(<ContextWrap>{modal}</ContextWrap>, container);

methodWarning();

const callID = addModal({ destroy });
return {
destroy
destroy: () => cleanModal(callID)
};
};

Expand Down Expand Up @@ -164,4 +189,4 @@ const open = ({ onOk = () => {}, onClose = () => {}, ...rest }, content) => {
return modal;
};

export { alert, confirm, open, openModal };
export { alert, confirm, open, openModal, cleanAllModal as destroyAll };

0 comments on commit 1eaafc0

Please sign in to comment.