Open History to see the last commit
1import { LitElement, html, nothing } from 'lit'
2import { customElement } from 'lit/decorators.js'
3import { type } from 'arktype'
4import { Schema as S } from 'effect'
5import * as v from 'valibot'
6import { z } from 'zod'
7import { TanStackFormController } from '@tanstack/lit-form'
8import type { AnyFieldApi } from '@tanstack/lit-form'
9β
10const ZodSchema = z.object({
11 firstName: z
12 .string()
13 .min(3, '[Zod] You must have a length of at least 3')
14 .startsWith('A', "[Zod] First name must start with 'A'"),
15 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
16})
17β
18const ValibotSchema = v.object({
19 firstName: v.pipe(
20 v.string(),
21 v.minLength(3, '[Valibot] You must have a length of at least 3'),
22 v.startsWith('A', "[Valibot] First name must start with 'A'"),
23 ),
24 lastName: v.pipe(
25 v.string(),
26 v.minLength(3, '[Valibot] You must have a length of at least 3'),
27 ),
28})
29β
30const ArkTypeSchema = type({
31 firstName: 'string >= 3',
32 lastName: 'string >= 3',
33})
34β
35const EffectSchema = S.standardSchemaV1(
36 S.Struct({
37 firstName: S.String.pipe(
38 S.minLength(3),
39 S.annotations({
40 message: () => '[Effect/Schema] You must have a length of at least 3',
41 }),
42 ),
43 lastName: S.String.pipe(
44 S.minLength(3),
45 S.annotations({
46 message: () => '[Effect/Schema] You must have a length of at least 3',
47 }),
48 ),
49 }),
50)
51β
52// Swap the validator below to any of these standard-schema implementations.
53void ValibotSchema
54void ArkTypeSchema
55void EffectSchema
56β
57function fieldInfo(field: AnyFieldApi) {
58 return html`
59 ${
60 field.meta.isTouched && field.meta.isInvalid
61 ? html`<em role="alert">
62 ${field.errors.map((error) => error.message).join(', ')}
63 </em>`
64 : nothing
65 }
66 ${field.meta.isValidating ? 'Validating...' : nothing}
67 `
68}
69β
70@customElement('tanstack-schema-form')
71export class TanStackSchemaForm extends LitElement {
72 private form = new TanStackFormController(this, {
73 defaultValues: {
74 firstName: '',
75 lastName: '',
76 },
77 validators: [
78 {
79 run: ZodSchema,
80 triggers: ['change'],
81 },
82 ],
83 onSubmit: async ({ value }) => {
84 console.log(value)
85 },
86 })
87β
88 protected createRenderRoot() {
89 return this
90 }
91β
92 render() {
93 return html`
94 <h1>Standard Schema Form Example</h1>
95 <form
96 @submit=${(event: SubmitEvent) => {
97 event.preventDefault()
98 event.stopPropagation()
99 void this.form.api.handleSubmit()
100 }}
101 >
102 ${this.renderStringField('firstName', 'First Name')}
103 ${this.renderStringField('lastName', 'Last Name')}
104 ${this.form.subscribe(
105 (state) => [state.canSubmit, state.isSubmitting] as const,
106 ([canSubmit, isSubmitting]) => html`
107 <button type="submit" ?disabled=${!canSubmit || isSubmitting}>
108 ${isSubmitting ? '...' : 'Submit'}
109 </button>
110 `,
111 )}
112 </form>
113 `
114 }
115β
116 private renderStringField(name: 'firstName' | 'lastName', label: string) {
117 return this.form.field(
118 { name },
119 (field) => html`
120 <div>
121 <label for=${field.name}>${label}:</label>
122 <input
123 id=${field.name}
124 name=${field.name}
125 .value=${field.value}
126 @blur=${() => field.handleBlur()}
127 @input=${(event: InputEvent) =>
128 field.handleChange(
129 (event.currentTarget as HTMLInputElement).value,
130 )}
131 aria-invalid=${field.meta.isInvalid ? 'true' : 'false'}
132 />
133 ${fieldInfo(field)}
134 </div>
135 `,
136 )
137 }
138}