mobx-react-hook-form 1.0.2 → 1.0.5
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 +32 -6
- package/hooks/use-mobx-form.d.ts.map +1 -1
- package/hooks/use-mobx-form.js +3 -4
- package/mobx-form/mobx-form.d.ts +25 -20
- package/mobx-form/mobx-form.d.ts.map +1 -1
- package/mobx-form/mobx-form.js +30 -7
- package/mobx-form/mobx-form.types.d.ts +27 -1
- package/mobx-form/mobx-form.types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,16 +1,40 @@
|
|
|
1
|
-
# mobx-react-hook-form
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Simple wrapper for `react-hook-form` package
|
|
1
|
+
# [mobx-react-hook-form](https://github.com/js2me/mobx-react-hook-form)
|
|
5
2
|
|
|
3
|
+
Simple [react-hook-form](https://react-hook-form.com/) wrapper for [MobX](https://mobx.js.org/).
|
|
6
4
|
|
|
7
5
|
## Usage
|
|
8
6
|
|
|
9
7
|
```tsx
|
|
8
|
+
import { reaction } from "mobx"
|
|
10
9
|
import { MobxForm } from "mobx-react-hook-form"
|
|
11
10
|
|
|
12
11
|
class YourVM {
|
|
13
|
-
form = new MobxForm(
|
|
12
|
+
form = new MobxForm({
|
|
13
|
+
disposer?: this.disposer,
|
|
14
|
+
resolver: valibotResolver(
|
|
15
|
+
v.object({
|
|
16
|
+
username: v.string('This field is required')
|
|
17
|
+
})
|
|
18
|
+
),
|
|
19
|
+
onSubmit: ({ username }) => {
|
|
20
|
+
console.info("nick username", username);
|
|
21
|
+
},
|
|
22
|
+
onSubmitFailed: () => {
|
|
23
|
+
//
|
|
24
|
+
},
|
|
25
|
+
onReset: () => {
|
|
26
|
+
//
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
mount(){
|
|
31
|
+
reaction(
|
|
32
|
+
() => this.form.data?.username,
|
|
33
|
+
(username) => {
|
|
34
|
+
//
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
}
|
|
14
38
|
}
|
|
15
39
|
|
|
16
40
|
|
|
@@ -19,7 +43,9 @@ const YourView = () => {
|
|
|
19
43
|
|
|
20
44
|
|
|
21
45
|
return (
|
|
22
|
-
<form {
|
|
46
|
+
<form onSubmit={form.onSubmit} onReset={form.onReset}>
|
|
47
|
+
<Controller control={form.control} name={'username'} render={...} />
|
|
48
|
+
</form>
|
|
23
49
|
)
|
|
24
50
|
}
|
|
25
51
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-mobx-form.d.ts","sourceRoot":"","sources":["../../src/hooks/use-mobx-form.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE3D,eAAO,MAAM,WAAW,GAAI,YAAY,SAAS,SAAS,EAAE,QAAQ,YACxD,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,KACzC,iBAAiB,CAAC,YAAY,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"use-mobx-form.d.ts","sourceRoot":"","sources":["../../src/hooks/use-mobx-form.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE3D,eAAO,MAAM,WAAW,GAAI,YAAY,SAAS,SAAS,EAAE,QAAQ,YACxD,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,KACzC,iBAAiB,CAAC,YAAY,EAAE,QAAQ,CAEC,CAAC"}
|
package/hooks/use-mobx-form.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { useForm } from 'react-hook-form';
|
|
2
|
-
export const useMobxForm = (mobxForm) =>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
};
|
|
2
|
+
export const useMobxForm = (mobxForm) =>
|
|
3
|
+
// @ts-expect-error ts(2445)
|
|
4
|
+
mobxForm.connect(useForm(mobxForm.params));
|
package/mobx-form/mobx-form.d.ts
CHANGED
|
@@ -4,33 +4,38 @@ import { AnyObject, Maybe } from 'yammies/utils/types';
|
|
|
4
4
|
import { ConnectedMobxForm, MobxFormParams } from './mobx-form.types';
|
|
5
5
|
export declare class MobxForm<TFieldValues extends AnyObject, TContext = any> implements Disposable {
|
|
6
6
|
protected disposer: IDisposer;
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Readl react-hook-form params
|
|
9
|
+
* Needed to connect real react-hook-form to this mobx wrapper
|
|
10
|
+
*/
|
|
11
|
+
protected params: UseFormProps<TFieldValues, TContext>;
|
|
8
12
|
protected submitHandler?: MobxFormParams<TFieldValues, TContext>['onSubmit'];
|
|
9
13
|
protected submitErrorHandler?: MobxFormParams<TFieldValues, TContext>['onSubmitFailed'];
|
|
10
14
|
protected resetHandler?: MobxFormParams<TFieldValues, TContext>['onReset'];
|
|
15
|
+
/**
|
|
16
|
+
* Original react-hook-form form
|
|
17
|
+
*/
|
|
11
18
|
form: Maybe<UseFormReturn<TFieldValues, TContext>>;
|
|
19
|
+
/**
|
|
20
|
+
* form state received from form.formState
|
|
21
|
+
*/
|
|
12
22
|
state: FormState<TFieldValues>;
|
|
23
|
+
/**
|
|
24
|
+
* Raw data received from form.getValues()
|
|
25
|
+
*/
|
|
13
26
|
data: Maybe<DeepPartial<TFieldValues>>;
|
|
14
27
|
protected isConnected: boolean;
|
|
15
|
-
constructor({ disposer, onSubmit, onSubmitFailed, onReset, ...
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
shouldFocusError: boolean;
|
|
27
|
-
shouldUnregister: boolean;
|
|
28
|
-
shouldUseNativeValidation: boolean;
|
|
29
|
-
progressive: boolean;
|
|
30
|
-
criteriaMode: import("react-hook-form").CriteriaMode;
|
|
31
|
-
delayError: number;
|
|
32
|
-
}>;
|
|
33
|
-
connectRHF(formResult: UseFormReturn<TFieldValues, TContext>): ConnectedMobxForm<TFieldValues, TContext>;
|
|
28
|
+
constructor({ disposer, onSubmit, onSubmitFailed, onReset, ...params }: MobxFormParams<TFieldValues, TContext>);
|
|
29
|
+
/**
|
|
30
|
+
* Allows to modify real react-hook-form useForm() payload
|
|
31
|
+
*/
|
|
32
|
+
setParams(params: UseFormProps<TFieldValues, TContext>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Needed to connect real react-hook-form to this mobx wrapper
|
|
35
|
+
*
|
|
36
|
+
* This is used in useMobxForm
|
|
37
|
+
*/
|
|
38
|
+
protected connect(formResult: UseFormReturn<TFieldValues, TContext>): ConnectedMobxForm<TFieldValues, TContext>;
|
|
34
39
|
dispose(): void;
|
|
35
40
|
}
|
|
36
41
|
//# sourceMappingURL=mobx-form.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mobx-form.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAY,SAAS,EAAE,MAAM,eAAe,CAAC;AAGhE,OAAO,EACL,WAAW,EACX,SAAS,EACT,YAAY,EACZ,aAAa,EACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEtE,qBAAa,QAAQ,CAAC,YAAY,SAAS,SAAS,EAAE,QAAQ,GAAG,GAAG,CAClE,YAAW,UAAU;IAErB,SAAS,CAAC,QAAQ,EAAE,SAAS,CAAC;IAE9B,SAAS,CAAC,
|
|
1
|
+
{"version":3,"file":"mobx-form.d.ts","sourceRoot":"","sources":["../../src/mob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AAC,MAAM,EAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,CAAC;IAItD;;;;OAIG;IACH,SAAS,CAAC,OAAO,CACf,UAAU,EAAE,aAAa,CAAC,YAAY,EAAE,QAAQ,CAAC,GAChD,iBAAiB,CAAC,YAAY,EAAE,QAAQ,CAAC;IA+B5C,OAAO,IAAI,IAAI;CAMhB"}
|
package/mobx-form/mobx-form.js
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
import { Disposer } from 'disposer-util';
|
|
2
2
|
import noop from 'lodash-es/noop';
|
|
3
|
-
import { makeObservable, observable, runInAction } from 'mobx';
|
|
3
|
+
import { action, makeObservable, observable, runInAction } from 'mobx';
|
|
4
4
|
export class MobxForm {
|
|
5
5
|
disposer;
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Readl react-hook-form params
|
|
8
|
+
* Needed to connect real react-hook-form to this mobx wrapper
|
|
9
|
+
*/
|
|
10
|
+
params;
|
|
7
11
|
submitHandler;
|
|
8
12
|
submitErrorHandler;
|
|
9
13
|
resetHandler;
|
|
14
|
+
/**
|
|
15
|
+
* Original react-hook-form form
|
|
16
|
+
*/
|
|
10
17
|
form;
|
|
18
|
+
/**
|
|
19
|
+
* form state received from form.formState
|
|
20
|
+
*/
|
|
11
21
|
state;
|
|
22
|
+
/**
|
|
23
|
+
* Raw data received from form.getValues()
|
|
24
|
+
*/
|
|
12
25
|
data;
|
|
13
26
|
isConnected = false;
|
|
14
|
-
constructor({ disposer, onSubmit, onSubmitFailed, onReset, ...
|
|
27
|
+
constructor({ disposer, onSubmit, onSubmitFailed, onReset, ...params }) {
|
|
15
28
|
this.disposer = disposer ?? new Disposer();
|
|
16
29
|
this.state = {
|
|
17
30
|
disabled: false,
|
|
@@ -32,16 +45,26 @@ export class MobxForm {
|
|
|
32
45
|
this.submitHandler = onSubmit;
|
|
33
46
|
this.submitErrorHandler = onSubmitFailed;
|
|
34
47
|
this.resetHandler = onReset;
|
|
35
|
-
this.
|
|
48
|
+
this.params = params;
|
|
36
49
|
makeObservable(this, {
|
|
37
50
|
state: observable.deep,
|
|
38
51
|
data: observable.deep,
|
|
52
|
+
params: observable.ref,
|
|
53
|
+
setParams: action.bound,
|
|
39
54
|
});
|
|
40
55
|
}
|
|
41
|
-
|
|
42
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Allows to modify real react-hook-form useForm() payload
|
|
58
|
+
*/
|
|
59
|
+
setParams(params) {
|
|
60
|
+
this.params = params;
|
|
43
61
|
}
|
|
44
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Needed to connect real react-hook-form to this mobx wrapper
|
|
64
|
+
*
|
|
65
|
+
* This is used in useMobxForm
|
|
66
|
+
*/
|
|
67
|
+
connect(formResult) {
|
|
45
68
|
if (!this.isConnected) {
|
|
46
69
|
this.isConnected = true;
|
|
47
70
|
runInAction(() => {
|
|
@@ -1,14 +1,40 @@
|
|
|
1
1
|
import { IDisposer } from 'disposer-util';
|
|
2
2
|
import { SubmitErrorHandler, SubmitHandler, UseFormProps, UseFormReturn } from 'react-hook-form';
|
|
3
3
|
import { AnyObject } from 'yammies/utils/types';
|
|
4
|
+
/**
|
|
5
|
+
* Additional options for {@link MobxForm} constructor
|
|
6
|
+
*/
|
|
4
7
|
export interface MobxFormParams<TFieldValues extends AnyObject, TContext = any> extends UseFormProps<TFieldValues, TContext> {
|
|
8
|
+
/**
|
|
9
|
+
* Disposer for mobx form
|
|
10
|
+
*/
|
|
5
11
|
disposer?: IDisposer;
|
|
12
|
+
/**
|
|
13
|
+
* Form submit handler
|
|
14
|
+
*/
|
|
6
15
|
onSubmit?: SubmitHandler<TFieldValues>;
|
|
16
|
+
/**
|
|
17
|
+
* Form submit failed handler
|
|
18
|
+
*/
|
|
7
19
|
onSubmitFailed?: SubmitErrorHandler<TFieldValues>;
|
|
20
|
+
/**
|
|
21
|
+
* Form reset handler
|
|
22
|
+
*/
|
|
8
23
|
onReset?: VoidFunction;
|
|
9
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Interface for a connected Mobx form.
|
|
27
|
+
* Extends the properties of react-hook-form's UseFormReturn,
|
|
28
|
+
* excluding the 'handleSubmit' method, and adds custom form handlers.
|
|
29
|
+
*/
|
|
10
30
|
export interface ConnectedMobxForm<TFieldValues extends AnyObject, TContext = any> extends Omit<UseFormReturn<TFieldValues, TContext>, 'handleSubmit'> {
|
|
11
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Handler to reset the form.
|
|
33
|
+
*/
|
|
12
34
|
onReset: VoidFunction;
|
|
35
|
+
/**
|
|
36
|
+
* Handler to submit the form.
|
|
37
|
+
*/
|
|
38
|
+
onSubmit: VoidFunction;
|
|
13
39
|
}
|
|
14
40
|
//# sourceMappingURL=mobx-form.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mobx-form.types.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,YAAY,EACZ,aAAa,EACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,MAAM,WAAW,cAAc,CAAC,YAAY,SAAS,SAAS,EAAE,QAAQ,GAAG,GAAG,CAC5E,SAAQ,YAAY,CAAC,YAAY,EAAE,QAAQ,CAAC;IAC5C,QAAQ,CAAC,EAAE,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"mobx-form.types.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,YAAY,EACZ,aAAa,EACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD;;GAEG;A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