forked from TanStack/form
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayOfNestedForms.js
More file actions
135 lines (118 loc) · 3.71 KB
/
ArrayOfNestedForms.js
File metadata and controls
135 lines (118 loc) · 3.71 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
/* ------------- 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 --------------------*/
const NestedFormArrayCode = () => {
const code = `
import { Form, Text, NestedForm } from 'react-form';
const Friend = ({ i }) => (
<NestedForm field={['friends', i]} key={\`nested-friend-\${i}\`}>
<Form>
{ formApi => (
<div>
<h2>Friend</h2>
<label htmlFor={\`nested-friend-first-\${i}\`}>First name</label>
<Text field="firstName" id={\`nested-friend-first-\${i}\`} />
<label htmlFor={\`nested-friend-last-\${i}\`}>Last name</label>
<Text field="lastName" id={\`nested-friend-last-\${i}\`} />
</div>
)}
</Form>
</NestedForm>
);
class FormWithArrayOfNestedForms extends Component {
constructor( props ) {
super( props );
this.state = {};
}
render() {
return (
<div>
<Form
onSubmit={submittedValues => this.setState( { submittedValues } )}>
{ formApi => (
<div>
<form onSubmit={formApi.submitForm} id="form3">
<Friend i={0} />
<Friend i={1} />
<Friend i={2} />
<button type="submit" className="mb-4 btn btn-primary">Submit</button>
</form>
</div>
)}
</Form>
</div>
);
}
}
`;
return (
<div>
<h5>Source Code:</h5>
<Code type="html">
{code}
</Code>
</div>
);
};
const Friend = ({ i }) => (
<NestedForm field={['friends', i]} key={`nested-friend-${i}`}>
<Form>
{ formApi => (
<div>
<h2>Friend</h2>
<label htmlFor={`nested-friend-first-${i}`}>First name</label>
<Text field="firstName" id={`nested-friend-first-${i}`} />
<label htmlFor={`nested-friend-last-${i}`}>Last name</label>
<Text field="lastName" id={`nested-friend-last-${i}`} />
</div>
)}
</Form>
</NestedForm>
);
class ArrayOfNestedForms extends Component {
constructor( props ) {
super( props );
this.state = {};
}
render() {
return (
<div>
<h2 className="mb-4">Array of nested forms</h2>
<p>
React Form also allows you to create an array of nested foms! How cool is that!
</p>
<Form
onSubmit={submittedValues => this.setState( { submittedValues } )}>
{ formApi => (
<div>
<form onSubmit={formApi.submitForm} id="form3">
<Friend i={0} />
<Friend i={1} />
<Friend i={2} />
<button type="submit" className="mb-4 btn btn-primary">Submit</button>
</form>
<br />
<Data title="Values" reference="formApi.values" data={formApi.values} />
<Data title="Touched" reference="formApi.touched" data={formApi.touched} />
<Data title="Submission attempts" reference="formApi.submits" data={formApi.submits} />
<Data title="Submitted" reference="formApi.submitted" data={formApi.submitted} />
<Data title="Submitted values" reference="onSubmit={submittedValues => this.setState( { submittedValues } )}" data={this.state.submittedValues} />
</div>
)}
</Form>
<br />
<NestedFormArrayCode />
</div>
);
}
}
export default ArrayOfNestedForms;