Skip to content

Commit dcc7e5d

Browse files
authored
feat(modal): add antd modal method and useModal (#552)
#551
1 parent 1c86c43 commit dcc7e5d

File tree

3 files changed

+78
-48
lines changed

3 files changed

+78
-48
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export { default as Image } from './image';
2121
export { default as Input } from './input';
2222
export { default as KeyEventListener } from './keyEventListener';
2323
export { default as MarkdownRender } from './markdownRender';
24-
export { default as Modal } from './modal';
24+
export { default as Modal } from './modal/modal';
2525
export { default as NotFound } from './notFound';
2626
export { default as ProgressBar } from './progressBar';
2727
export { default as ProgressLine } from './progressLine';

src/modal/index.tsx

+24-47
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
1-
import React from 'react';
2-
import { Alert, type AlertProps, Modal, type ModalProps } from 'antd';
3-
import classNames from 'classnames';
4-
import { omit } from 'lodash-es';
1+
import { Modal as AntdModal } from 'antd';
2+
import { ModalStaticFunctions } from 'antd/lib/modal/confirm';
53

6-
import './index.scss';
4+
import InternalModal from './modal';
75

8-
export interface IModalProps extends ModalProps {
9-
size?: 'small' | 'default' | 'middle' | 'large';
10-
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
11-
}
6+
type ModalType = typeof InternalModal &
7+
ModalStaticFunctions & {
8+
useModal: typeof AntdModal.useModal;
9+
destroyAll: () => void;
10+
config: typeof AntdModal.config;
11+
};
1212

13-
const getWidthFromSize = (size: IModalProps['size']) => {
14-
if (size === 'small') return 400;
15-
if (size === 'middle') return 640;
16-
if (size === 'large') return 900;
17-
return 520;
18-
};
13+
const { useModal, info, success, error, warning, confirm, destroyAll, config } = AntdModal;
1914

20-
const isValidBanner = (banner: IModalProps['banner']): banner is AlertProps['message'] => {
21-
if (typeof banner === 'object') return React.isValidElement(banner);
22-
return true;
23-
};
15+
const Modal = InternalModal as ModalType;
2416

25-
export default function InternalModal({
26-
bodyStyle,
27-
banner,
28-
size = 'default',
29-
children,
30-
width,
31-
className,
32-
...rest
33-
}: IModalProps) {
34-
const finalWidth = width ?? getWidthFromSize(size);
17+
Object.assign(Modal, {
18+
useModal,
19+
info,
20+
success,
21+
error,
22+
warning,
23+
confirm,
24+
destroyAll,
25+
config,
26+
});
3527

36-
return (
37-
<Modal
38-
className={classNames('dtc-modal', className)}
39-
bodyStyle={{ padding: 0, ...bodyStyle }}
40-
width={finalWidth}
41-
{...rest}
42-
>
43-
{banner && (
44-
<Alert
45-
message={isValidBanner(banner) ? banner : banner.message}
46-
banner
47-
{...(isValidBanner(banner) ? {} : omit(banner, 'message'))}
48-
/>
49-
)}
50-
<section className="dtc-modal-body">{children}</section>
51-
</Modal>
52-
);
53-
}
28+
export { IModalProps } from './modal';
29+
30+
export default Modal;

src/modal/modal.tsx

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from 'react';
2+
import { Alert, type AlertProps, Modal as AntdModal, type ModalProps } from 'antd';
3+
import classNames from 'classnames';
4+
import { omit } from 'lodash-es';
5+
6+
import './index.scss';
7+
8+
export interface IModalProps extends ModalProps {
9+
size?: 'small' | 'default' | 'middle' | 'large';
10+
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
11+
}
12+
13+
const getWidthFromSize = (size: IModalProps['size']) => {
14+
if (size === 'small') return 400;
15+
if (size === 'middle') return 640;
16+
if (size === 'large') return 900;
17+
return 520;
18+
};
19+
20+
const isValidBanner = (banner: IModalProps['banner']): banner is AlertProps['message'] => {
21+
if (typeof banner === 'object') return React.isValidElement(banner);
22+
return true;
23+
};
24+
25+
export default function Modal({
26+
bodyStyle,
27+
banner,
28+
size = 'default',
29+
children,
30+
width,
31+
className,
32+
...rest
33+
}: IModalProps) {
34+
const finalWidth = width ?? getWidthFromSize(size);
35+
36+
return (
37+
<AntdModal
38+
className={classNames('dtc-modal', className)}
39+
bodyStyle={{ padding: 0, ...bodyStyle }}
40+
width={finalWidth}
41+
{...rest}
42+
>
43+
{banner && (
44+
<Alert
45+
message={isValidBanner(banner) ? banner : banner.message}
46+
banner
47+
{...(isValidBanner(banner) ? {} : omit(banner, 'message'))}
48+
/>
49+
)}
50+
<section className="dtc-modal-body">{children}</section>
51+
</AntdModal>
52+
);
53+
}

0 commit comments

Comments
 (0)