mobx-react-hook-form 2.1.2 → 2.1.3

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.
@@ -0,0 +1,28 @@
1
+ import { DeepMap, DeepPartial, FieldErrors, FieldValues, FormState } from 'react-hook-form';
2
+ type FormValues<TFieldValues extends FieldValues> = {
3
+ values: TFieldValues;
4
+ };
5
+ type MobxFormStateUpdate<TFieldValues extends FieldValues> = Omit<Partial<MobxFormState<TFieldValues>>, 'update'>;
6
+ export type MobxFormStateReadonly<TFieldValues extends FieldValues> = Readonly<Omit<MobxFormState<TFieldValues>, 'update'>>;
7
+ export declare class MobxFormState<TFieldValues extends FieldValues = FieldValues> implements FormState<TFieldValues>, FormValues<TFieldValues> {
8
+ values: TFieldValues;
9
+ isDirty: boolean;
10
+ isLoading: boolean;
11
+ isSubmitted: boolean;
12
+ isSubmitSuccessful: boolean;
13
+ isSubmitting: boolean;
14
+ isValidating: boolean;
15
+ isValid: boolean;
16
+ disabled: boolean;
17
+ submitCount: number;
18
+ defaultValues: Readonly<DeepPartial<TFieldValues>> | undefined;
19
+ dirtyFields: Partial<Readonly<DeepMap<DeepPartial<TFieldValues>, boolean>>>;
20
+ touchedFields: Partial<Readonly<DeepMap<DeepPartial<TFieldValues>, boolean>>>;
21
+ validatingFields: Partial<Readonly<DeepMap<DeepPartial<TFieldValues>, boolean>>>;
22
+ errors: FieldErrors<TFieldValues>;
23
+ isReady: boolean;
24
+ constructor(initialState?: MobxFormStateUpdate<TFieldValues>);
25
+ update({ values, errors, ...simpleProperties }: MobxFormStateUpdate<TFieldValues>): void;
26
+ }
27
+ export {};
28
+ //# sourceMappingURL=mobx-form-state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mobx-form-state.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form-state.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,OAAO,EACP,WAAW,EACX,WAAW,EACX,WAAW,EACX,SAAS,E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,CACtD,CAAM;IACP,MAAM,EAAE,WAAW,CAAC,YAAY,CAAC,CAAM;IACvC,OAAO,EAAE,OAAO,CAAS;gBAEb,YAAY,CAAC,EAAE,mBAAmB,CAAC,YAAY,CAAC;IA0B5D,MAAM,CAAC,EACL,MAAM,EACN,MAAM,EACN,GAAG,gBAAgB,EACpB,EAAE,mBAAmB,CAAC,YAAY,CAAC;CAkCrC"}
@@ -0,0 +1,76 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
3
+ import { action, makeObservable, observable } from 'mobx';
4
+ export class MobxFormState {
5
+ values;
6
+ isDirty = false;
7
+ isLoading = false;
8
+ isSubmitted = false;
9
+ isSubmitSuccessful = false;
10
+ isSubmitting = false;
11
+ isValidating = false;
12
+ isValid = false;
13
+ disabled = false;
14
+ submitCount = 0;
15
+ defaultValues;
16
+ dirtyFields = {};
17
+ touchedFields = {};
18
+ validatingFields = {};
19
+ errors = {};
20
+ isReady = false;
21
+ constructor(initialState) {
22
+ if (initialState) {
23
+ Object.assign(this, initialState);
24
+ }
25
+ observable.deep(this, 'values');
26
+ observable.ref(this, 'isDirty');
27
+ observable.ref(this, 'isLoading');
28
+ observable.ref(this, 'isSubmitted');
29
+ observable.ref(this, 'isSubmitSuccessful');
30
+ observable.ref(this, 'isSubmitting');
31
+ observable.ref(this, 'isValidating');
32
+ observable.ref(this, 'isValid');
33
+ observable.ref(this, 'disabled');
34
+ observable.ref(this, 'submitCount');
35
+ observable.ref(this, 'isReady');
36
+ observable.deep(this, 'defaultValues');
37
+ observable.deep(this, 'dirtyFields');
38
+ observable.deep(this, 'touchedFields');
39
+ observable.deep(this, 'validatingFields');
40
+ observable.deep(this, 'errors');
41
+ action(this, 'update');
42
+ makeObservable(this);
43
+ }
44
+ update({ values, errors, ...simpleProperties }) {
45
+ Object.entries(simpleProperties).forEach(([key, value]) => {
46
+ if (value != null) {
47
+ // @ts-ignore
48
+ this[key] = value;
49
+ }
50
+ });
51
+ if (errors) {
52
+ const currentErrorsSet = new Set(Object.keys(this.errors));
53
+ const newErrors = Object.keys(errors);
54
+ for (const errorField of newErrors) {
55
+ if (currentErrorsSet.has(errorField)) {
56
+ currentErrorsSet.delete(errorField);
57
+ // @ts-ignore
58
+ Object.assign(this.errors[errorField], errors[errorField]);
59
+ }
60
+ else {
61
+ // @ts-ignore
62
+ this.errors[errorField] = errors[errorField];
63
+ }
64
+ }
65
+ currentErrorsSet.forEach((errorField) => {
66
+ // @ts-ignore
67
+ delete this.errors[errorField];
68
+ });
69
+ }
70
+ else {
71
+ this.errors = {};
72
+ }
73
+ // @ts-ignore
74
+ this.values = values ?? {};
75
+ }
76
+ }
@@ -1,5 +1,5 @@
1
- import { createFormControl, FieldValues, FormState, SubmitErrorHandler, SubmitHandler, UseFormProps } from 'react-hook-form';
2
- import { Maybe } from 'yummies/utils/types';
1
+ import { createFormControl, FieldValues, SubmitErrorHandler, SubmitHandler, UseFormProps } from 'react-hook-form';
2
+ import { MobxFormStateReadonly } from './mobx-form-state.js';
3
3
  import { MobxFormParams } from './mobx-form.types.js';
