Skip to content

Commit

Permalink
Add nested field array playground to website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Nov 8, 2022
1 parent f6d4c9f commit 097d704
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/website/src/routes/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function PlaygroundLayout() {
<FormProvider>
<main class="flex w-full max-w-screen-xl flex-1 flex-col self-center lg:flex-row lg:space-x-9">
<div class="min-full flex-1 space-y-12 py-12 md:space-y-14 md:py-20 lg:space-y-16 lg:py-32">
<Tabs items={['Login', 'Payment', 'Todos', 'Special']} />
<Tabs items={['Login', 'Payment', 'Todos', 'Special', 'Nested']} />
<Outlet />
</div>
<SideBar>
Expand Down
216 changes: 216 additions & 0 deletions packages/website/src/routes/playground/nested.tsx
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>
</>
);
}

0 comments on commit 097d704

Please sign in to comment.