Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

simple.ts

176 lines5.3 KB

1import { LitElement, html } from 'lit'
2import { TanStackFormController } from '../src/index.js'
3import type { FieldApi, FormOptions } from '../src/index.js'
4​
5interface Employee {
6 firstName: string
7 lastName: string
8 color?: '#FF0000' | '#00FF00' | '#0000FF'
9 employed: boolean
10 jobTitle: string
12​
13export const sampleData: Employee = {
14 firstName: 'Christian',
15 lastName: '',
16 employed: false,
17 jobTitle: '',
19​
20const formConfig: FormOptions<Employee, undefined> = {
21 defaultValues: sampleData,
23​
24export class TestForm extends LitElement {
25 form = new TanStackFormController(this, formConfig)
26​
27 render() {
28 return html`
29 <form
30 id="form"
31 @submit=${(e: Event) => {
32 e.preventDefault()
33 }}
34 >
35 <h1>TanStack Form - Lit Demo</h1>
36​
37 ${this.form.field(
38 {
39 name: `firstName`,
40 validators: {
41 onChange: ({ value }) =>
42 value.length < 3 ? 'Not long enough' : undefined,
43 },
44 },
45 (field: FieldApi<Employee, 'firstName'>) => {
46 return html` <div>
47 <label>First Name</label>
48 <input
49 id="firstName"
50 type="text"
51 placeholder="First Name"
52 .value="${field.state.value}"
53 @blur="${() => field.handleBlur()}"
54 @input="${(e: Event) => {
55 const target = e.target as HTMLInputElement
56 field.handleChange(target.value)
57 }}"
58 />
59 </div>`
60 },
61 )}
62 ${this.form.field(
63 {
64 name: `lastName`,
65 validators: {
66 onChange: ({ value }) =>
67 value.length < 3 ? 'Not long enough' : undefined,
68 },
69 },
70 (field: FieldApi<Employee, 'lastName'>) => {
71 return html` <div>
72 <label>Last Name</label>
73 <input
74 id="lastName"
75 type="text"
76 placeholder="Last Name"
77 .value="${field.state.value}"
78 @blur="${() => field.handleBlur()}"
79 @input="${(e: Event) => {
80 const target = e.target as HTMLInputElement
81 field.handleChange(target.value)
82 }}"
83 />
84 </div>`
85 },
86 )}
87 ${this.form.field(
88 { name: `color` },
89 (field: FieldApi<Employee, 'color'>) => {
90 return html` <div>
91 <label>Favorite Color</label>
92 <select
93 .value="${field.state.value}"
94 @blur="${() => field.handleBlur()}"
95 @input="${(e: Event) => {
96 const target = e.target as HTMLInputElement
97 field.handleChange(
98 target.value as '#FF0000' | '#00FF00' | '#0000FF',
99 )
100 }}"
101 >
102 <option value="#FF0000">Red</option>
103 <option value="#00FF00">Green</option>
104 <option value="#0000FF">Blue</option>
105 </select>
106 </div>`
107 },
108 )}
109 ${this.form.field(
110 { name: `employed` },
111 (field: FieldApi<Employee, 'employed'>) => {
112 return html`
113 <div>
114 <label>Employed?</label>
115 <input
116 @input="${() => field.handleChange(!field.state.value)}"
117 .checked="${field.state.value}"
118 @blur="${() => field.handleBlur()}"
119 id="employed"
120 .type=${'checkbox'}
121 />
122 </div>
123 ${field.state.value
124 ? this.form.field(
125 {
126 name: `jobTitle`,
127 validators: {
128 onChange: ({ value }) =>
129 value.length === 0
130 ? 'Needs to have a job here'
131 : null,
132 },
133 },
134 (subField: FieldApi<Employee, 'jobTitle'>) => {
135 return html` <div>
136 <label>Job Title</label>
137 <input
138 type="text"
139 id="jobTitle"
140 placeholder="Job Title"
141 .value="${subField.state.value}"
142 @blur="${() => subField.handleBlur()}"
143 @input="${(e: Event) => {
144 const target = e.target as HTMLInputElement
145 subField.handleChange(target.value)
146 }}"
147 />
148 </div>`
149 },
150 )
151 : ''}
152 `
153 },
154 )}
155 </form>
156 <div>
157 <button
158 type="submit"
159 ?disabled=${this.form.api.state.isSubmitting}
160 >${this.form.api.state.isSubmitting ? html` Submitting` : 'Submit'}
161 </button>
162 <button
163 type="button"
164 id="reset"
165 @click=${() => {
166 this.form.api.reset()
167 }}
168 >
169 Reset
170 </button>
171 </div>
172 </form>
173 <pre>${JSON.stringify(this.form.api.state, null, 2)}</pre>
174 `
175 }