|
1 |
| -import { Modal as AntdModal } from 'antd'; |
2 |
| -import { ModalStaticFunctions } from 'antd/lib/modal/confirm'; |
| 1 | +import React from 'react'; |
| 2 | +import { CheckFilled, CloseFilled, InformationFilled, WarningFilled } from '@dtinsight/react-icons'; |
| 3 | +import { Modal as AntdModal, ModalFuncProps } from 'antd'; |
| 4 | +import { ModalFunc, ModalStaticFunctions } from 'antd/lib/modal/confirm'; |
3 | 5 |
|
| 6 | +import { getRunTimeLocale, ModalLocale } from './locale'; |
4 | 7 | import InternalModal from './modal';
|
5 | 8 |
|
6 | 9 | type ModalType = typeof InternalModal &
|
7 | 10 | ModalStaticFunctions & {
|
8 | 11 | useModal: typeof AntdModal.useModal;
|
9 | 12 | destroyAll: () => void;
|
10 | 13 | config: typeof AntdModal.config;
|
| 14 | + delete: (props: ModalFuncProps) => void; |
11 | 15 | };
|
12 | 16 |
|
13 | 17 | const { useModal, info, success, error, warning, confirm, destroyAll, config } = AntdModal;
|
14 | 18 |
|
15 |
| -const Modal = InternalModal as ModalType; |
| 19 | +const getCustomProps = ( |
| 20 | + locale: ModalLocale |
| 21 | +): Record< |
| 22 | + | keyof Pick<ModalStaticFunctions, 'info' | 'success' | 'error' | 'warning' | 'confirm'> |
| 23 | + | 'delete', |
| 24 | + ModalFuncProps |
| 25 | +> => { |
| 26 | + return { |
| 27 | + info: { icon: <InformationFilled color="#1D78FF" />, okText: locale.okText }, |
| 28 | + success: { icon: <CheckFilled color="#11D7B2" />, okText: locale.okText }, |
| 29 | + error: { |
| 30 | + icon: <CloseFilled color="#F96C5B" />, |
| 31 | + okText: locale.okText, |
| 32 | + okButtonProps: { danger: true }, |
| 33 | + }, |
| 34 | + warning: { icon: <WarningFilled color="#FBB310" />, okText: locale.okText }, |
| 35 | + confirm: { |
| 36 | + icon: <WarningFilled color="#FBB310" />, |
| 37 | + okText: locale.okText, |
| 38 | + cancelText: locale.cancelText, |
| 39 | + }, |
| 40 | + delete: { |
| 41 | + icon: <CloseFilled color="#F96C5B" />, |
| 42 | + okButtonProps: { danger: true }, |
| 43 | + okText: locale.okText, |
| 44 | + cancelText: locale.notYetText, |
| 45 | + }, |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +function withCustomIcon(fn: ModalFunc, type: keyof ReturnType<typeof getCustomProps>) { |
| 50 | + return (props: ModalFuncProps) => { |
| 51 | + const locale = getRunTimeLocale(); |
| 52 | + const customProps = getCustomProps(locale); |
| 53 | + return fn({ ...customProps[type], wrapClassName: 'dtc-modal', ...props }); |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +const Modal = InternalModal as unknown as ModalType; |
16 | 58 |
|
17 | 59 | Object.assign(Modal, {
|
| 60 | + InternalModal, |
18 | 61 | useModal,
|
19 |
| - info, |
20 |
| - success, |
21 |
| - error, |
22 |
| - warning, |
23 |
| - confirm, |
| 62 | + info: withCustomIcon(info, 'info'), |
| 63 | + success: withCustomIcon(success, 'success'), |
| 64 | + error: withCustomIcon(error, 'error'), |
| 65 | + warning: withCustomIcon(warning, 'warning'), |
| 66 | + confirm: withCustomIcon(confirm, 'confirm'), |
| 67 | + delete: withCustomIcon(confirm, 'delete'), |
24 | 68 | destroyAll,
|
25 | 69 | config,
|
26 | 70 | });
|
|
0 commit comments