-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathForm.js
118 lines (102 loc) · 2.85 KB
/
Form.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
'use strict';
var React = require('react-native');
var {
StyleSheet,
ScrollView,
} = React;
var Form = React.createClass({
componentWillMount: function() {
// 引用输入的子组件
this.inputs = [];
},
// 注册子组件,在子组件的componentWillMount()中调用
registerInput: function (component) {
if (this.inputs.indexOf(component) === -1) {
this.inputs.push(component);
}
},
// 注销子组件,在子组件的componentWillUnmount()中调用
unregisterInput: function (component) {
var componentPos = this.inputs.indexOf(component);
if (componentPos !== -1) {
this.inputs = this.inputs.slice(0, componentPos)
.concat(this.inputs.slice(componentPos + 1));
}
},
scrollTo: function(...args) {
this.refs.scrollView.scrollTo(...args);
},
/**
* 滚动到指定输入框位置
* 参考 https://github.com/facebook/react-native/issues/3195#issuecomment-146518331
* @param input: the input component
* @param extraHeight: takes an extra height in consideration. like tabbar height is 49
*/
scrollToFocusedInput: function (input, extraHeight = 49) {
this.refs.scrollView.scrollResponderScrollNativeHandleToKeyboard(
React.findNodeHandle(input), extraHeight, true
)
},
// 获得所有字段的标题
getTitles(){
return this.inputs.reduce((titles, input) => {
titles[input.props.name] = input.props.title;
return titles;
}, {});
},
// 获得所有字段值
getValues(){
return this.inputs.reduce((values, input) => {
values[input.props.name] = input.state.value;
return values;
}, {})
},
// 获得所有字段值
getValueQueryString(){
var result = '';
for(var i in this.inputs){
var pref = (i == 0 ? '' : '&');
var {props, state} = this.inputs[i];
result += (pref + props.name + '=' + state.value);
}
return result;
},
// 校验所有字段
validate(){
var result = true;
for(var i in this.inputs){
result = this.inputs[i].validate() && result; // 不管validate()在此前是否调用过, 在此时都要重新调用一次
}
return result;
},
// 获得所有字段的校验失败的消息
getErrorMessages(){
return this.inputs.reduce((msgs, input) => {
if(input.state.errorMessage){
msgs.push(input.state.errorMessage);
}
return msgs;
}, [])
},
// 封装子组件
childrenWithProps() {
return React.Children.map(this.props.children, (child) => {
if (!!child) {
return React.cloneElement(child, {
form: this, // 传递form
navigator: this.props.navigator, // 传递导航栏
});
}
});
},
render(){
return (
<ScrollView
{...this.props}
ref='scrollView'>
{this.childrenWithProps()}
</ScrollView>
)
}
})
module.exports = Form;