1
- import { AnyListener , ArrayForm , ChildForm , ErrorMap , FormInput , FormSelect , FormTextArea , useForm } from "typed-react-form" ;
1
+ import { AnyListener , ArrayForm , ChildForm , ErrorMap , Field , useForm } from "typed-react-form" ;
2
2
import tv , { SchemaType } from "typed-object-validator" ;
3
3
import React from "react" ;
4
4
import { VisualRender } from "./VisualRender" ;
@@ -37,15 +37,18 @@ export function ExampleForm() {
37
37
{
38
38
longText : "" ,
39
39
text : "" ,
40
+ customText : "nice" ,
40
41
number : 123 ,
41
42
enum : "option1" ,
42
43
boolean : false ,
43
44
object : { childText : "" , childNumber : 0 } ,
45
+ date : new Date ( ) ,
44
46
array : [ "Item 1" , "Item 2" ] ,
45
47
objectArray : [
46
48
{ boolean : true , text : "Item 1" } ,
47
49
{ boolean : false , text : "Item 2" }
48
- ]
50
+ ] ,
51
+ tags : [ "food" ]
49
52
} as FormData ,
50
53
validate
51
54
) ;
@@ -71,31 +74,31 @@ export function ExampleForm() {
71
74
72
75
{ /* A simple text input */ }
73
76
< label > Text</ label >
74
- < FormInput form = { form } name = "text" />
75
- < pre > { `<FormInput form={form} name="fieldName" />` } </ pre >
77
+ < Field form = { form } name = "text" />
78
+ < pre > { `<Field form={form} name="fieldName" />` } </ pre >
76
79
77
80
{ /* A simple text input */ }
78
81
< label > Deserializer text</ label >
79
- < FormInput form = { form } name = "customText" serializer = { ( e ) => e ?. toLowerCase ( ) } deserializer = { ( e ) => e . toUpperCase ( ) } />
80
- < pre > { `<FormInput form={form} name="fieldName" />` } </ pre >
82
+ < Field form = { form } name = "customText" serializer = { ( e ) => e ?. toLowerCase ( ) } deserializer = { ( e ) => e . toUpperCase ( ) } />
83
+ < pre > { `<Field form={form} name="fieldName" />` } </ pre >
81
84
82
85
{ /* A textarea */ }
83
86
< label > Long text</ label >
84
- < FormTextArea form = { form } name = "longText" />
87
+ < Field form = { form } name = "longText" as = "textarea " />
85
88
< pre > { `<FormTextArea form={form} name="fieldName" />` } </ pre >
86
89
87
90
{ /* A number input */ }
88
91
< label > Number</ label >
89
- < FormInput form = { form } type = "number" name = "number" />
90
- < pre > { `<FormInput form={form} type="number" name="fieldName" />` } </ pre >
92
+ < Field form = { form } type = "number" name = "number" />
93
+ < pre > { `<Field form={form} type="number" name="fieldName" />` } </ pre >
91
94
92
95
{ /* A select input */ }
93
96
< label > Enum</ label >
94
- < FormSelect form = { form } name = "enum" >
97
+ < Field form = { form } as = "select" name = "enum" >
95
98
< option value = "option1" > Option 1</ option >
96
99
< option value = "option2" > Option 2</ option >
97
100
< option value = "option3" > Option 3</ option >
98
- </ FormSelect >
101
+ </ Field >
99
102
< pre > { `
100
103
<FormSelect form={form} name="fieldName">
101
104
<option value="option1">Option 1</option>
@@ -106,65 +109,65 @@ export function ExampleForm() {
106
109
107
110
{ /* A checkbox input */ }
108
111
< label > Boolean</ label >
109
- < FormInput form = { form } type = "checkbox" name = "boolean" />
110
- < pre > { `<FormInput form={form} type="checkbox" name="fieldName" />` } </ pre >
112
+ < Field form = { form } type = "checkbox" name = "boolean" />
113
+ < pre > { `<Field form={form} type="checkbox" name="fieldName" />` } </ pre >
111
114
112
115
< label > Tags</ label >
113
116
< div >
114
117
< label >
115
118
Food
116
- < FormInput form = { form } type = "checkbox" name = "tags" value = "food" />
119
+ < Field form = { form } type = "checkbox" name = "tags" value = "food" />
117
120
</ label >
118
121
< label >
119
122
Sports
120
- < FormInput form = { form } type = "checkbox" name = "tags" value = "sports" />
123
+ < Field form = { form } type = "checkbox" name = "tags" value = "sports" />
121
124
</ label >
122
125
< label >
123
126
Tech
124
- < FormInput form = { form } type = "checkbox" name = "tags" value = "tech" />
127
+ < Field form = { form } type = "checkbox" name = "tags" value = "tech" />
125
128
</ label >
126
129
</ div >
127
- < pre > { `<FormInput form={form} type="checkbox" name="fieldName" />` } </ pre >
130
+ < pre > { `<Field form={form} type="checkbox" name="fieldName" />` } </ pre >
128
131
129
132
{ /* A radio input */ }
130
133
< label > Enum</ label >
131
134
< div >
132
135
< label >
133
- < FormInput form = { form } type = "radio" name = "enum" value = "option1" /> Option 1
136
+ < Field form = { form } type = "radio" name = "enum" value = "option1" /> Option 1
134
137
</ label >
135
138
< label >
136
- < FormInput form = { form } type = "radio" name = "enum" value = "option2" /> Option 2
139
+ < Field form = { form } type = "radio" name = "enum" value = "option2" /> Option 2
137
140
</ label >
138
141
< label >
139
- < FormInput form = { form } type = "radio" name = "enum" value = "option3" /> Option 3
142
+ < Field form = { form } type = "radio" name = "enum" value = "option3" /> Option 3
140
143
</ label >
141
144
</ div >
142
145
< pre > { `
143
- <FormInput form={form} type="radio" name="fieldName" value="option1" /> Option 1
144
- <FormInput form={form} type="radio" name="fieldName" value="option2" /> Option 2
145
- <FormInput form={form} type="radio" name="fieldName" value="option3" /> Option 3
146
+ <Field form={form} type="radio" name="fieldName" value="option1" /> Option 1
147
+ <Field form={form} type="radio" name="fieldName" value="option2" /> Option 2
148
+ <Field form={form} type="radio" name="fieldName" value="option3" /> Option 3
146
149
` } </ pre >
147
150
148
151
{ /* A date input */ }
149
152
< label > Date</ label >
150
- < FormInput form = { form } type = "date" name = "date" />
151
- < pre > { `<FormInput form={form} type="date" name="fieldName" />` } </ pre >
153
+ < Field form = { form } type = "date" name = "date" />
154
+ < pre > { `<Field form={form} type="date" name="fieldName" />` } </ pre >
152
155
153
156
{ /* A date timestamp input */ }
154
157
< label > Timestamp (number)</ label >
155
- < FormInput form = { form } type = "date" name = "number" dateAsNumber />
156
- < pre > { `<FormInput form={form} type="date" name="fieldName" dateAsNumber />` } </ pre >
158
+ < Field form = { form } type = "date" name = "number" dateAsNumber />
159
+ < pre > { `<Field form={form} type="date" name="fieldName" dateAsNumber />` } </ pre >
157
160
158
161
{ /* Toggle field */ }
159
162
< label > Toggle text</ label >
160
163
< div >
161
- < FormInput form = { form } type = "checkbox" name = "text" setUndefinedOnUncheck value = "" />
162
- < FormInput form = { form } name = "text" hideWhenNull />
164
+ < Field form = { form } type = "checkbox" name = "text" setUndefinedOnUncheck value = "" />
165
+ < Field form = { form } name = "text" hideWhenNull />
163
166
</ div >
164
167
< pre >
165
168
{ `
166
- <FormInput form={form} type="checkbox" name="text" setUndefinedOnUncheck value="" />
167
- <FormInput form={form} name="text" hideWhenNull />
169
+ <Field form={form} type="checkbox" name="text" setUndefinedOnUncheck value="" />
170
+ <Field form={form} name="text" hideWhenNull />
168
171
` }
169
172
</ pre >
170
173
@@ -177,9 +180,9 @@ export function ExampleForm() {
177
180
render = { ( form ) => (
178
181
< div style = { { background : "#0001" } } >
179
182
< p > Text</ p >
180
- < FormInput form = { form } name = "childText" />
183
+ < Field form = { form } name = "childText" />
181
184
< p > Number</ p >
182
- < FormInput form = { form } name = "childNumber" type = "number" />
185
+ < Field form = { form } name = "childNumber" type = "number" />
183
186
</ div >
184
187
) }
185
188
/>
@@ -191,7 +194,7 @@ export function ExampleForm() {
191
194
name="parentObjectFieldName"
192
195
render={(childForm) => (
193
196
<div>
194
- <FormInput form={childForm} name="childFieldName" />
197
+ <Field form={childForm} name="childFieldName" />
195
198
</div>
196
199
)}
197
200
/>
@@ -200,10 +203,10 @@ export function ExampleForm() {
200
203
201
204
{ /* Set object field to undefined on uncheck */ }
202
205
< label > Toggle object</ label >
203
- < FormInput form = { form } type = "checkbox" name = "object" setUndefinedOnUncheck value = { { childNumber : 0 , childText : "" } } />
206
+ < Field form = { form } type = "checkbox" name = "object" setUndefinedOnUncheck value = { { childNumber : 0 , childText : "" } } />
204
207
< pre >
205
208
{ `
206
- <FormInput
209
+ <Field
207
210
form={form}
208
211
type="checkbox"
209
212
name="fieldName"
@@ -221,7 +224,7 @@ export function ExampleForm() {
221
224
< ul >
222
225
{ form . values . map ( ( _ , i ) => (
223
226
< li key = { i } >
224
- < FormInput form = { form } name = { i } />
227
+ < Field form = { form } name = { i } />
225
228
</ li >
226
229
) ) }
227
230
</ ul >
@@ -236,7 +239,7 @@ export function ExampleForm() {
236
239
<ul>
237
240
{form.values.map((_, i) => (
238
241
<li key={i}>
239
- <FormInput form={form} name={i} />
242
+ <Field form={form} name={i} />
240
243
</li>
241
244
))}
242
245
</ul>
@@ -253,7 +256,7 @@ export function ExampleForm() {
253
256
< ul >
254
257
{ form . values . map ( ( _ , i ) => (
255
258
< li key = { i } >
256
- < FormInput form = { form } name = { i } />
259
+ < Field form = { form } name = { i } />
257
260
< button onClick = { ( ) => remove ( i ) } > Remove</ button >
258
261
</ li >
259
262
) ) }
@@ -273,7 +276,7 @@ export function ExampleForm() {
273
276
<ul>
274
277
{form.values.map((_, i) => (
275
278
<li key={i}>
276
- <FormInput form={form} name={i} />
279
+ <Field form={form} name={i} />
277
280
<button onClick={() => remove(i)}>Remove</button>
278
281
</li>
279
282
))}
@@ -299,8 +302,8 @@ export function ExampleForm() {
299
302
name = { i }
300
303
render = { ( form ) => (
301
304
< div >
302
- < FormInput form = { form } name = "text" />
303
- < FormInput form = { form } name = "boolean" type = "checkbox" />
305
+ < Field form = { form } name = "text" />
306
+ < Field form = { form } name = "boolean" type = "checkbox" />
304
307
</ div >
305
308
) }
306
309
/>
@@ -321,7 +324,7 @@ export function ExampleForm() {
321
324
name={i}
322
325
render={(form) => (
323
326
<div>
324
- <FormInput form={form} name="objectFieldName" />
327
+ <Field form={form} name="objectFieldName" />
325
328
</div>
326
329
)}
327
330
/>
@@ -344,8 +347,8 @@ export function ExampleForm() {
344
347
name = { i }
345
348
render = { ( form ) => (
346
349
< div >
347
- < FormInput form = { form } name = "text" />
348
- < FormInput form = { form } name = "boolean" type = "checkbox" />
350
+ < Field form = { form } name = "text" />
351
+ < Field form = { form } name = "boolean" type = "checkbox" />
349
352
< button type = "button" onClick = { ( ) => remove ( i ) } >
350
353
Remove
351
354
</ button >
@@ -372,7 +375,7 @@ export function ExampleForm() {
372
375
name={i}
373
376
render={(form) => (
374
377
<div>
375
- <FormInput form={form} name="objectFieldName" />
378
+ <Field form={form} name="objectFieldName" />
376
379
<button type="button" onClick={() => remove(i)}>
377
380
Remove
378
381
</button>
0 commit comments