1
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
import * as React from 'react';
|
||||
import type { ConfigUpdate } from '../confirm';
|
||||
import type { ModalFuncProps } from '../interface';
|
||||
export interface HookModalProps {
|
||||
afterClose: () => void;
|
||||
config: ModalFuncProps;
|
||||
onConfirm?: (confirmed: boolean) => void;
|
||||
/**
|
||||
* Do not throw if is await mode
|
||||
*/
|
||||
isSilent?: () => boolean;
|
||||
}
|
||||
export interface HookModalRef {
|
||||
destroy: () => void;
|
||||
update: (config: ConfigUpdate) => void;
|
||||
}
|
||||
declare const HookModal: React.ForwardRefExoticComponent<HookModalProps & React.RefAttributes<HookModalRef>>;
|
||||
export default HookModal;
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
"use client";
|
||||
|
||||
import * as React from 'react';
|
||||
import { ConfigContext } from '../../config-provider';
|
||||
import defaultLocale from '../../locale/en_US';
|
||||
import useLocale from '../../locale/useLocale';
|
||||
import ConfirmDialog from '../ConfirmDialog';
|
||||
const HookModal = /*#__PURE__*/React.forwardRef((props, ref) => {
|
||||
const {
|
||||
afterClose: hookAfterClose,
|
||||
config,
|
||||
...restProps
|
||||
} = props;
|
||||
const [open, setOpen] = React.useState(true);
|
||||
const [innerConfig, setInnerConfig] = React.useState(config);
|
||||
const {
|
||||
direction,
|
||||
getPrefixCls
|
||||
} = React.useContext(ConfigContext);
|
||||
const prefixCls = getPrefixCls('modal');
|
||||
const rootPrefixCls = getPrefixCls();
|
||||
const afterClose = () => {
|
||||
hookAfterClose();
|
||||
innerConfig.afterClose?.();
|
||||
};
|
||||
const close = (...args) => {
|
||||
setOpen(false);
|
||||
const triggerCancel = args.some(param => param?.triggerCancel);
|
||||
if (triggerCancel) {
|
||||
innerConfig.onCancel?.(() => {}, ...args.slice(1));
|
||||
}
|
||||
};
|
||||
React.useImperativeHandle(ref, () => ({
|
||||
destroy: close,
|
||||
update: newConfig => {
|
||||
setInnerConfig(originConfig => {
|
||||
const nextConfig = typeof newConfig === 'function' ? newConfig(originConfig) : newConfig;
|
||||
return {
|
||||
...originConfig,
|
||||
...nextConfig
|
||||
};
|
||||
});
|
||||
}
|
||||
}));
|
||||
const mergedOkCancel = innerConfig.okCancel ?? innerConfig.type === 'confirm';
|
||||
const [contextLocale] = useLocale('Modal', defaultLocale.Modal);
|
||||
return /*#__PURE__*/React.createElement(ConfirmDialog, {
|
||||
prefixCls: prefixCls,
|
||||
rootPrefixCls: rootPrefixCls,
|
||||
...innerConfig,
|
||||
close: close,
|
||||
open: open,
|
||||
afterClose: afterClose,
|
||||
okText: innerConfig.okText || (mergedOkCancel ? contextLocale?.okText : contextLocale?.justOkText),
|
||||
direction: innerConfig.direction || direction,
|
||||
cancelText: innerConfig.cancelText || contextLocale?.cancelText,
|
||||
...restProps
|
||||
});
|
||||
});
|
||||
export default HookModal;
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import * as React from 'react';
|
||||
import type { ModalFunc, ModalStaticFunctions } from '../confirm';
|
||||
export type ModalFuncWithPromise = (...args: Parameters<ModalFunc>) => ReturnType<ModalFunc> & {
|
||||
then: <T>(resolve: (confirmed: boolean) => T, reject: VoidFunction) => Promise<T>;
|
||||
};
|
||||
export type HookAPI = Omit<Record<keyof ModalStaticFunctions, ModalFuncWithPromise>, 'warn'>;
|
||||
declare function useModal(): readonly [instance: HookAPI, contextHolder: React.ReactElement];
|
||||
export default useModal;
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
"use client";
|
||||
|
||||
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
||||
import * as React from 'react';
|
||||
import { usePatchElement } from '../../_util/hooks';
|
||||
import { withConfirm, withError, withInfo, withSuccess, withWarn } from '../confirm';
|
||||
import destroyFns from '../destroyFns';
|
||||
import HookModal from './HookModal';
|
||||
let uuid = 0;
|
||||
const ElementsHolder = /*#__PURE__*/React.memo(/*#__PURE__*/React.forwardRef((_props, ref) => {
|
||||
const [elements, patchElement] = usePatchElement();
|
||||
React.useImperativeHandle(ref, () => ({
|
||||
patchElement
|
||||
}), [patchElement]);
|
||||
return /*#__PURE__*/React.createElement(React.Fragment, null, elements);
|
||||
}));
|
||||
function useModal() {
|
||||
const holderRef = React.useRef(null);
|
||||
// ========================== Effect ==========================
|
||||
const [actionQueue, setActionQueue] = React.useState([]);
|
||||
React.useEffect(() => {
|
||||
if (actionQueue.length) {
|
||||
const cloneQueue = _toConsumableArray(actionQueue);
|
||||
cloneQueue.forEach(action => {
|
||||
action();
|
||||
});
|
||||
setActionQueue([]);
|
||||
}
|
||||
}, [actionQueue]);
|
||||
// =========================== Hook ===========================
|
||||
const getConfirmFunc = React.useCallback(withFunc => function hookConfirm(config) {
|
||||
uuid += 1;
|
||||
const modalRef = /*#__PURE__*/React.createRef();
|
||||
// Proxy to promise with `onClose`
|
||||
let resolvePromise;
|
||||
const promise = new Promise(resolve => {
|
||||
resolvePromise = resolve;
|
||||
});
|
||||
let silent = false;
|
||||
let closeFunc;
|
||||
const modal = /*#__PURE__*/React.createElement(HookModal, {
|
||||
key: `modal-${uuid}`,
|
||||
config: withFunc(config),
|
||||
ref: modalRef,
|
||||
afterClose: () => {
|
||||
closeFunc?.();
|
||||
},
|
||||
isSilent: () => silent,
|
||||
onConfirm: confirmed => {
|
||||
resolvePromise(confirmed);
|
||||
}
|
||||
});
|
||||
closeFunc = holderRef.current?.patchElement(modal);
|
||||
if (closeFunc) {
|
||||
destroyFns.push(closeFunc);
|
||||
}
|
||||
const instance = {
|
||||
destroy: () => {
|
||||
function destroyAction() {
|
||||
modalRef.current?.destroy();
|
||||
}
|
||||
if (modalRef.current) {
|
||||
destroyAction();
|
||||
} else {
|
||||
setActionQueue(prev => [].concat(_toConsumableArray(prev), [destroyAction]));
|
||||
}
|
||||
},
|
||||
update: newConfig => {
|
||||
function updateAction() {
|
||||
modalRef.current?.update(newConfig);
|
||||
}
|
||||
if (modalRef.current) {
|
||||
updateAction();
|
||||
} else {
|
||||
setActionQueue(prev => [].concat(_toConsumableArray(prev), [updateAction]));
|
||||
}
|
||||
},
|
||||
then: resolve => {
|
||||
silent = true;
|
||||
return promise.then(resolve);
|
||||
}
|
||||
};
|
||||
return instance;
|
||||
}, []);
|
||||
const fns = React.useMemo(() => ({
|
||||
info: getConfirmFunc(withInfo),
|
||||
success: getConfirmFunc(withSuccess),
|
||||
error: getConfirmFunc(withError),
|
||||
warning: getConfirmFunc(withWarn),
|
||||
confirm: getConfirmFunc(withConfirm)
|
||||
}), [getConfirmFunc]);
|
||||
return [fns, /*#__PURE__*/React.createElement(ElementsHolder, {
|
||||
key: "modal-holder",
|
||||
ref: holderRef
|
||||
})];
|
||||
}
|
||||
export default useModal;
|
||||
Reference in New Issue
Block a user