forked from DTStack/dt-react-component
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal.tsx
More file actions
53 lines (47 loc) · 1.5 KB
/
modal.tsx
File metadata and controls
53 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react';
import { Alert, type AlertProps, Modal as AntdModal, type ModalProps } from 'antd';
import classNames from 'classnames';
import { omit } from 'lodash-es';
import './index.scss';
export interface IModalProps extends ModalProps {
size?: 'small' | 'default' | 'middle' | 'large';
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
}
const getWidthFromSize = (size: IModalProps['size']) => {
if (size === 'small') return 400;
if (size === 'middle') return 640;
if (size === 'large') return 900;
return 520;
};
const isValidBanner = (banner: IModalProps['banner']): banner is AlertProps['message'] => {
if (typeof banner === 'object') return React.isValidElement(banner);
return true;
};
export default function Modal({
bodyStyle,
banner,
size = 'default',
children,
width,
className,
...rest
}: IModalProps) {
const finalWidth = width ?? getWidthFromSize(size);
return (
<AntdModal
className={classNames('dtc-modal', className)}
bodyStyle={{ padding: 0, ...bodyStyle }}
width={finalWidth}
{...rest}
>
{banner && (
<Alert
message={isValidBanner(banner) ? banner : banner.message}
banner
{...(isValidBanner(banner) ? {} : omit(banner, 'message'))}
/>
)}
<section className="dtc-modal-body">{children}</section>
</AntdModal>
);
}