Skip to content

Commit 8cd3499

Browse files
committed
[docs] Add multiple forms guide
1 parent 2035cb6 commit 8cd3499

File tree

6 files changed

+112
-2
lines changed

6 files changed

+112
-2
lines changed

.changeset/soft-rivers-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"docs": minor
3+
---
4+
5+
Add multiple forms guide

apps/docs/src/content/docs/advanced/custom-form.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Note that you can simplify the above code if certain restrictions are met:
2424

2525
`Object.setPrototypeOf(options, defaults)`
2626

27-
- If you are sure you will not use getters as options, then use a spread operator instead of `Proxy`:
27+
- If you are sure you will not use getters as options, then use `Object.assign` instead of `Proxy`:
2828

29-
`{ ...defaults, ...options }`
29+
`Object.assign(defaults, options)`
3030

3131
:::
3232

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script lang="ts" generics="T, E">
2+
import type { HTMLFormAttributes } from "svelte/elements";
3+
import { FormContent, setFromContext, SubmitButton, type FormAPI, type FormContext } from '@sjsf/form';
4+
5+
interface Props extends HTMLFormAttributes {
6+
form: FormAPI<T, E>;
7+
ctx: FormContext;
8+
}
9+
10+
const { form, ctx, ...rest }: Props = $props();
11+
12+
setFromContext(ctx)
13+
</script>
14+
15+
<form use:form.enhance {...rest}>
16+
<!-- svelte-ignore ownership_invalid_binding -->
17+
<FormContent bind:value={form.formValue} />
18+
<SubmitButton />
19+
</form>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
import { ShadowHost } from '@/components/shadow';
3+
4+
import Form from './_multiple-forms.svelte'
5+
---
6+
7+
<ShadowHost client:only="svelte">
8+
<Form client:only="svelte" />
9+
</ShadowHost>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script lang="ts">
2+
import { createForm2, type Schema } from '@sjsf/form';
3+
import { createValidator } from '@sjsf/ajv8-validator';
4+
import { translation } from '@sjsf/form/translations/en';
5+
import { theme } from '@sjsf/form/basic-theme';
6+
7+
import Form from './_multiple-forms-form.svelte';
8+
9+
const schema: Schema = {
10+
type: "string"
11+
}
12+
13+
const common = {
14+
...theme,
15+
schema,
16+
translation,
17+
validator: createValidator(),
18+
onSubmit: (v: string) => window.alert(v)
19+
}
20+
21+
const [form1, form1Ctx] = createForm2({
22+
...common,
23+
initialValue: "foo",
24+
idPrefix: "form1",
25+
})
26+
27+
const [form2, form2Ctx] = createForm2({
28+
...common,
29+
initialValue: "bar",
30+
idPrefix: "form2",
31+
})
32+
</script>
33+
34+
<div style="display: flex; gap: 1rem;">
35+
<Form style="display: flex; flex-direction: column; gap: 1rem; flex-grow: 1;" form={form1} ctx={form1Ctx} />
36+
<Form style="display: flex; flex-direction: column; gap: 1rem; flex-grow: 1;" form={form2} ctx={form2Ctx} />
37+
</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Multiple forms
3+
sidebar:
4+
order: 6
5+
---
6+
7+
import { Code, Tabs, TabItem, Card } from '@astrojs/starlight/components';
8+
9+
import { DEFAULT_ID_PREFIX } from '@sjsf/form'
10+
11+
import Form from './_multiple-forms.astro';
12+
import formCode from './_multiple-forms.svelte?raw';
13+
import formComponentCode from './_multiple-forms-form.svelte?raw';
14+
15+
There are a few rules to follow if you want to use multiple forms
16+
17+
## On the same page
18+
19+
You should provide custom `idPrefix` to avoid id collisions in the DOM
20+
21+
The default value of `idPrefix` is <code>{DEFAULT_ID_PREFIX}</code>.
22+
23+
## In one component
24+
25+
Because `useForm2` internally calls `setFormContext`, you can't create multiple forms in one component with it.
26+
27+
But if you really want to create multiple forms in one component, look at `createForm2`.
28+
29+
<Tabs>
30+
<TabItem label="_multiple-forms.svelte" >
31+
<Code code={formCode} lang='svelte' />
32+
</TabItem>
33+
<TabItem label="_multiple-forms-form.svelte">
34+
<Code code={formComponentCode} lang='svelte' />
35+
</TabItem>
36+
</Tabs>
37+
38+
<Card>
39+
<Form />
40+
</Card>

0 commit comments

Comments
 (0)