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

index.tsx

146 lines3.3 KB

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