Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/svelte-form/src/createForm.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { FormApi } from '@tanstack/form-core'
import { useSelector } from '@tanstack/svelte-store'
import { onMount } from 'svelte'
import { getContext, hasContext, onMount, setContext } from 'svelte'

const FORM_ID_CTX = Symbol()

function nextFormId(): string {
try {
if (!hasContext(FORM_ID_CTX)) setContext(FORM_ID_CTX, { n: 0 })
const ctx = getContext<{ n: number }>(FORM_ID_CTX)
return `svelte-form-${++ctx.n}`
} catch {
// Outside component context (unit tests, etc.)
return `svelte-form-${Math.random().toString(36).slice(2, 9)}`
}
}
import Field from './Field.svelte'
import FormGroup from './FormGroup.svelte'
import Subscribe from './Subscribe.svelte'
Expand Down Expand Up @@ -310,7 +323,7 @@ export function createForm<
TFormOnDynamicAsync,
TFormOnServer,
TSubmitMeta
>(options)
>({ ...options, formId: options?.formId ?? nextFormId() } as typeof options)
const extendedApi: typeof api &
SvelteFormApi<
TParentData,
Expand Down