-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
0 deletions.
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
59 changes: 59 additions & 0 deletions
59
sites/docs/src/lib/registry/default/example/input-otp-form.svelte
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,59 @@ | ||
<script lang="ts" module> | ||
import { z } from "zod"; | ||
export const formSchema = z.object({ | ||
pin: z.string().min(6, { | ||
message: "Your one-time password must be at least 6 characters.", | ||
}), | ||
}); | ||
export type FormSchema = typeof formSchema; | ||
</script> | ||
|
||
<script lang="ts"> | ||
import SuperDebug, { type Infer, type SuperValidated, superForm } from "sveltekit-superforms"; | ||
import { zodClient } from "sveltekit-superforms/adapters"; | ||
import { toast } from "svelte-sonner"; | ||
import { browser } from "$app/environment"; | ||
import * as InputOTP from "$lib/registry/default/ui/input-otp/index.js"; | ||
import * as Form from "$lib/registry/default/ui/form/index.js"; | ||
import { page } from "$app/stores"; | ||
let { form: data = $page.data.inputOtp }: { form: SuperValidated<Infer<FormSchema>> } = | ||
$props(); | ||
const form = superForm(data, { | ||
validators: zodClient(formSchema), | ||
onUpdated: ({ form: f }) => { | ||
if (f.valid) { | ||
toast.success(`You submitted ${JSON.stringify(f.data, null, 2)}`); | ||
} else { | ||
toast.error("Please fix the errors in the form."); | ||
} | ||
}, | ||
}); | ||
const { form: formData, enhance } = form; | ||
</script> | ||
|
||
<form action="/?/inputOtp" method="POST" class="w-2/3 space-y-6" use:enhance> | ||
<Form.Field {form} name="pin"> | ||
<Form.Control> | ||
{#snippet children({ props })} | ||
<InputOTP.Root maxlength={6} {...props} bind:value={$formData.pin}> | ||
{#snippet children({ cells })} | ||
<InputOTP.Group> | ||
{#each cells as cell} | ||
<InputOTP.Slot {cell} /> | ||
{/each} | ||
</InputOTP.Group> | ||
{/snippet} | ||
</InputOTP.Root> | ||
{/snippet} | ||
</Form.Control> | ||
<Form.Description>Please enter the one-time password sent to your phone.</Form.Description> | ||
<Form.FieldErrors /> | ||
</Form.Field> | ||
<Form.Button>Submit</Form.Button> | ||
{#if browser} | ||
<SuperDebug data={$formData} /> | ||
{/if} | ||
</form> |
59 changes: 59 additions & 0 deletions
59
sites/docs/src/lib/registry/new-york/example/input-otp-form.svelte
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,59 @@ | ||
<script lang="ts" module> | ||
import { z } from "zod"; | ||
export const formSchema = z.object({ | ||
pin: z.string().min(6, { | ||
message: "Your one-time password must be at least 6 characters.", | ||
}), | ||
}); | ||
export type FormSchema = typeof formSchema; | ||
</script> | ||
|
||
<script lang="ts"> | ||
import SuperDebug, { type Infer, type SuperValidated, superForm } from "sveltekit-superforms"; | ||
import { zodClient } from "sveltekit-superforms/adapters"; | ||
import { toast } from "svelte-sonner"; | ||
import { browser } from "$app/environment"; | ||
import * as InputOTP from "$lib/registry/new-york/ui/input-otp/index.js"; | ||
import * as Form from "$lib/registry/new-york/ui/form/index.js"; | ||
import { page } from "$app/stores"; | ||
let { form: data = $page.data.inputOtp }: { form: SuperValidated<Infer<FormSchema>> } = | ||
$props(); | ||
const form = superForm(data, { | ||
validators: zodClient(formSchema), | ||
onUpdated: ({ form: f }) => { | ||
if (f.valid) { | ||
toast.success(`You submitted ${JSON.stringify(f.data, null, 2)}`); | ||
} else { | ||
toast.error("Please fix the errors in the form."); | ||
} | ||
}, | ||
}); | ||
const { form: formData, enhance } = form; | ||
</script> | ||
|
||
<form action="/?/inputOtp" method="POST" class="w-2/3 space-y-6" use:enhance> | ||
<Form.Field {form} name="pin"> | ||
<Form.Control> | ||
{#snippet children({ props })} | ||
<InputOTP.Root maxlength={6} {...props} bind:value={$formData.pin}> | ||
{#snippet children({ cells })} | ||
<InputOTP.Group> | ||
{#each cells as cell} | ||
<InputOTP.Slot {cell} /> | ||
{/each} | ||
</InputOTP.Group> | ||
{/snippet} | ||
</InputOTP.Root> | ||
{/snippet} | ||
</Form.Control> | ||
<Form.Description>Please enter the one-time password sent to your phone.</Form.Description> | ||
<Form.FieldErrors /> | ||
</Form.Field> | ||
<Form.Button>Submit</Form.Button> | ||
{#if browser} | ||
<SuperDebug data={$formData} /> | ||
{/if} | ||
</form> |
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