-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathForm.js
64 lines (54 loc) · 1.35 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
import React from 'react';
let { View, TextInput,
StyleSheet,
ScrollView,
Text,
SliderIOS,
TouchableWithoutFeedback
} = require('react-native');
// import {Separator} from './fields/Separator';
export class Form extends React.Component{
constructor(props){
super();
this.values = {};
}
handleFieldFocused(event, inputHandle){
this.props.onFocus && this.props.onFocus(event, inputHandle);
}
handleFieldChange(field_ref, value){
this.values[field_ref] = value;
this.props.onChange && this.props.onChange(this.values);
}
getValues(){
return this.values;
}
underscoreToSpaced(str){
var words = str.split('_');
var res=[];
words.map(function(word, i){
res.push(word.charAt(0).toUpperCase() + word.slice(1));
})
return res.join(' ');
}
render(){
let wrappedChildren = [];
React.Children.map(this.props.children, (child, i)=> {
if (!child) {
return;
}
wrappedChildren.push(React.cloneElement(child, {
key: child.ref || child.type+i,
fieldRef : child.ref,
ref: child.ref,
onFocus:this.handleFieldFocused.bind(this),
onChange:this.handleFieldChange.bind(this, child.ref)
}
));
}, this);
return (
<View style={this.props.style}>
{wrappedChildren}
</View>
);
}
}