mobx-react-hook-form 5.1.0 → 5.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -2
- package/mobx-form/mobx-form.d.ts +16 -3
- package/mobx-form/mobx-form.d.ts.map +1 -1
- package/mobx-form/mobx-form.js +19 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# [mobx-react-hook-form](https://github.com/js2me/mobx-react-hook-form)
|
|
2
2
|
|
|
3
|
-
Simple [react-hook-form](https://react-hook-form.com/) wrapper for [MobX](https://mobx.js.org/).
|
|
3
|
+
Simple [react-hook-form](https://react-hook-form.com/) wrapper for [MobX](https://mobx.js.org/).
|
|
4
4
|
|
|
5
|
-
## Usage
|
|
5
|
+
## Usage
|
|
6
6
|
|
|
7
7
|
```tsx
|
|
8
8
|
import { reaction } from "mobx";
|
|
@@ -36,3 +36,69 @@ const YourView = observer(() => {
|
|
|
36
36
|
})
|
|
37
37
|
|
|
38
38
|
```
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
This library uses [`createFormControl`](https://react-hook-form.com/docs/createFormControl).
|
|
43
|
+
So API is almost identical with result of `createFormControl` function call.
|
|
44
|
+
|
|
45
|
+
Differences:
|
|
46
|
+
|
|
47
|
+
- `reset` method renamed to `resetForm`
|
|
48
|
+
|
|
49
|
+
## Additional API
|
|
50
|
+
|
|
51
|
+
### `changeField(name, value, opts)`
|
|
52
|
+
|
|
53
|
+
The same as [`setValue`](https://react-hook-form.com/docs/useform/setvalue), but will trigger validation if form was submitted
|
|
54
|
+
Also you can pass `undefined` as value to remove value
|
|
55
|
+
It should work the same as `field.onChange` from `react-hook-form`'s Controller
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
// Update a single field
|
|
61
|
+
changeField("name", "value");
|
|
62
|
+
|
|
63
|
+
/** form submitted **/
|
|
64
|
+
|
|
65
|
+
changeField("name", "value"); // will call setValue('name', 'value', { shouldValidate: true })
|
|
66
|
+
|
|
67
|
+
changeField("name", undefined); // removes value
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `submit()`
|
|
71
|
+
|
|
72
|
+
This method is needed to pass into `<form />` as `onSubmit` prop, or you can call this method if you want to submit form
|
|
73
|
+
|
|
74
|
+
Example:
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
const form = new Form();
|
|
78
|
+
|
|
79
|
+
const Component = () => {
|
|
80
|
+
return (
|
|
81
|
+
<form onSubmit={form.submit} onReset={form.reset}>
|
|
82
|
+
...
|
|
83
|
+
</form>
|
|
84
|
+
);
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `reset()`
|
|
89
|
+
|
|
90
|
+
This method is needed to pass into `<form />` as `onReset` prop, or you can call this method if you want to reset form
|
|
91
|
+
|
|
92
|
+
Example:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
const form = new Form();
|
|
96
|
+
|
|
97
|
+
const Component = () => {
|
|
98
|
+
return (
|
|
99
|
+
<form onSubmit={form.submit} onReset={form.reset}>
|
|
100
|
+
...
|
|
101
|
+
</form>
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
```
|
package/mobx-form/mobx-form.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseSyntheticEvent } from 'react';
|
|
2
|
-
import { Control, createFormControl, DeepMap, DeepPartial, DefaultValues, FieldErrors, FieldValues, FormState, UseFormClearErrors, UseFormRegister, UseFormReset, UseFormResetField, UseFormSetError, UseFormSetFocus, UseFormSetValue, UseFormTrigger, UseFormUnregister } from 'react-hook-form';
|
|
2
|
+
import { Control, createFormControl, DeepMap, DeepPartial, DefaultValues, FieldErrors, FieldPath, FieldPathValue, FieldValues, FormState, SetValueConfig, UseFormClearErrors, UseFormRegister, UseFormReset, UseFormResetField, UseFormSetError, UseFormSetFocus, UseFormSetValue, UseFormTrigger, UseFormUnregister } from 'react-hook-form';
|
|
3
3
|
import { FormParams } from './mobx-form.types.js';
|
|
4
4
|
type FormFullState<TFieldValues extends FieldValues> = FormState<TFieldValues> & {
|
|
5
5
|
values: TFieldValues;
|
|
@@ -261,8 +261,22 @@ export declare class Form<TFieldValues extends FieldValues = FieldValues, TConte
|
|
|
261
261
|
/**
|
|
262
262
|
* The same as setValue, but will trigger validation if form was submitted
|
|
263
263
|
* It should work the same as field.onChange from react-hook-form's Controller
|
|
264
|
+
*
|
|
265
|
+
* @param name - the path name to the form field value.
|
|
266
|
+
* @param value - field value
|
|
267
|
+
* @param options - should validate or update form state
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```tsx
|
|
271
|
+
* // Update a single field
|
|
272
|
+
* changeField('name', 'value');
|
|
273
|
+
*
|
|
274
|
+
* ** form submitted **
|
|
275
|
+
*
|
|
276
|
+
* changeField('name', 'value'); // will call setValue('name', 'value', { shouldValidate: true })
|
|
277
|
+
* ```
|
|
264
278
|
*/
|
|
265
|
-
changeField:
|
|
279
|
+
changeField: <TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(name: TFieldName, value: FieldPathValue<TFieldValues, TFieldName> | undefined, options?: SetValueConfig) => void;
|
|
266
280
|
/**
|
|
267
281
|
* Method to manually submit form.
|
|
268
282
|
* Used to attach this method to <form /> element
|
|
@@ -282,7 +296,6 @@ export declare class Form<TFieldValues extends FieldValues = FieldValues, TConte
|
|
|
282
296
|
*/
|
|
283
297
|
reset(e?: BaseSyntheticEvent): void;
|
|
284
298
|
private updateFormState;
|
|
285
|
-
protected lastRafId: number | undefined;
|
|
286
299
|
protected lastTimeoutId: number | undefined;
|
|
287
300
|
private stopScheduledFormStateUpdate;
|
|
288
301
|
private scheduleUpdateFormState;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mobx-form.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form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setValue, but will trigger validation if form was submitted
|
|
359
359
|
* It should work the same as field.onChange from react-hook-form's Controller
|
|
360
|
+
*
|
|
361
|
+
* @param name - the path name to the form field value.
|
|
362
|
+
* @param value - field value
|
|
363
|
+
* @param options - should validate or update form state
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```tsx
|
|
367
|
+
* // Update a single field
|
|
368
|
+
* changeField('name', 'value');
|
|
369
|
+
*
|
|
370
|
+
* ** form submitted **
|
|
371
|
+
*
|
|
372
|
+
* changeField('name', 'value'); // will call setValue('name', 'value', { shouldValidate: true })
|
|
373
|
+
* ```
|
|
360
374
|
*/
|
|
361
|
-
changeField = (name, value,
|
|
375
|
+
changeField = (name, value, options) => {
|
|
362
376
|
this.setValue(name, value, {
|
|
363
|
-
...
|
|
364
|
-
shouldValidate:
|
|
377
|
+
...options,
|
|
378
|
+
shouldValidate: options?.shouldValidate ?? this.isSubmitted,
|
|
365
379
|
});
|
|
366
380
|
};
|
|
367
381
|
/**
|
|
@@ -410,13 +424,8 @@ export class Form {
|
|
|
410
424
|
values,
|
|
411
425
|
});
|
|
412
426
|
}
|
|
413
|
-
lastRafId;
|
|
414
427
|
lastTimeoutId;
|
|
415
428
|
stopScheduledFormStateUpdate = () => {
|
|
416
|
-
if (this.lastRafId !== undefined) {
|
|
417
|
-
cancelAnimationFrame(this.lastRafId);
|
|
418
|
-
this.lastRafId = undefined;
|
|
419
|
-
}
|
|
420
429
|
if (this.lastTimeoutId !== undefined) {
|
|
421
430
|
clearTimeout(this.lastTimeoutId);
|
|
422
431
|
this.lastTimeoutId = undefined;
|
|
@@ -425,11 +434,8 @@ export class Form {
|
|
|
425
434
|
scheduleUpdateFormState = (rawFormState) => {
|
|
426
435
|
this.stopScheduledFormStateUpdate();
|
|
427
436
|
this.lastTimeoutId = setTimeout(() => {
|
|
428
|
-
this.
|
|
429
|
-
|
|
430
|
-
this.lastTimeoutId = undefined;
|
|
431
|
-
this.lastRafId = undefined;
|
|
432
|
-
});
|
|
437
|
+
this.updateFormState(rawFormState);
|
|
438
|
+
this.lastTimeoutId = undefined;
|
|
433
439
|
}, this.config.lazyUpdatesTimer ?? 0);
|
|
434
440
|
};
|
|
435
441
|
destroy() {
|