Open History to see the last commit
1import { LitElement, html } from 'lit'
2import { customElement } from 'lit/decorators.js'
3import { TanStackFormController } from '@tanstack/lit-form'
4import { DateRangeField } from './date-range'
5import { LowerBoundField, UpperBoundField } from './field-bounds'
6import './styles.css'
7β
8@customElement('tanstack-field-groups')
9export class TanStackFieldGroups extends LitElement {
10 private pricingForm = new TanStackFormController(this, {
11 defaultValues: { minPrice: '', maxPrice: '' },
12 })
13β
14 private ageForm = new TanStackFormController(this, {
15 defaultValues: { minAge: '', maxAge: '' },
16 })
17β
18 private dateForm = new TanStackFormController(this, {
19 defaultValues: {
20 dateRanges: [
21 { id: 'A', start: '2026-07-01', end: '2026-07-05' },
22 { id: 'B', start: '2026-08-10', end: '2026-08-15' },
23 ],
24 },
25 })
26β
27 protected createRenderRoot() {
28 return this
29 }
30β
31 render() {
32 return html`
33 <h1>Reusable Field Group Example</h1>
34 <section>
35 <h2>Price Filter Form</h2>
36 ${LowerBoundField({
37 form: this.pricingForm,
38 fields: { value: 'minPrice' },
39 label: 'Lowest Price',
40 })}
41 ${UpperBoundField({
42 form: this.pricingForm,
43 fields: { value: 'maxPrice', lowerBound: 'minPrice' },
44 label: 'Highest Price',
45 })}
46 </section>
47 <section>
48 <h2>Age Range Form</h2>
49 ${LowerBoundField({
50 form: this.ageForm,
51 fields: { value: 'minAge' },
52 label: 'Lowest Age',
53 })}
54 ${UpperBoundField({
55 form: this.ageForm,
56 fields: { value: 'maxAge', lowerBound: 'minAge' },
57 label: 'Highest Age',
58 })}
59 </section>
60 <section>
61 <h2>Swappable Date Ranges</h2>
62 <button
63 type="button"
64 @click=${() => this.dateForm.api.swapFieldValues('dateRanges', 0, 1)}
65 >
66 Swap date ranges
67 </button>
68 ${this.dateForm.arrayField({ name: 'dateRanges' }, (array) =>
69 array.value.map((range, index) =>
70 DateRangeField({
71 form: this.dateForm,
72 fields: {
73 start: `dateRanges[${index}].start`,
74 end: `dateRanges[${index}].end`,
75 },
76 label: `Range ${range.id}`,
77 }),
78 ),
79 )}
80 </section>
81 `
82 }
83}