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

index.ts

52 lines1.4 KB

1import { LitElement, html } from 'lit'
2import { customElement } from 'lit/decorators.js'
3import { TanStackFormController } from '@tanstack/lit-form'
4import { formSection } from './form-section'
5import { formSubmitButton } from './form-submit-button'
6import { sharedFormOptions } from './shared-form'
7import { stringField } from './string-field'
8import './styles.css'
9​
10@customElement('tanstack-split-form')
11export class TanStackSplitForm extends LitElement {
12 private form = new TanStackFormController(this, {
13 ...sharedFormOptions,
14 onSubmit: ({ value }) => {
15 console.log(value)
16 },
17 })
18​
19 protected createRenderRoot() {
20 return this
21 }
22​
23 render() {
24 return html`
25 <h1>Split Form Example</h1>
26 <form
27 @submit=${(event: SubmitEvent) => {
28 event.preventDefault()
29 event.stopPropagation()
30 void this.form.api.handleSubmit()
31 }}
32 >
33 ${this.form.field({ name: 'firstName' }, (field) =>
34 stringField(field, 'First Name'),
35 )}
36 ${this.form.field({ name: 'lastName' }, (field) =>
37 stringField(field, 'Last Name'),
38 )}
39 ${formSection(this.form)} ${formSubmitButton(this.form)}
40 <button
41 type="reset"
42 @click=${(event: Event) => {
43 event.preventDefault()
44 this.form.api.reset()
45 }}
46 >
47 Reset
48 </button>
49 </form>
50 `
51 }