forked from TanStack/form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedFormExample.js
More file actions
185 lines (164 loc) · 5.23 KB
/
NestedFormExample.js
File metadata and controls
185 lines (164 loc) · 5.23 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* ------------- Imports -------------- */
import React, { Component } from 'react';
/* ------------- Form Library Imports -------------- */
import {
Form,
Text,
NestedForm
} from '../src/index';
/* ---------------- Other Imports ------------------ */
import Data from './Data';
import Code from './Code';
/* ------------------ Form Stuff --------------------*/
let nestedFormApi = {};
const Questions = () => (
<NestedForm field="questions">
<Form>
{ ( formApi ) => {
nestedFormApi = formApi;
return (
<div>
<label htmlFor="color">Whats your favorite color?</label>
<Text field="color" id="color" />
<label htmlFor="food">Whats your favorite food?</label>
<Text field="food" id="food" />
<label htmlFor="car">Whats type of car do you drive?</label>
<Text field="car" id="car" />
</div>
);
}}
</Form>
</NestedForm>
);
const FormContent = ({ formApi, submittedValues }) => {
return (
<div>
<form onSubmit={formApi.submitForm} id="form4">
<label htmlFor="firstName3">First name</label>
<Text field="firstName" id="firstName3" />
<Questions />
<button type="submit" className="mb-4 btn btn-primary">Submit</button>
</form>
<br />
<div className="row mb-4">
<div className="col-sm-6">
<h4>Full form properties</h4>
</div>
<div className="col-sm-6">
<h4>Nested form properties</h4>
</div>
</div>
<div className="row">
<div className="col-sm-6">
<Data title="Values" reference="formApi.values" data={formApi.values} />
</div>
<div className="col-sm-6">
<Data title="Values" reference="nestedFormsApi.values" data={nestedFormApi.values} />
</div>
</div>
<div className="row">
<div className="col-sm-6">
<Data title="Touched" reference="formApi.touched" data={formApi.touched} />
</div>
<div className="col-sm-6">
<Data title="Touched" reference="nestedFormsApi.touched" data={nestedFormApi.touched} />
</div>
</div>
<div className="row">
<div className="col-sm-6">
<Data title="Submission attempts" reference="formApi.submits" data={formApi.submits} />
</div>
<div className="col-sm-6">
<Data title="Submission attempts" reference="nestedFormsApi.submits" data={nestedFormApi.submits} />
</div>
</div>
<div className="row">
<div className="col-sm-6">
<Data title="Submitted" reference="formApi.submitted" data={formApi.submitted} />
</div>
<div className="col-sm-6">
<Data title="Submitted" reference="nestedFormsApi.submitted" data={nestedFormApi.submitted} />
</div>
</div>
<Data title="Submitted values" reference="onSubmit={submittedValues => this.setState( { submittedValues } )}" data={submittedValues} />
</div>
);
};
const NestedFormCode = () => {
const code = `
import { Form, Text, NestedForm } from 'react-form';
const Questions = () => (
<NestedForm field="questions">
<Form>
{ formApi => (
<div>
<label htmlFor="color">Whats your favorite color?</label>
<Text field="color" id="color" />
<label htmlFor="food">Whats your favorite food?</label>
<Text field="food" id="food" />
<label htmlFor="car">Whats type of car do you drive?</label>
<Text field="car" id="car" />
</div>
)}
</Form>
</NestedForm>
);
class NestedFormExample extends Component {
constructor( props ) {
super( props );
this.state = {};
}
render() {
return (
<div>
<Form onSubmit={submittedValues => this.setState( { submittedValues } )}>
{ formApi => (
<form onSubmit={formApi.submitForm} id="form4">
<label htmlFor="firstName3">First name</label>
<Text field="firstName" id="firstName3" />
<Questions />
<button type="submit" className="mb-4 btn btn-primary">Submit</button>
</form>
)}
</Form>
</div>
);
}
}
`;
return (
<div>
<h5>Source Code:</h5>
<Code type="html">
{code}
</Code>
</div>
);
};
class NestedFormExample extends Component {
constructor( props ) {
super( props );
this.state = {};
}
render() {
return (
<div>
<h2 className="mb-4">Nested Forms</h2>
<p>
You can also choose to create nested forms. This can become very
usefull for complex forms. Nested forms should always have one child,
a <code><Form></code> component. <code><Form></code>s within
a <code><NestedForm></code> have all the same properties as a normal
<code><Form></code>
</p>
<Form
onSubmit={submittedValues => this.setState( { submittedValues } )}>
<FormContent submittedValues={this.state.submittedValues} />
</Form>
<br />
<NestedFormCode />
</div>
);
}
}
export default NestedFormExample;