-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmodal.tsx
More file actions
149 lines (138 loc) · 5.03 KB
/
modal.tsx
File metadata and controls
149 lines (138 loc) · 5.03 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { useMemo } from 'react';
import { Resizable, type ResizableProps } from 'react-resizable';
import { Alert, type AlertProps, Modal, type ModalProps as AntdModalProps, Spin } from 'antd';
import classNames from 'classnames';
import { omit } from 'lodash-es';
import Float, { type IFloatProps } from '../float';
import useMergeOption, { type MergeOption } from '../useMergeOption';
import { isAlertObjectProps } from '../utils';
import Handler from './handle';
import './index.scss';
export type RectState = { width: number; height: number };
export interface ModalProps extends AntdModalProps {
size?: 'small' | 'default' | 'middle' | 'large';
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
draggable?: IFloatProps['draggable'];
resizable?: MergeOption<Partial<ResizableProps>>;
rect?: RectState;
position?: IFloatProps['position'];
loading?: boolean;
onPositionChange?: (data: NonNullable<IFloatProps['position']>) => void;
onRectChange?: (data: RectState) => void;
}
const getWidthFromSize = (size: ModalProps['size']) => {
if (size === 'small') return 400;
if (size === 'middle') return 640;
if (size === 'large') return 900;
return 520;
};
export default function InternalModal({
bodyStyle,
banner,
size = 'default',
children,
width,
className,
draggable = false,
position,
resizable = false,
rect,
loading = false,
onRectChange,
onPositionChange,
modalRender,
...rest
}: ModalProps) {
const mergedDraggable = useMergeOption(draggable, { handle: '.ant-modal-header' });
const mergedResizable = useMergeOption(resizable, {
axis: 'both',
resizeHandles: ['s', 'w', 'e', 'n', 'ne', 'nw', 'sw', 'se'],
width: 400,
height: 400,
minConstraints: [400, 400],
handle: <Handler />,
});
const final = useMemo(() => {
if (mergedResizable.disabled)
return { width: width ?? getWidthFromSize(size), height: 'auto' };
return {
width: rect?.width || mergedResizable.options.width || 0,
height: rect?.height || mergedResizable.options.height || 0,
};
}, [mergedResizable, width, size, rect]);
const handleResize: ResizableProps['onResize'] = (e, data) => {
mergedResizable.options.onResize?.(e, data);
const nextSize = { width: data.size.width, height: data.size.height };
onRectChange?.(nextSize);
if (mergedDraggable.disabled || !position) return;
const vertical = data.handle.includes('n');
const horizontal = data.handle.includes('w');
const offsetY = vertical ? nextSize.height - (final.height as number) : 0;
const offsetX = horizontal ? nextSize.width - (final.width as number) : 0;
const after = {
x: position.x - offsetX,
y: position.y - offsetY,
};
// Prevent unnecessary update
if (after.x === position.x && after.y === position.y) return;
onPositionChange?.(after);
};
const handleRenderModal = (modal: React.ReactNode) => {
const container = modalRender?.(modal) || modal;
let child = <>{container}</>;
if (!mergedResizable.disabled) {
child = (
<Resizable
{...mergedResizable.options}
axis={mergedResizable.options.axis}
width={final.width as number}
height={final.height as number}
onResize={handleResize}
>
{child}
</Resizable>
);
}
if (!mergedDraggable.disabled) {
child = (
<Float
draggable={mergedDraggable.options}
position={position}
style={{ width: final.width, height: final.height }}
onChange={(_, { x, y }) => onPositionChange?.({ x, y })}
>
{child}
</Float>
);
}
return child;
};
return (
<Modal
className={classNames(
'dtc-modal',
!mergedDraggable.disabled && 'dtc-modal__draggable',
!mergedResizable.disabled && 'dtc-modal__resizable',
className
)}
bodyStyle={{ padding: 0, ...bodyStyle }}
style={{ height: final.height, width: final.width }}
width={final.width}
modalRender={handleRenderModal}
maskClosable={false}
{...rest}
>
{banner && (
<Alert
className="dtc-modal-alert"
message={isAlertObjectProps(banner) ? banner.message : banner}
banner
{...(isAlertObjectProps(banner) ? omit(banner, 'message') : {})}
/>
)}
<section className="dtc-modal-body">
<Spin spinning={loading}>{children}</Spin>
</section>
</Modal>
);
}