-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathPopupMixin.tsx
More file actions
159 lines (143 loc) · 4.2 KB
/
PopupMixin.tsx
File metadata and controls
159 lines (143 loc) · 4.2 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
150
151
152
153
154
155
156
157
158
159
import * as React from 'react';
import { IPopupPickerProps } from './PopupPickerTypes';
export default function PopupMixin(getModal, platformProps) {
return class extends React.Component<IPopupPickerProps, any> {
static defaultProps = {
onVisibleChange(_) { },
okText: 'Ok',
dismissText: 'Dismiss',
title: '',
onOk(_) { },
onDismiss() { },
...platformProps,
};
picker: any;
constructor(props) {
super(props);
this.state = {
pickerValue: 'value' in this.props ? this.props.value : null,
visible: this.props.visible || false,
};
}
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
// value 有实际的变化才更新
if (!Array.isArray(nextProps.value) || !Array.isArray(this.props.value)) {
// 不是数组的话,跟原来的逻辑一样,实际上 Picker 不会走到这个逻辑,因为 Picker 在使用的时候必须传数组
this.setState({
pickerValue: nextProps.value,
});
} else if (nextProps.value.length !== this.props.value.length || nextProps.value.some(
(item, index) => item !== this.props.value[index]
)) {
this.setState({
pickerValue: nextProps.value,
});
}
}
if ('visible' in nextProps) {
this.setVisibleState(nextProps.visible);
}
}
onPickerChange = (pickerValue) => {
if (this.state.pickerValue !== pickerValue) {
this.setState({
pickerValue,
});
const { picker, pickerValueChangeProp } = this.props;
if (picker && picker.props[pickerValueChangeProp!]) {
picker.props[pickerValueChangeProp!](pickerValue);
}
}
}
saveRef = (picker) => {
this.picker = picker;
}
setVisibleState(visible) {
this.setState({
visible,
});
if (!visible) {
this.setState({
pickerValue: null,
});
}
}
fireVisibleChange(visible) {
if (this.state.visible !== visible) {
if (!('visible' in this.props)) {
this.setVisibleState(visible);
}
this.props.onVisibleChange!(visible);
}
}
getRender() {
const props = this.props;
const children = props.children;
if (!children) {
return getModal(props, this.state.visible, {
getContent: this.getContent,
onOk: this.onOk,
hide: this.hide,
onDismiss: this.onDismiss,
});
}
const { WrapComponent, disabled } = this.props;
const child = children;
const newChildProps = {};
if (!disabled) {
newChildProps[props.triggerType!] = this.onTriggerClick;
}
return (
<WrapComponent style={props.wrapStyle}>
{React.cloneElement(child as any, newChildProps)}
{
getModal(props, this.state.visible, {
getContent: this.getContent,
onOk: this.onOk,
hide: this.hide,
onDismiss: this.onDismiss,
})
}
</WrapComponent>
);
}
onTriggerClick = (e) => {
const child: any = this.props.children;
const childProps = child.props || {};
if (childProps[this.props.triggerType!]) {
childProps[this.props.triggerType!](e);
}
this.fireVisibleChange(!this.state.visible);
}
onOk = () => {
this.props.onOk!(this.picker && this.picker.getValue());
this.fireVisibleChange(false);
}
getContent = () => {
if (this.props.picker) {
let { pickerValue } = this.state;
if (pickerValue === null) {
pickerValue = this.props.value;
}
return React.cloneElement(this.props.picker, ({
[this.props.pickerValueProp!]: pickerValue,
[this.props.pickerValueChangeProp!]: this.onPickerChange,
ref: this.saveRef,
}));
} else {
return this.props.content;
}
}
onDismiss = () => {
this.props.onDismiss!();
this.fireVisibleChange(false);
}
hide = () => {
this.fireVisibleChange(false);
}
render() {
return this.getRender();
}
};
}