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

index.ts

226 lines8.9 KB

1import { LitElement, html, nothing } from 'lit'
2import { customElement } from 'lit/decorators.js'
3​
4import { TanStackFormController } from '@tanstack/lit-form'
5import { repeat } from 'lit/directives/repeat.js'
6import { styles } from './styles.js'
7​
8interface Employee {
9 firstName: string
10 lastName: string
11 employed: boolean
12 jobTitle?: string
13 about?: string
15​
16@customElement('tanstack-form-demo')
17export class TanStackFormDemo extends LitElement {
18 static styles = styles
19​
20 #form = new TanStackFormController(this, {
21 defaultValues: {
22 employees: [] as Employee[],
23 },
24 })
25​
26 render() {
27 return html`
28 <form
29 id="form"
30 @submit=${(e: Event) => {
31 e.preventDefault()
32 }}
33 >
34 <h1>TanStack Form - Lit Demo</h1>
35 ${this.#form.field(
36 {
37 name: 'employees',
38 defaultValue: [],
39 },
40 (employeesField) => {
41 return html`${repeat(
42 employeesField.state.value,
43 (_, index) => index,
44 (_, index) => {
45 return html`
46 ${this.#form.field(
47 {
48 name: `employees[${index}].firstName`,
49 validators: {
50 onChange: ({ value }: { value: string }) => {
51 return value && value.length < 3
52 ? 'Not long enough'
53 : undefined
54 },
55 },
56 },
57 (field) => {
58 return html` <div>
59 <div class="container">
60 <label>First Name</label>
61 <input
62 type="text"
63 placeholder="First Name"
64 .value="${field.state.value}"
65 @blur="${() => field.handleBlur()}"
66 @input="${(e: Event) => {
67 const target = e.target as HTMLInputElement
68 field.handleChange(target.value)
69 }}"
70 />
71 </div>
72 ${field.state.meta.isTouched &&
73 field.state.meta.errors.length
74 ? html`${repeat(
75 field.state.meta.errors,
76 (__, idx) => idx,
77 (error) => {
78 return html`<div class="container">
79 ${error}
80 </div>`
81 },
82 )}`
83 : nothing}
84 </div>`
85 },
86 )}
87 ${this.#form.field(
88 { name: `employees[${index}].lastName` },
89 (lastNameField) => {
90 return html` <div class="container">
91 <label>Last Name</label>
92 <input
93 type="text"
94 placeholder="Last Name"
95 .value="${lastNameField.state.value}"
96 @blur="${() => lastNameField.handleBlur()}"
97 @input="${(e: Event) => {
98 const target = e.target as HTMLInputElement
99 lastNameField.handleChange(target.value)
100 }}"
101 />
102 </div>`
103 },
104 )}
105 ${this.#form.field(
106 { name: `employees[${index}].about`, defaultValue: '' },
107 (aboutField) => {
108 return html` <div class="container">
109 <label>About</label>
110 <textarea
111 style="width: 100%"
112 .value="${aboutField.state.value}"
113 @blur="${() => aboutField.handleBlur()}"
114 @input="${(e: Event) => {
115 const target = e.target as HTMLInputElement
116 aboutField.handleChange(target.value)
117 }}"
118 ></textarea>
119 </div>`
120 },
121 )}
122 ${this.#form.field(
123 { name: `employees[${index}].employed` },
124 (employedField) => {
125 return html`<div class="container">
126 <label>Employed?</label>
127 <input
128 type="checkbox"
129 @input="${() =>
130 employedField.handleChange(
131 !employedField.state.value,
132 )}"
133 .checked="${employedField.state.value}"
134 @blur="${() => employedField.handleBlur()}"
135 />
136 </div>
137 ${employedField.state.value
138 ? this.#form.field(
139 {
140 name: `employees[${index}].jobTitle`,
141 defaultValue: '',
142 validators: {
143 onChange: ({ value }) => {
144 return value?.length === 0
145 ? 'Needs to have a job here'
146 : null
147 },
148 },
149 },
150 (jobTitleField) => {
151 return html` <div>
152 <div class="container">
153 <label>Job Title</label>
154 <input
155 type="text"
156 placeholder="Job Title"
157 .value="${jobTitleField.state.value}"
158 @blur="${() =>
159 jobTitleField.handleBlur()}"
160 @input="${(e: Event) => {
161 const target =
162 e.target as HTMLInputElement
163 jobTitleField.handleChange(
164 target.value,
165 )
166 }}"
167 />
168 </div>
169 ${jobTitleField.state.meta.isTouched &&
170 jobTitleField.state.meta.errors.length
171 ? html`${repeat(
172 jobTitleField.state.meta.errors,
173 (__, idx) => idx,
174 (error) => {
175 return html`<div class="container">
176 ${error}
177 </div>`
178 },
179 )}`
180 : nothing}
181 </div>`
182 },
183 )
184 : ''} `
185 },
186 )}
187 `
188 },
189 )}
190​
191 <div class="container">
192 <button
193 type="button"
194 @click=${() => {
195 employeesField.pushValue({
196 firstName: '',
197 lastName: '',
198 employed: false,
199 })
200 }}
201 >
202 Add employee
203 </button>
204 </div> `
205 },
206 )}
207​
208 <div class="container">
209 <button type="submit" ?disabled=${this.#form.api.state.isSubmitting}>
210 ${this.#form.api.state.isSubmitting ? html` Submitting` : 'Submit'}
211 </button>
212 <button
213 type="button"
214 id="reset"
215 @click=${() => {
216 this.#form.api.reset()
217 }}
218 >
219 Reset
220 </button>
221 </div>
222 </form>
223 <pre>${JSON.stringify(this.#form.api.state, null, 2)}</pre>
224 `
225 }