-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathInlineModal.tsx
177 lines (162 loc) · 4.38 KB
/
InlineModal.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
import React, { Component, ReactElement } from 'react';
import { ModalProps } from 'antd/es/modal';
import { Modal, Icon } from 'antd';
import RouteContext from '@ant-design/pro-layout/es/RouteContext';
export interface InlineModalProps extends ModalProps {
element?: ReactElement;
maxmin: boolean;
fullScreen: boolean;
beforeOpen?: Function;
beforeClose?: Function;
}
interface InlineModalState {
visible: boolean;
fullScreen: boolean;
}
class InlineModal extends Component<InlineModalProps, InlineModalState> {
static defaultProps = {
maxmin: true,
fullScreen: false,
};
constructor(props: InlineModalProps) {
super(props);
this.state = {
visible: !!props.visible,
fullScreen: props.fullScreen,
};
}
// 显示模态框
showModalHandler = (e: React.MouseEvent<HTMLElement>) => {
e.stopPropagation();
const { beforeOpen } = this.props;
if (beforeOpen) {
beforeOpen();
}
this.handleModalVisible(true);
};
// 隐藏模态框
hideModalHandler = () => {
const { beforeClose } = this.props;
if (beforeClose) {
beforeClose();
}
this.handleModalVisible(false);
};
handleModalVisible = (flag?: boolean) => {
this.setState({
visible: !!flag,
});
};
okHandler = (e: React.MouseEvent<HTMLElement>) => {
e.stopPropagation();
const { onOk } = this.props;
if (onOk) {
const ret = onOk(e);
// @ts-ignore
if (ret instanceof Promise) {
ret
.then(() => {
this.hideModalHandler();
})
.catch(err => {
console.error(err);
});
// @ts-ignore
} else if (ret) {
this.hideModalHandler();
}
} else {
this.hideModalHandler();
}
};
cancelHandler = (e: React.MouseEvent<HTMLElement>) => {
e.stopPropagation();
const { onCancel } = this.props;
if (onCancel) {
const ret = onCancel(e);
// @ts-ignore
if (ret instanceof Promise) {
ret
.then(() => {
this.hideModalHandler();
})
.catch(() => {});
} else {
this.hideModalHandler();
}
} else {
this.hideModalHandler();
}
};
toggleFullScreen = () => {
const { fullScreen } = this.state;
this.setState({
fullScreen: !fullScreen,
});
};
titleRender = () => {
const { title, maxmin } = this.props;
const { fullScreen } = this.state;
return (
<>
{title}
{maxmin && (
<button
type="button"
className="ant-modal-close"
style={{ right: 42 }}
onClick={this.toggleFullScreen}
>
<span className="ant-modal-close-x">
<Icon className="ant-modal-close-icon" type={fullScreen ? 'shrink' : 'arrows-alt'} />
</span>
</button>
)}
</>
);
};
render() {
const { children, title, element, onCancel, onOk, maxmin, fullScreen: f, ...rest } = this.props;
const { footer } = rest;
const { visible, fullScreen } = this.state;
return (
<RouteContext.Consumer>
{({
// @ts-ignore
tabsView,
fixedHeader,
}) => (
<>
{element && React.cloneElement(element, { onClick: this.showModalHandler })}
{/* wrapped in span to stop event propagation when it's in a table
of which expandRowByClick is true */}
<span onClick={(e: React.MouseEvent<HTMLElement>) => e.stopPropagation()}>
<Modal
wrapClassName={`wm-modal-wrap${fullScreen ? ' wm-modal-wrap-fullscreen' : ''}${
footer === false ? ' wm-modal-wrap-nofooter' : ''
}`}
title={this.titleRender()}
visible={visible}
centered
onOk={this.okHandler}
onCancel={this.cancelHandler}
getContainer={
(tabsView &&
fixedHeader &&
(document.querySelector(
'.ant-tabs-tabpane.ant-tabs-tabpane-active',
) as HTMLElement)) ||
undefined
}
{...rest}
>
{children}
</Modal>
</span>
</>
)}
</RouteContext.Consumer>
);
}
}
export default InlineModal;