/
nested.tsx
196 lines (184 loc) · 6.42 KB
/
nested.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
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
186
187
188
189
190
191
192
193
194
195
196
import {
useForm,
remove,
insert,
move,
swap,
replace,
} from '@modular-forms/preact';
import { FormHeader, FormFooter, TextInput, ColorButton } from '../components';
type NestedForm = {
items: {
label: string;
options: string[];
}[];
};
const initialValues = {
items: [
{
label: 'Item 1',
options: ['Option 1', 'Option 2'],
},
{
label: 'Item 2',
options: ['Option 1', 'Option 2'],
},
],
};
export default function NestedPage() {
// Create nested form
const [nestedForm, { Form, Field, FieldArray }] = useForm<NestedForm>({
initialValues,
});
return (
<Form
class="space-y-12 md:space-y-14 lg:space-y-16"
onSubmit={(values) => console.log(values)}
>
<FormHeader of={nestedForm} heading="Nested form" />
<div class="space-y-5 px-8 lg:px-10">
<FieldArray name="items">
{(fieldArray) => (
<>
{fieldArray.items.value.map((item, index) => (
<div
key={item}
class="flex-1 space-y-5 rounded-2xl border-2 border-slate-200 bg-slate-100/25 py-6 hover:border-slate-300 dark:border-slate-800 dark:bg-slate-800/10 dark:hover:border-slate-700"
>
<div class="flex space-x-5 px-6">
<Field name={`${fieldArray.name}.${index}.label`}>
{(field, props) => (
<TextInput
{...props}
class="flex-1 !p-0"
value={field.value}
error={field.error}
type="text"
placeholder="Enter item"
/>
)}
</Field>
<ColorButton
color="red"
label="Delete"
width="auto"
onClick={() =>
remove(nestedForm, fieldArray.name, { at: index })
}
/>
</div>
<div
class="border-t-2 border-t-slate-200 dark:border-t-slate-800"
role="separator"
/>
<FieldArray name={`${fieldArray.name}.${index}.options`}>
{(fieldArray) => (
<div class="space-y-5 px-6">
{fieldArray.items.value.map((option, index) => (
<div key={option} class="flex space-x-5">
<Field name={`${fieldArray.name}.${index}`}>
{(field, props) => (
<TextInput
{...props}
class="flex-1 !p-0"
value={field.value}
error={field.error}
type="text"
placeholder="Enter option"
/>
)}
</Field>
<ColorButton
color="red"
label="Delete"
width="auto"
onClick={() =>
remove(nestedForm, fieldArray.name, {
at: index,
})
}
/>
</div>
))}
<div class="flex flex-wrap gap-4">
<ColorButton
color="green"
label="Add option"
onClick={() =>
insert(nestedForm, fieldArray.name, {
value: '',
})
}
/>
<ColorButton
color="yellow"
label="Move first to end"
onClick={() =>
move(nestedForm, fieldArray.name, {
from: 0,
to: fieldArray.items.value.length - 1,
})
}
/>
<ColorButton
color="purple"
label="Swap first two"
onClick={() =>
swap(nestedForm, fieldArray.name, {
at: 0,
and: 1,
})
}
/>
</div>
</div>
)}
</FieldArray>
</div>
))}
<div class="flex flex-wrap gap-4">
<ColorButton
color="green"
label="Add item"
onClick={() =>
insert(nestedForm, fieldArray.name, {
value: { label: '', options: [''] },
})
}
/>
<ColorButton
color="yellow"
label="Move first to end"
onClick={() =>
move(nestedForm, fieldArray.name, {
from: 0,
to: fieldArray.items.value.length - 1,
})
}
/>
<ColorButton
color="purple"
label="Swap first two"
onClick={() =>
swap(nestedForm, fieldArray.name, { at: 0, and: 1 })
}
/>
<ColorButton
color="blue"
label="Replace first"
onClick={() =>
replace(nestedForm, fieldArray.name, {
at: 0,
value: { label: '', options: [''] },
})
}
/>
</div>
</>
)}
</FieldArray>
</div>
<FormFooter of={nestedForm} />
</Form>
);
}