-
Notifications
You must be signed in to change notification settings - Fork 120
/
modal.tsx
129 lines (116 loc) · 3.39 KB
/
modal.tsx
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
import React, { useEffect } from 'react';
import Dialog from 'rc-dialog';
import { IDialogPropTypes } from 'rc-dialog/lib/IDialogPropTypes';
import { classNames } from 'mo/common/className';
import {
modalClassName,
closeDialogDescriptorClassName,
wrapDialogClassName,
} from './base';
import { ConfrimType } from './';
import { Icon } from 'mo/components/icon';
import { Button, IButtonProps } from 'mo/components/button';
export interface IModalProps extends IDialogPropTypes {
onOk?: (e: React.MouseEvent<HTMLElement>) => void;
onCancel?: (e: React.SyntheticEvent<Element, Event>) => void;
centered?: boolean;
cancelText?: React.ReactNode;
okText?: React.ReactNode;
okButtonProps?: IButtonProps;
cancelButtonProps?: IButtonProps;
okCancel?: boolean;
}
export interface IModalFuncProps extends IDialogPropTypes {
cancelText?: React.ReactNode;
okText?: React.ReactNode;
icon?: string | JSX.Element;
content?: React.ReactNode;
onOk?: (...args: any[]) => any;
onCancel?: (...args: any[]) => void;
okButtonProps?: IButtonProps;
cancelButtonProps?: IButtonProps;
centered?: boolean;
okCancel?: boolean;
type?: ConfrimType;
}
export const destroyFns: Array<() => void> = [];
let mousePosition: any;
const getClickPosition = (e: MouseEvent) => {
mousePosition = {
x: e.pageX,
y: e.pageY,
};
setTimeout(() => {
mousePosition = null;
}, 100);
};
const closeIconToRender = (
<span className={closeDialogDescriptorClassName}>
<Icon type="close" />
</span>
);
export const Modal: React.FC<IModalProps> = (props: IModalProps) => {
useEffect(() => {
document.documentElement.addEventListener(
'click',
getClickPosition,
true
);
return () => {
document.documentElement.removeEventListener(
'click',
getClickPosition,
true
);
};
}, []);
const handleCancel = (e: React.SyntheticEvent<Element, Event>) => {
const { onCancel } = props;
onCancel?.(e);
};
const handleOk = (e: React.MouseEvent<HTMLButtonElement>) => {
const { onOk } = props;
onOk?.(e);
};
const {
footer,
visible,
wrapClassName,
centered,
getContainer,
closeIcon,
cancelText = 'Cancel',
okText = 'Ok',
...restProps
} = props;
const wrapClassNameExtended = classNames(wrapClassName, {
[wrapDialogClassName]: !!centered,
});
const renderFooter = () => {
const { footer, cancelButtonProps, okButtonProps } = props;
if (footer !== undefined) return footer;
return (
<>
<Button onClick={handleCancel} {...cancelButtonProps}>
{cancelText}
</Button>
<Button onClick={handleOk} {...okButtonProps}>
{okText}
</Button>
</>
);
};
return (
<Dialog
{...restProps}
getContainer={getContainer}
prefixCls={modalClassName}
wrapClassName={wrapClassNameExtended}
footer={renderFooter()}
visible={visible}
mousePosition={mousePosition}
onClose={handleCancel}
closeIcon={closeIconToRender}
/>
);
};