mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 00:29:12 +08:00
59ad48476b
* chore: add boime lint * fix lint * use files ignore * revert change * ignore clarity.js * fix some errors * fix some errors * fix some errors * fix some errors * add yml file * Update clarity.js Signed-off-by: afc163 <afc163@gmail.com> * add npm run lint:biome * add npm run lint:biome * fix test case * fix ts errors * fix ts errors * fix lint and add .lintstagedrc * shorten prop name * chore: update package.json * update biome.json * chore: remove stylelint * chore: useOptionalChain * fix lint * biome format * prettier all code * prettier all code * fix site test --------- Signed-off-by: afc163 <afc163@gmail.com>
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
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';
|
|
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: ModalFuncProps) => void;
|
|
}
|
|
|
|
const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> = (
|
|
{ afterClose: hookAfterClose, config, ...restProps },
|
|
ref,
|
|
) => {
|
|
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: any[]) => {
|
|
setOpen(false);
|
|
const triggerCancel = args.some((param) => param?.triggerCancel);
|
|
if (triggerCancel) {
|
|
innerConfig.onCancel?.(() => {}, ...args.slice(1));
|
|
}
|
|
};
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
destroy: close,
|
|
update: (newConfig: ModalFuncProps) => {
|
|
setInnerConfig((originConfig) => ({
|
|
...originConfig,
|
|
...newConfig,
|
|
}));
|
|
},
|
|
}));
|
|
|
|
const mergedOkCancel = innerConfig.okCancel ?? innerConfig.type === 'confirm';
|
|
|
|
const [contextLocale] = useLocale('Modal', defaultLocale.Modal);
|
|
|
|
return (
|
|
<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 React.forwardRef(HookModal);
|