-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTcomb.js
112 lines (104 loc) · 2.29 KB
/
Tcomb.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {
View,
TouchableOpacity,
Text,
ToastAndroid,
StyleSheet,
} from 'react-native';
import t from 'tcomb-form-native';
let Form = t.form.Form;
class Tcomb extends Component {
constructor(props) {
super(props);
this.state = {
value: {
name: 'First',
surname: 'Last',
age: 25,
rememberMe: true,
},
};
this.submitForm = this.submitForm.bind(this)
}
submitForm() {
var value = this.refs.personForm.getValue();
if (value) {
// if validation fails, value will be null
ToastAndroid.show('Validation successful', ToastAndroid.SHORT);
} else {
ToastAndroid.show('Please fix errors', ToastAndroid.SHORT);
}
}
render() {
let PersonModel = t.struct({
name: t.String, // a required string
surname: t.maybe(t.String), // an optional string
age: t.Number, // a required number
rememberMe: t.Boolean, // a boolean
gender: t.enums({M: 'Male', F: 'Female'}, 'gender'),
});
let options = {
fields: {
name: {
label: 'First Name',
help: 'Must be less than 20 characters',
},
age: {
editable: false,
},
rememberMe: {
disabled: true,
},
gender: {
disabled: true,
},
},
};
return (
<View>
<Form
ref='personForm'
type={PersonModel}
options={options}
value={this.state.value}
// onChange={{}}
/>
<TouchableOpacity style={styles.button} onPress={this.submitForm}>
<Text style={styles.buttonText}>Submit</Text>
</TouchableOpacity>
</View>
);
}
}
var styles = StyleSheet.create({
container: {
justifyContent: 'center',
marginTop: 50,
padding: 20,
backgroundColor: '#ffffff',
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center',
},
button: {
height: 36,
backgroundColor: '#48BBEC',
borderColor: '#48BBEC',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center',
},
});
export default Tcomb;