4
4
  export declare class MobxForm<TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues = TFieldValues> {
5
5
  private config;
@@ -19,13 +19,12 @@ export declare class MobxForm<TFieldValues extends FieldValues = FieldValues, TC
19
19
  /**
20
20
  * form state received from form.formState
21
21
  */
22
- state: Maybe<Partial<FormState<TFieldValues>> & {
23
- values: TFieldValues;
24
- }>;
22
+ private _state;
23
+ get state(): MobxFormStateReadonly<TFieldValues>;
25
24
  /**
26
25
  * Raw data received from form.getValues()
27
26
  */
28
- data: TFieldValues;
27
+ get data(): TFieldValues;
29
28
  constructor(config: MobxFormParams<TFieldValues, TContext, TTransformedValues>);
30
29
  destroy(): void;
31
30
  submit: (event?: any) => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"mobx-form.d.ts","sourceRoot":"","sources":["../../src/mobx-form/mobx-form.ts"],"names":[],"mapp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;IAIf,MAAM,GAAU,QAAQ,GAAG,mBAOzB;IAEF,KAAK,aAEH;CACH"}
@@ -1,7 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment */
2
2
  import { LinkedAbortController } from 'linked-abort-controller';
3
- import { action, makeObservable, observable, runInAction } from 'mobx';
3
+ import { action, computed, makeObservable, observable } from 'mobx';
4
4
  import { createFormControl, } from 'react-hook-form';
5
+ import { MobxFormState } from './mobx-form-state.js';
5
6
  export class MobxForm {
6
7
  config;
7
8
  abortController;
@@ -33,30 +34,39 @@ export class MobxForm {
33
34
  /**
34
35
  * form state received from form.formState
35
36
  */
36
- state;
37
+ _state;
38
+ get state() {
39
+ return this._state;
40
+ }
37
41
  /**
38
42
  * Raw data received from form.getValues()
39
43
  */
40
- data;
44
+ get data() {
45
+ return this._state.values;
46
+ }
41
47
  constructor(config) {
42
48
  this.config = config;
43
49
  this.params = config;
44
50
  this.abortController = new LinkedAbortController(config.abortSignal);
45
51
  this.form = createFormControl(config);
46
- this.data = this.form.getValues();
47
- this.state = null;
52
+ this._state = new MobxFormState({
53
+ values: this.form.getValues(),
54
+ defaultValues: config.defaultValues || {},
55
+ });
48
56
  this.params = config;
49
57
  const subscription = this.form.subscribe({
58
+ formState: {
59
+ values: true,
60
+ errors: true,
61
+ isValid: true,
62
+ isDirty: true,
63
+ isValidating: true,
64
+ dirtyFields: true,
65
+ touchedFields: true,
66
+ validatingFields: true,
67
+ },
50
68
  callback: (rawFormState) => {
51
- runInAction(() => {
52
- if (this.state) {
53
- Object.assign(this.state, rawFormState);
54
- }
55
- else {
56
- this.state = rawFormState;
57
- }
58
- Object.assign(this.data, rawFormState.values);
59
- });
69
+ this._state.update(rawFormState);
60
70
  },
61
71
  });
62
72
  this.abortController.signal.addEventListener('abort', () => {
@@ -65,11 +75,9 @@ export class MobxForm {
65
75
  this.form = null;
66
76
  // @ts-ignore
67
77
  this.data = null;
68
- // @ts-ignore
69
- this.state = null;
70
78
  });
71
- observable.deep(this, 'state');
72
- observable.deep(this, 'data');
79
+ computed.struct(this, 'data');
80
+ computed.struct(this, 'state');
73
81
  observable.ref(this, 'params');
74
82
  observable.ref(this, 'form');
75
83
  action.bound(this, 'setParams');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-react-hook-form",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -31,6 +31,7 @@
31
31
  "eslint": "^8.57.1",
32
32
  "js2me-eslint-config": "^1.0.7",
33
33
  "js2me-exports-post-build-script": "^3.0.1",
34
+ "nodemon": "^3.1.10",
34
35
  "rimraf": "^6.0.1",
35
36
  "typescript": "^5.8.3"
36
37
  },
@@ -45,6 +46,11 @@
45
46
  "default": "./mobx-form/index.js",
46
47
  "types": "./mobx-form/index.d.ts"
47
48
  },
49
+ "./mobx-form/mobx-form-state": {
50
+ "import": "./mobx-form/mobx-form-state.js",
51
+ "default": "./mobx-form/mobx-form-state.js",
52
+ "types": "./mobx-form/mobx-form-state.d.ts"
53
+ },
48
54
  "./mobx-form/mobx-form": {
49
55
  "import": "./mobx-form/mobx-form.js",
50
56
  "default": "./mobx-form/mobx-form.js",
@@ -62,6 +68,7 @@
62
68
  "check": "eslint . --fix",
63
69
  "prebuild": "npm run clean && npm run check",
64
70
  "build": "tsc && node ./post-build.mjs",
71
+ "build:watch": "npm run build && nodemon --delay 0.5 --watch src --ext ts,tsx --exec \"tsc && node ./post-build.mjs\"",
65
72
  "pub": "PUBLISH=true pnpm run build",
66
73
  "pub:next": "PUBLISH=true PUBLISH_TAG=next PUBLISH_FORCE=true pnpm run build",
67
74
  "pub:patch": "PUBLISH=true PUBLISH_VERSION=patch pnpm run build",