forked from ElemeFE/element-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialog.jsx
160 lines (140 loc) · 4.4 KB
/
Dialog.jsx
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
/* @flow */
import React from 'react';
import { Component, View, Transition, PropTypes } from '../../libs';
type State = {
bodyOverflow: string,
}
export default class Dialog extends Component {
state: State;
static defaultProps = {
visible: false,
title: '',
size: 'small',
top: '15%',
modal: true,
lockScroll: true,
closeOnClickModal: true,
closeOnPressEscape: true,
showClose: true
}
constructor(props: Object) {
super(props);
this.state = {
bodyOverflow: ''
}
}
componentWillReceiveProps(nextProps: Object): void {
if (this.willOpen(this.props, nextProps)){
if (this.props.lockScroll && document.body && document.body.style) {
if (!this.state.bodyOverflow) {
this.setState({
bodyOverflow: document.body.style.overflow
});
}
document.body.style.overflow = 'hidden';
}
}
if (this.willClose(this.props, nextProps) && this.props.lockScroll) {
if (this.props.modal && this.state.bodyOverflow !== 'hidden' && document.body && document.body.style) {
document.body.style.overflow = this.state.bodyOverflow;
}
}
}
componentDidUpdate(prevProps: Object): void {
if (this.willOpen(prevProps, this.props)){
this.refs.wrap.focus();
}
}
componentWillUnmount(): void {
if (this.props.lockScroll && document.body && document.body.style) {
document.body.style.removeProperty('overflow');
}
}
onKeyDown(e: SyntheticKeyboardEvent): void {
if (this.props.closeOnPressEscape && e.keyCode === 27) {
this.close(e);
}
}
handleWrapperClick(e: SyntheticEvent): void {
if (e.target instanceof HTMLDivElement) {
if (this.props.closeOnClickModal && e.target === e.currentTarget) {
this.close(e);
}
}
}
close(e: SyntheticEvent | SyntheticKeyboardEvent): void {
this.props.onCancel(e);
}
willOpen(prevProps: Object, nextProps: Object): boolean {
return (!prevProps.visible && nextProps.visible);
}
willClose(prevProps: Object, nextProps: Object): boolean {
return (prevProps.visible && !nextProps.visible);
}
render(): React.Element<any> {
const { visible, title, size, top, modal, customClass, showClose } = this.props;
return (
<div>
<Transition name="dialog-fade">
<View show={ visible }>
<div
ref="wrap"
style={{ zIndex: 1013 }}
className={this.classNames('el-dialog__wrapper')}
onClick={ e => this.handleWrapperClick(e) }
onKeyDown={ e => this.onKeyDown(e) }
>
<div
ref="dialog"
style={this.style(size === 'full' ? {} : { 'top': top })}
className={ this.className("el-dialog", `el-dialog--${ size }`, customClass) }
>
<div className="el-dialog__header">
<span className="el-dialog__title">{ title }</span>
{
showClose && (
<button type="button" className="el-dialog__headerbtn">
<i className="el-dialog__close el-icon el-icon-close" onClick={ e => this.close(e) }></i>
</button>
)
}
</div>
{ this.props.children }
</div>
</div>
</View>
</Transition>
{
modal && (
<View show={ visible }>
<div className="v-modal" style={{ zIndex: 1012 }}></div>
</View>
)
}
</div>
);
}
}
Dialog.propTypes = {
// 控制对话框是否可见
visible: PropTypes.bool.isRequired,
// 标题
title: PropTypes.string,
// 大小 (tiny/small/large/full)
size: PropTypes.string,
// top 值(仅在 size 不为 full 时有效)
top: PropTypes.string,
// 控制遮罩层展示
modal: PropTypes.bool,
// Dialog 的自定义类名
customClass: PropTypes.string,
// 是否在 Dialog 出现时将 body 滚动锁定
lockScroll: PropTypes.bool,
// 是否可以通过点击 modal 关闭 Dialog
closeOnClickModal: PropTypes.bool,
// 是否可以通过按下 ESC 关闭 Dialog
closeOnPressEscape: PropTypes.bool,
// 点击遮罩层或右上角叉或取消按钮的回调
onCancel: PropTypes.func.isRequired,
showClose: PropTypes.bool
};