mobx-react-hook-form 2.0.15 → 2.0.17

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.
@@ -1,6 +1,6 @@
1
- import { DeepPartial, FormState, SubmitErrorHandler, SubmitHandler, UseFormProps, createFormControl } from 'react-hook-form';
1
+ import { DeepPartial, FormState, SubmitErrorHandler, SubmitHandler, UseFormProps, UseFormReturn } from 'react-hook-form';
2
2
  import type { AnyObject, Maybe } from 'yummies/utils/types';
3
- import { MobxFormParams } from './mobx-form.types';
3
+ import { ConnectedMobxForm, MobxFormParams } from './mobx-form.types';
4
4
  export declare class MobxForm<TFieldValues extends AnyObject, TContext = any, TFieldOutputValues extends AnyObject = TFieldValues> {
5
5
  private config;
6
6
  protected abortController: AbortController;
@@ -9,11 +9,13 @@ export declare class MobxForm<TFieldValues extends AnyObject, TContext = any, TF
9
9
  * Needed to connect real react-hook-form to this mobx wrapper
10
10
  */
11
11
  params: UseFormProps<TFieldValues, TContext>;
12
+ protected handleSubmit(...args: Parameters<SubmitHandler<TFieldOutputValues>>): void | Promise<void>;
13
+ protected handleSubmitFailed(...args: Parameters<SubmitErrorHandler<TFieldValues>>): void | Promise<void>;
12
14
  protected handleReset(): void;
13
15
  /**
14
16
  * Original react-hook-form form
15
17
  */
16
- instance: Maybe<ReturnType<typeof createFormControl<TFieldValues, TContext>>>;
18
+ form: Maybe<UseFormReturn<TFieldValues, TContext, TFieldOutputValues>>;
17
19
  /**
18
20
  * form state received from form.formState
19
21
  */
@@ -22,13 +24,8 @@ export declare class MobxForm<TFieldValues extends AnyObject, TContext = any, TF
22
24
  * Raw data received from form.getValues()
23
25
  */
24
26
  data: Maybe<DeepPartial<TFieldValues>>;
25
- onSubmit: (event?: any) => Promise<void>;
26
27
  protected isConnected: boolean;
27
28
  constructor(config: MobxFormParams<TFieldValues, TContext, TFieldOutputValues>);
28
- protected handleSubmit(...args: Parameters<SubmitHandler<TFieldOutputValues>>): void | Promise<void>;
29
- protected handleSubmitFailed(...args: Parameters<SubmitErrorHandler<TFieldValues>>): void | Promise<void>;
30
- protected syncForm(formState: Partial<FormState<TFieldValues>>, data?: DeepPartial<TFieldValues> | TFieldValues): void;
31
- onReset: () => void;
32
29
  /**
33
30
  * Allows to modify real react-hook-form useForm() payload
34
31
  */
@@ -37,7 +34,13 @@ export declare class MobxForm<TFieldValues extends AnyObject, TContext = any, TF
37
34
  * Allows to modify real react-hook-form useForm() payload
38
35
  */
39
36
  updateParams(params: Partial<UseFormProps<TFieldValues, TContext>>): void;
40
- destroy(): void;
37
+ protected syncForm(formResult: UseFormReturn<TFieldValues, TContext, TFieldOutputValues>, data?: DeepPartial<TFieldValues>): void;
38
+ /**
39
+ * Needed to connect real react-hook-form to this mobx wrapper
40
+ *
41
+ * This is used in useMobxForm
42
+ */
43
+ protected connect(formResult: UseFormReturn<TFieldValues, TContext, TFieldOutputValues>): ConnectedMobxForm<TFieldValues, TContext, TFieldOutputValues>;
41
44
  /**
42
45
  * @deprecated use destroy();
43
46
  */
@@ -1 +1 @@
1
- {"version":3,"file":"mobx-form.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,WAAW,EACX,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,YAAY,EACZ,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE5D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAcnD,qBAAa,QAAQ,CACnB,YAAY,SAAS,SAAS,EAC9B,QAAQ,GAAG,GAAG,EACd,kBAAkB,SAAS,SAAS,GAAG,YAAY;IAmCjD,OAAO,CAAC,MAAM;IAjChB,SAAS,CAAC,eAAe,EAAE,eAAe,CAAC;IAE3C;;;OAGG;IACH,MAAM,EAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAE7C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C;IAItD;;OAEG;IACH,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAIlE,SAAS,CAAC,QAAQ,CAChB,UAAU,EAAE,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACrE,IAAI,GAAE,WAAW,CAAC,YAAY,CAAiC;IAOjE;;;;OAIG;IACH,SAAS,CAAC,OAAO,CACf,UAAU,EAAE,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,GACpE,iBAAiB,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC;IA4ChE;;OAEG;IACH,OAAO,IAAI,IAAI;CAGhB"}
@@ -1,16 +1,5 @@
1
1
  import { LinkedAbortController } from 'linked-abort-controller';
2
- import { action, makeObservable, observable, reaction, runInAction, } from 'mobx';
3
- import { createFormControl, } from 'react-hook-form';
4
- const dependsOn = {
5
- isDirty: true,
6
- isLoading: true,
7
- isValidating: true,
8
- isValid: true,
9
- dirtyFields: true,
10
- touchedFields: true,
11
- validatingFields: true,
12
- errors: true,
13
- };
2
+ import { action, makeObservable, observable, reaction } from 'mobx';
14
3
  export class MobxForm {
15
4
  config;
16
5
  abortController;
@@ -19,6 +8,18 @@ export class MobxForm {
19
8
  * Needed to connect real react-hook-form to this mobx wrapper
20
9
  */
21
10
  params;
11
+ handleSubmit(
12
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
+ ...args) {
14
+ this.config.onSubmit?.(...args);
15
+ // used to override
16
+ }
17
+ handleSubmitFailed(
18
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
+ ...args) {
20
+ this.config.onSubmitFailed?.(...args);
21
+ // used to override
22
+ }
22
23
  handleReset() {
23
24
  this.config.onReset?.();
24
25
  // used to override
@@ -26,7 +27,7 @@ export class MobxForm {
26
27
  /**
27
28
  * Original react-hook-form form
28
29
  */
29
- instance;
30
+ form;
30
31
  /**
31
32
  * form state received from form.formState
32
33
  */
@@ -35,7 +36,6 @@ export class MobxForm {
35
36
  * Raw data received from form.getValues()
36
37
  */
37
38
  data;
38
- onSubmit;
39
39
  isConnected = false;
40
40
  constructor(config) {
41
41
  this.config = config;
@@ -46,7 +46,7 @@ export class MobxForm {
46
46
  config.disposer.add(() => this.dispose());
47
47
  }
48
48
  this.abortController.signal.addEventListener('abort', () => {
49
- this.instance = null;
49
+ this.form = null;
50
50
  this.data = null;
51
51
  });
52
52
  this.state = {
@@ -69,23 +69,9 @@ export class MobxForm {
69
69
  observable.deep(this, 'state');
70
70
  observable.deep(this, 'data');
71
71
  observable.ref(this, 'params');
72
- observable.ref(this, 'instance');
73
72
  action.bound(this, 'setParams');
74
73
  action.bound(this, 'updateParams');
75
74
  action.bound(this, 'syncForm');
76
- this.instance = createFormControl(config);
77
- this.onSubmit = this.instance?.handleSubmit(this.handleSubmit.bind(this), this.handleSubmitFailed.bind(this));
78
- /**
79
- *
80
- *
81
- * Subscribes
82
- *
83
- *
84
- */
85
- this.instance.subscribe({
86
- formState: dependsOn,
87
- callback: (formState) => this.syncForm(formState, formState.values),
88
- });
89
75
  makeObservable(this);
90
76
  if (config.getParams) {
91
77
  reaction(config.getParams, (params) => {
@@ -95,32 +81,6 @@ export class MobxForm {
95
81
  });
96
82
  }
97
83
  }
98
- handleSubmit(
99
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
100
- ...args) {
101
- this.config.onSubmit?.(...args);
102
- // used to override
103
- }
104
- handleSubmitFailed(
105
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
106
- ...args) {
107
- this.config.onSubmitFailed?.(...args);
108
- // used to override
109
- }
110
- syncForm(formState, data) {
111
- runInAction(() => {
112
- Object.getOwnPropertyNames(formState).forEach((key) => {
113
- // @ts-expect-error kkkk
114
- this.state[key] = formState[key];
115
- });
116
- if (this.data) {
117
- this.data = data;
118
- }
119
- });
120
- }
121
- onReset = () => {
122
- this.config.onReset?.();
123
- };
124
84
  /**
125
85
  * Allows to modify real react-hook-form useForm() payload
126
86
  */
@@ -133,13 +93,52 @@ export class MobxForm {
133
93
  updateParams(params) {
134
94
  this.setParams({ ...this.params, ...params });
135
95
  }
136
- destroy() {
137
- this.abortController.abort();
96
+ syncForm(formResult, data = formResult.getValues()) {
97
+ this.form = formResult;
98
+ this.state = formResult.formState;
99
+ this.data = data;
100
+ }
101
+ /**
102
+ * Needed to connect real react-hook-form to this mobx wrapper
103
+ *
104
+ * This is used in useMobxForm
105
+ */
106
+ connect(formResult) {
107
+ if (!this.isConnected) {
108
+ this.isConnected = true;
109
+ this.syncForm(formResult);
110
+ const formWatchSubscription = formResult.watch((values) => {
111
+ this.syncForm(formResult, values);
112
+ });
113
+ this.abortController.signal.addEventListener('abort', formWatchSubscription.unsubscribe);
114
+ }
115
+ return {
116
+ ...formResult,
117
+ onReset: () => this.handleReset(),
118
+ onSubmit: formResult.handleSubmit(
119
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
120
+ // @ts-expect-error
121
+ (...args) => this.handleSubmit(...args), (...args) => this.handleSubmitFailed(...args)),
122
+ handleSubmit: (onValid, onInvalid) => {
123
+ return formResult.handleSubmit(
124
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
125
+ // @ts-expect-error
126
+ async (...args) => {
127
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
128
+ // @ts-expect-error
129
+ await this.handleSubmit(...args);
130
+ await onValid(...args);
131
+ }, async (...args) => {
132
+ await this.handleSubmitFailed(...args);
133
+ await onInvalid?.(...args);
134
+ });
135
+ },
136
+ };
138
137
  }
139
138
  /**
140
139
  * @deprecated use destroy();
141
140
  */
142
141
  dispose() {
143
- this.destroy();
142
+ this.abortController.abort();
144
143
  }
145
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-react-hook-form",
3
- "version": "2.0.15",
3
+ "version": "2.0.17",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "js2me",
@@ -8,10 +8,10 @@
8
8
  "peerDependencies": {
9
9
  "mobx": "^6.12.4",
10
10
  "mobx-react-lite": "^4.0.7",
11
- "react": "^18.3.1"
11
+ "react": "^18.3.1",
12
+ "react-hook-form": "^7.53.0"
12
13
  },
13
14
  "dependencies": {
14
- "react-hook-form": "^7.55.0-next.3",
15
15
  "disposer-util": "^1.0.11",
16
16
  "linked-abort-controller": "^1.0.1",
17
17
  "lodash-es": "^4.17.21",
@@ -20,10 +20,9 @@
20
20
  "devDependencies": {
21
21
  "@types/lodash-es": "4.17.12",
22
22
  "eslint": "8.57.0",
23
- "js2me-eslint-config": "1.0.7",
23
+ "js2me-eslint-config": "1.0.4",
24
24
  "js2me-exports-post-build-script": "2.0.10",
25
25
  "rimraf": "6.0.1",
26
- "prettier": "3.3.3",
27
26
  "typescript": "5.6.2"
28
27
  },
29
28
  "exports": {