You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"password":"Please lengthen this text to 8 characters or more",
120
-
}
126
+
errors: {
127
+
password:'Please lengthen this text to 8 characters or more',
128
+
},
129
+
clear:Function,
130
+
clearField:Function,
131
+
setField:Function,
121
132
}
122
133
```
123
134
@@ -350,6 +361,50 @@ function Widget() {
350
361
}
351
362
```
352
363
364
+
### Updating Fields Manually
365
+
366
+
There are cases where you may want to update the value of an input manually without user interaction. To do so, the `formState.setField` method can be used.
367
+
368
+
```js
369
+
functionForm() {
370
+
const [formState, { text }] =useFormState();
371
+
372
+
functionsetNameField() {
373
+
// manually setting the value of the "name" input
374
+
formState.setField('name', 'Mary Poppins');
375
+
}
376
+
377
+
return (
378
+
<>
379
+
<input {...text('name')} readOnly />
380
+
<button onClick={setNameField}>Set Name</button>
381
+
</>
382
+
)
383
+
}
384
+
```
385
+
386
+
Please note that when `formState.setField` is called, any existing errors that might have been set due to previous interactions from the user will be cleared, and both of the `validity` and the `touched` states of the input will be set to `true`.
387
+
388
+
It's also possible to clear a single input's value using `formState.clearField`.
389
+
390
+
### Resetting The From State
391
+
392
+
All fields in the form can be cleared all at once at any time using `formState.clear`.
A function that gets called after calling `formState.clear` indicating that all fields in the form state are cleared successfully.
523
+
524
+
```js
525
+
const [formState, inputs] =useFormState(null, {
526
+
onClear() {
527
+
// form state was cleared successfully
528
+
}
529
+
});
530
+
531
+
formState.clear(); // clearing the form state
532
+
```
533
+
463
534
#### `formOptions.withIds`
464
535
465
536
Indicates whether `useFormState` should generate and pass an `id` attribute to its fields. This is helpful when [working with labels](#labels-and-ids).
@@ -497,29 +568,38 @@ The first item returned by `useFormState`.
497
568
const [formState, inputs] =useFormState();
498
569
```
499
570
500
-
An object describing the form state that updates during subsequent re-renders.
501
-
502
-
Form state consists of three nested objects:
503
-
504
-
-`values`: an object holding the state of each input being rendered.
505
-
-`validity`: an object indicating whether the value of each input is valid.
506
-
-`errors`: an object holding all errors resulting from input validations.
507
-
-`touched`: an object indicating whether the input was touched (focused) by the user.
571
+
An object containing the form state that updates during subsequent re-renders. It also include methods to update the form state manually.
508
572
509
573
```ts
510
574
formState= {
575
+
// an object holding the values of all input being rendered
511
576
values: {
512
-
[inputName: string]: string|string[] |boolean,
577
+
[name: string]: string|string[] |boolean,
513
578
},
579
+
580
+
// an object indicating whether the value of each input is valid
514
581
validity: {
515
-
[inputName: string]?: boolean,
582
+
[name: string]?: boolean,
516
583
},
584
+
585
+
// an object holding all errors resulting from input validations
517
586
errors: {
518
-
[input: name]?: any,
587
+
[name: string]?: any,
519
588
},
589
+
590
+
// an object indicating whether the input was touched (focused) by the user
0 commit comments