/
Modal.tsx
221 lines (205 loc) · 5.97 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as React from 'react';
import Dialog from 'rc-dialog';
import * as PropTypes from 'prop-types';
import classNames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import Button from '../button';
import { ButtonType, NativeButtonProps } from '../button/button';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { getConfirmLocale } from './locale';
import Icon from '../icon';
let mousePosition: { x: number, y: number } | null;
let mousePositionEventBinded: boolean;
export interface ModalProps {
/** 对话框是否可见*/
visible?: boolean;
/** 确定按钮 loading*/
confirmLoading?: boolean;
/** 标题*/
title?: React.ReactNode | string;
/** 是否显示右上角的关闭按钮*/
closable?: boolean;
/** 点击确定回调*/
onOk?: (e: React.MouseEvent<any>) => void;
/** 点击模态框右上角叉、取消按钮、Props.maskClosable 值为 true 时的遮罩层或键盘按下 Esc 时的回调*/
onCancel?: (e: React.MouseEvent<any>) => void;
afterClose?: () => void;
/** 居中 */
centered?: boolean;
/** 宽度*/
width?: string | number;
/** 底部内容*/
footer?: React.ReactNode;
/** 确认按钮文字*/
okText?: string;
/** 确认按钮类型*/
okType?: ButtonType;
/** 取消按钮文字*/
cancelText?: string;
/** 点击蒙层是否允许关闭*/
maskClosable?: boolean;
okButtonProps?: NativeButtonProps;
cancelButtonProps?: NativeButtonProps;
destroyOnClose?: boolean;
style?: React.CSSProperties;
wrapClassName?: string;
maskTransitionName?: string;
transitionName?: string;
className?: string;
getContainer?: (instance: React.ReactInstance) => HTMLElement;
zIndex?: number;
bodyStyle?: React.CSSProperties;
maskStyle?: React.CSSProperties;
mask?: boolean;
keyboard?: boolean;
wrapProps?: any;
prefixCls?: string;
}
export interface ModalFuncProps {
prefixCls?: string;
className?: string;
visible?: boolean;
title?: React.ReactNode;
content?: React.ReactNode;
onOk?: (...args: any[]) => any | PromiseLike<any>;
onCancel?: (...args: any[]) => any | PromiseLike<any>;
okButtonProps?: NativeButtonProps;
cancelButtonProps?: NativeButtonProps;
centered?: boolean;
width?: string | number;
iconClassName?: string;
okText?: string;
okType?: ButtonType;
cancelText?: string;
iconType?: string;
maskClosable?: boolean;
zIndex?: number;
okCancel?: boolean;
style?: React.CSSProperties;
maskStyle?: React.CSSProperties;
type?: string;
keyboard?: boolean;
getContainer?: (instance: React.ReactInstance) => HTMLElement;
autoFocusButton?: null | 'ok' | 'cancel';
}
export type ModalFunc = (props: ModalFuncProps) => {
destroy: () => void,
update: (newConfig: ModalFuncProps) => void,
};
export interface ModalLocale {
okText: string;
cancelText: string;
justOkText: string;
}
export default class Modal extends React.Component<ModalProps, {}> {
static info: ModalFunc;
static success: ModalFunc;
static error: ModalFunc;
static warn: ModalFunc;
static warning: ModalFunc;
static confirm: ModalFunc;
static defaultProps = {
prefixCls: 'ant-modal',
width: 520,
transitionName: 'zoom',
maskTransitionName: 'fade',
confirmLoading: false,
visible: false,
okType: 'primary' as ButtonType,
okButtonDisabled: false,
cancelButtonDisabled: false,
};
static propTypes = {
prefixCls: PropTypes.string,
onOk: PropTypes.func,
onCancel: PropTypes.func,
okText: PropTypes.node,
cancelText: PropTypes.node,
centered: PropTypes.bool,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
confirmLoading: PropTypes.bool,
visible: PropTypes.bool,
align: PropTypes.object,
footer: PropTypes.node,
title: PropTypes.node,
closable: PropTypes.bool,
};
handleCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
const onCancel = this.props.onCancel;
if (onCancel) {
onCancel(e);
}
}
handleOk = (e: React.MouseEvent<HTMLButtonElement>) => {
const onOk = this.props.onOk;
if (onOk) {
onOk(e);
}
}
componentDidMount() {
if (mousePositionEventBinded) {
return;
}
// 只有点击事件支持从鼠标位置动画展开
addEventListener(document.documentElement, 'click', (e: MouseEvent) => {
mousePosition = {
x: e.pageX,
y: e.pageY,
};
// 100ms 内发生过点击事件,则从点击位置动画展示
// 否则直接 zoom 展示
// 这样可以兼容非点击方式展开
setTimeout(() => mousePosition = null, 100);
});
mousePositionEventBinded = true;
}
renderFooter = (locale: ModalLocale) => {
const { okText, okType, cancelText, confirmLoading } = this.props;
return (
<div>
<Button
onClick={this.handleCancel}
{...this.props.cancelButtonProps}
>
{cancelText || locale.cancelText}
</Button>
<Button
type={okType}
loading={confirmLoading}
onClick={this.handleOk}
{...this.props.okButtonProps}
>
{okText || locale.okText}
</Button>
</div>
);
}
render() {
const { footer, visible, wrapClassName, centered, prefixCls, ...restProps } = this.props;
const defaultFooter = (
<LocaleReceiver
componentName="Modal"
defaultLocale={getConfirmLocale()}
>
{this.renderFooter}
</LocaleReceiver>
);
const closeIcon = (
<span className={`${prefixCls}-close-x`}>
<Icon className={`${prefixCls}-close-icon`} type={'close'}/>
</span>
);
return (
<Dialog
{...restProps}
prefixCls={prefixCls}
wrapClassName={classNames({ [`${prefixCls}-centered`]: !!centered }, wrapClassName)}
footer={footer === undefined ? defaultFooter : footer}
visible={visible}
mousePosition={mousePosition}
onClose={this.handleCancel}
closeIcon={closeIcon}
/>
);
}
}