-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (96 loc) · 2.58 KB
/
index.js
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
// 基于 React 事件的, 内聚的事件管理器
import React from 'react';
import Event from './Event';
export default class extends React.PureComponent {
static defaultProps = {
group: 'handle',
};
formValues = {};
formRefs = {};
formUpdates = {};
formDatas = {};
componentDidMount() {
const { onDidMount } = this.props;
if (typeof onDidMount === 'function') {
onDidMount(this.formDatas);
}
}
componentWillUnmount() {
this.formValues = {};
this.formRefs = {};
this.formUpdates = {};
this.formDatas = {};
const { onUnMount } = this.props;
if (typeof onUnMount === 'function') {
onUnMount(this.formDatas);
}
}
handleOnDidMound = params => {
this.handleOnEvent({ ...params, isFromDidMount: true });
};
update = (key, payload) => {
if (this.formUpdates[key]) {
this.formUpdates[key](payload);
}
};
handleOnEvent = ({ ref, value, handle, update, eventName, eventArgs, isFromDidMount }) => {
const { datas, onEvent } = this.props;
this.formValues[handle] = value;
this.formRefs[handle] = ref;
this.formUpdates[handle] = update;
this.formDatas = {
ref,
value,
handle,
eventArgs,
refs: this.formRefs,
values: this.formValues,
update: this.update,
eventName,
};
if (datas) {
for (const k in this.formDatas) {
datas[k] = this.formDatas[k];
}
}
if (!isFromDidMount && typeof onEvent === 'function') {
onEvent(this.formDatas);
}
};
regChild = ({ children }) => {
const { group } = this.props;
return React.Children.map(children, child => {
if (!child || !child.props) {
return child;
}
if (child.props.children && typeof child.props.children !== 'function') {
child = React.cloneElement(child, {
children: this.regChild({ children: child.props.children }),
});
}
if (child.props.SubManager) {
child = React.cloneElement(child, { SubManager: this.regChild });
}
if (child.props[group]) {
return (
<Event
defvalue={child.props.defvalue}
events={child.props.events}
handle={child.props[group]}
onDidMount={this.handleOnDidMound}
onEvent={this.handleOnEvent}
formValues={this.formValues}
formTargets={this.formRefs}
>
{child}
</Event>
);
}
return child;
});
};
render() {
const { children } = this.props;
return this.regChild({ children });
}
}