-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
96 lines (88 loc) · 2.44 KB
/
index.tsx
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
import * as React from "react";
import { render } from "react-dom";
import {
FormBuilder,
FieldControl,
FieldGroup,
AbstractControl
} from "react-reactive-form";
import Values from "./Values";
import styles from "./styles";
import {
TextInput,
Checkbox,
GenderRadio,
SelectBox,
TextArea
} from "./components";
class SimpleForm extends React.Component {
// Create a group of form controls with default values.
myForm = FormBuilder.group({
first_name: "",
last_name: "",
gender: "male",
nationality: "",
terms: false,
notes: ""
});
handleSubmit(e: React.MouseEvent<HTMLButtonElement>) {
e.preventDefault();
alert(`You submitted \n ${JSON.stringify(this.myForm.value, null, 2)}`);
}
handleReset() {
this.myForm.reset();
}
render() {
return (
<div style={styles.main}>
<h2>Simple Form</h2>
<FieldGroup
control={this.myForm}
render={({ pristine, value }: AbstractControl) => (
<form onSubmit={() => this.handleSubmit}>
<FieldControl
name="first_name"
render={TextInput}
// Use meta to add some extra props
meta={{
label: "First Name",
placeholder: "Enter first name"
}}
/>
<FieldControl
name="last_name"
meta={{
label: "Last Name",
placeholder: "Enter last name"
}}
render={TextInput}
/>
<FieldControl name="gender" render={GenderRadio} />
<FieldControl name="nationality" render={SelectBox} />
<FieldControl name="notes" render={TextArea} />
<FieldControl name="terms" render={Checkbox} />
<div>
<button
disabled={pristine}
style={styles.button}
onClick={e => this.handleSubmit(e)}
>
Submit
</button>
<button
type="button"
style={styles.button}
onClick={() => this.handleReset()}
>
Reset
</button>
</div>
<Values value={value} />
</form>
)}
/>
</div>
);
}
}
render(<SimpleForm />, document.getElementById("root"));