-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nested field array playground to website
- Loading branch information
1 parent
f6d4c9f
commit 097d704
Showing
2 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
import { | ||
createForm, | ||
Field, | ||
FieldArray, | ||
Form, | ||
insert, | ||
move, | ||
remove, | ||
replace, | ||
swap, | ||
} from '@modular-forms/solid'; | ||
import { For, onMount } from 'solid-js'; | ||
import { | ||
FormHeader, | ||
FormFooter, | ||
TextInput, | ||
Title, | ||
ColorButton, | ||
} from '~/components'; | ||
import { useForm } from '~/contexts'; | ||
|
||
type NestedForm = { | ||
items: { | ||
label: string; | ||
options: string[]; | ||
}[]; | ||
}; | ||
|
||
const initialValues = { | ||
items: [ | ||
{ | ||
label: 'Item 1', | ||
options: ['Option 1'], | ||
}, | ||
{ | ||
label: 'Item 2', | ||
options: ['Option 1', 'Option 2'], | ||
}, | ||
], | ||
}; | ||
|
||
export default function NestedPage() { | ||
// Create nested form | ||
const nestedForm = createForm<NestedForm>({ initialValues }); | ||
|
||
// Set nested form in form context | ||
onMount(() => useForm().set(nestedForm)); | ||
|
||
return ( | ||
<> | ||
<Title>Nested form</Title> | ||
|
||
<Form | ||
class="space-y-12 md:space-y-14 lg:space-y-16" | ||
of={nestedForm} | ||
onSubmit={(values) => alert(JSON.stringify(values, null, 4))} | ||
> | ||
<FormHeader of={nestedForm} heading="Nested form" /> | ||
|
||
<div class="space-y-5 px-8 lg:px-10"> | ||
<FieldArray of={nestedForm} name="items"> | ||
{(fieldArray) => ( | ||
<> | ||
<For each={fieldArray.items}> | ||
{(_, index) => ( | ||
<div 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 | ||
of={nestedForm} | ||
name={`${fieldArray.name}.${index()}.label`} | ||
> | ||
{(field) => ( | ||
<TextInput | ||
{...field.props} | ||
value={field.value} | ||
error={field.error} | ||
type="text" | ||
class="flex-1" | ||
placeholder="Enter item" | ||
padding="none" | ||
/> | ||
)} | ||
</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 | ||
of={nestedForm} | ||
name={`${fieldArray.name}.${index()}.options`} | ||
> | ||
{(fieldArray) => ( | ||
<div class="space-y-5 px-6"> | ||
<For each={fieldArray.items}> | ||
{(_, index) => ( | ||
<div class="flex space-x-5"> | ||
<Field | ||
of={nestedForm} | ||
name={`${fieldArray.name}.${index()}`} | ||
> | ||
{(field) => ( | ||
<TextInput | ||
{...field.props} | ||
value={field.value} | ||
error={field.error} | ||
class="flex-1" | ||
type="text" | ||
placeholder="Enter option" | ||
padding="none" | ||
/> | ||
)} | ||
</Field> | ||
|
||
<ColorButton | ||
color="red" | ||
label="Delete" | ||
width="auto" | ||
onClick={() => | ||
remove(nestedForm, fieldArray.name, { | ||
at: index(), | ||
}) | ||
} | ||
/> | ||
</div> | ||
)} | ||
</For> | ||
|
||
<div class="flex flex-wrap gap-4"> | ||
<ColorButton | ||
color="green" | ||
label="Add option" | ||
onClick={() => | ||
insert(nestedForm, fieldArray.name) | ||
} | ||
/> | ||
<ColorButton | ||
color="yellow" | ||
label="Move first to end" | ||
onClick={() => | ||
move(nestedForm, fieldArray.name, { | ||
from: 0, | ||
to: fieldArray.length - 1, | ||
}) | ||
} | ||
/> | ||
<ColorButton | ||
color="purple" | ||
label="Swap first two" | ||
onClick={() => | ||
swap(nestedForm, fieldArray.name, { | ||
at: 0, | ||
and: 1, | ||
}) | ||
} | ||
/> | ||
</div> | ||
</div> | ||
)} | ||
</FieldArray> | ||
</div> | ||
)} | ||
</For> | ||
|
||
<div class="flex flex-wrap gap-4"> | ||
<ColorButton | ||
color="green" | ||
label="Add item" | ||
onClick={() => insert(nestedForm, fieldArray.name)} | ||
/> | ||
<ColorButton | ||
color="yellow" | ||
label="Move first to end" | ||
onClick={() => | ||
move(nestedForm, fieldArray.name, { | ||
from: 0, | ||
to: fieldArray.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 }) | ||
} | ||
/> | ||
</div> | ||
</> | ||
)} | ||
</FieldArray> | ||
</div> | ||
|
||
<FormFooter of={nestedForm} /> | ||
</Form> | ||
</> | ||
); | ||
} |