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