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

index.tsx

151 lines3.3 KB

1import * as React from 'react'
2import { TanStackDevtools } from '@tanstack/react-devtools'
3import { useForm } from '@tanstack/react-form'
4import { formDevtoolsPlugin } from '@tanstack/react-form-devtools'
5import ReactDOM from 'react-dom/client'
6import './index.css'
7import { DateRangeField } from './fieldGroups/dateRange'
8import { LowerBoundField, UpperBoundField } from './fieldGroups/fieldBounds'
9​
10// The bounds field groups package validation and blur normalization once, then
11// each form binds the group's virtual names to its own real field paths.
12function PricingFilterForm() {
13 const form = useForm({
14 defaultValues: {
15 minPrice: '',
16 maxPrice: '',
17 },
18 })
19​
20 return (
21 <>
22 <LowerBoundField
23 label="Lowest Price"
24 form={form}
25 fields={{
26 // The group calls this field `value`; this form stores it at `minPrice`.
27 value: 'minPrice',
28 }}
29 />
30 <UpperBoundField
31 label="Highest Price"
32 form={form}
33 fields={{
34 // `value` is rendered, and `lowerBound` is only read by the validator.
35 value: 'maxPrice',
36 lowerBound: 'minPrice',
37 }}
38 />
39 </>
40 )
42​
43function AgeRangeForm() {
44 const form = useForm({
45 defaultValues: {
46 minAge: '',
47 maxAge: '',
48 },
49 })
50​
51 return (
52 <>
53 <LowerBoundField
54 label="Lowest Age"
55 form={form}
56 fields={{
57 // Same field group, different form paths.
58 value: 'minAge',
59 }}
60 />
61 <UpperBoundField
62 label="Highest Age"
63 form={form}
64 fields={{
65 value: 'maxAge',
66 lowerBound: 'minAge',
67 }}
68 />
69 </>
70 )
72​
73// DateRangeField has access to both dates, but it still knows nothing about the
74// array it is rendered from. The parent form supplies those concrete paths here.
75function SwappableDateRangesForm() {
76 const form = useForm({
77 defaultValues: {
78 dateRanges: [
79 {
80 id: 'A',
81 start: '2026-07-01',
82 end: '2026-07-05',
83 },
84 {
85 id: 'B',
86 start: '2026-08-10',
87 end: '2026-08-15',
88 },
89 ],
90 },
91 })
92​
93 return (
94 <>
95 <button
96 type="button"
97 onClick={() => form.swapFieldValues('dateRanges', 0, 1)}
98 >
99 Swap date ranges
100 </button>
101 <form.ArrayField name="dateRanges">
102 {(array) =>
103 array.value.map((range, i) => (
104 <DateRangeField
105 key={range.id}
106 label={`Range ${range.id}`}
107 form={form}
108 fields={{
109 start: `dateRanges[${i}].start`,
110 end: `dateRanges[${i}].end`,
111 }}
112 />
113 ))
114 }
115 </form.ArrayField>
116 </>
117 )
119​
120function App() {
121 return (
122 <div>
123 <h1>Field Group example</h1>
124​
125 <div>
126 <h2>Price Filter Form</h2>
127 <PricingFilterForm />
128 </div>
129 <div>
130 <h2>Age Range Form</h2>
131 <AgeRangeForm />
132 </div>
133 <div>
134 <h2>Swappable Date Ranges Form</h2>
135 <SwappableDateRangesForm />
136 </div>
137 <TanStackDevtools
138 plugins={[formDevtoolsPlugin()]}
139 config={{
140 hideUntilHover: false,
141 }}
142 />
143 </div>
144 )
146​
147ReactDOM.createRoot(document.getElementById('root')!).render(
148 <React.StrictMode>
149 <App />
150 </React.StrictMode>,