@strictly/react-form 0.0.18 → 0.0.20
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/.out/core/mobx/form_model.d.ts +12 -9
- package/.out/core/mobx/form_model.js +103 -137
- package/.out/core/mobx/hooks.js +3 -4
- package/.out/core/mobx/merge_field_adapters_with_two_way_converter.d.ts +1 -1
- package/.out/core/mobx/merge_field_adapters_with_validators.js +5 -1
- package/.out/core/mobx/specs/form_model.tests.js +28 -23
- package/.out/mantine/create_fields_view.d.ts +2 -1
- package/.out/mantine/hooks.d.ts +6 -5
- package/.out/mantine/specs/checkbox_hooks.stories.d.ts +5 -2
- package/.out/mantine/specs/checkbox_hooks.stories.js +3 -2
- package/.out/mantine/specs/text_input_hooks.stories.d.ts +3 -2
- package/.out/mantine/specs/text_input_hooks.stories.js +3 -2
- package/.out/mantine/types.d.ts +3 -3
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.out/util/partial.d.ts +5 -2
- package/.out/util/specs/partial.tests.d.ts +1 -0
- package/.out/util/specs/partial.tests.js +8 -0
- package/.turbo/turbo-build.log +7 -7
- package/.turbo/turbo-check-types.log +2 -2
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/core/mobx/form_model.ts +95 -157
- package/core/mobx/hooks.tsx +6 -5
- package/core/mobx/merge_field_adapters_with_two_way_converter.ts +2 -1
- package/core/mobx/merge_field_adapters_with_validators.ts +1 -1
- package/core/mobx/specs/form_model.tests.ts +39 -27
- package/dist/index.cjs +93 -139
- package/dist/index.d.cts +28 -21
- package/dist/index.d.ts +28 -21
- package/dist/index.js +92 -139
- package/mantine/create_fields_view.tsx +8 -4
- package/mantine/hooks.tsx +23 -15
- package/mantine/specs/checkbox_hooks.stories.tsx +7 -1
- package/mantine/specs/text_input_hooks.stories.tsx +8 -1
- package/mantine/types.ts +12 -4
- package/package.json +12 -12
- package/util/partial.tsx +8 -1
- package/util/specs/partial.tests.tsx +21 -0
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
} from '@strictly/define'
|
|
17
17
|
import {
|
|
18
18
|
type FieldAdapter,
|
|
19
|
+
type ToOfFieldAdapter,
|
|
19
20
|
} from 'core/mobx/field_adapter'
|
|
20
21
|
import {
|
|
21
22
|
adapterFromTwoWayConverter,
|
|
@@ -24,6 +25,7 @@ import {
|
|
|
24
25
|
import {
|
|
25
26
|
type FlattenedTypePathsToAdaptersOf,
|
|
26
27
|
FormModel,
|
|
28
|
+
Validation,
|
|
27
29
|
type ValuePathsToAdaptersOf,
|
|
28
30
|
} from 'core/mobx/form_model'
|
|
29
31
|
import { mergeAdaptersWithValidators } from 'core/mobx/merge_field_adapters_with_validators'
|
|
@@ -46,7 +48,7 @@ import {
|
|
|
46
48
|
const IS_NAN_ERROR = 1
|
|
47
49
|
|
|
48
50
|
const originalIntegerToStringAdapter = adapterFromTwoWayConverter(
|
|
49
|
-
new IntegerToStringConverter(IS_NAN_ERROR),
|
|
51
|
+
new IntegerToStringConverter<typeof IS_NAN_ERROR, string, unknown>(IS_NAN_ERROR),
|
|
50
52
|
prototypingFieldValueFactory(0),
|
|
51
53
|
)
|
|
52
54
|
|
|
@@ -69,6 +71,13 @@ class TestFormModel<
|
|
|
69
71
|
valuePath,
|
|
70
72
|
}
|
|
71
73
|
}
|
|
74
|
+
|
|
75
|
+
setFieldValueAndValidate<K extends keyof ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths>>(
|
|
76
|
+
valuePath: K,
|
|
77
|
+
value: ToOfFieldAdapter<ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths>[K]>,
|
|
78
|
+
) {
|
|
79
|
+
this.setFieldValue(valuePath, value, Validation.Always)
|
|
80
|
+
}
|
|
72
81
|
}
|
|
73
82
|
|
|
74
83
|
describe('all', function () {
|
|
@@ -510,7 +519,7 @@ describe('all', function () {
|
|
|
510
519
|
$: integerToStringAdapter,
|
|
511
520
|
} as const
|
|
512
521
|
const originalValue: ValueOfType<typeof typeDef> = 2
|
|
513
|
-
let model:
|
|
522
|
+
let model: TestFormModel<
|
|
514
523
|
typeof typeDef,
|
|
515
524
|
ValueToTypePathsOfType<typeof typeDef>,
|
|
516
525
|
typeof adapters
|
|
@@ -570,9 +579,9 @@ describe('all', function () {
|
|
|
570
579
|
|
|
571
580
|
describe('conversion succeeds, but validation fails', function () {
|
|
572
581
|
const newValue = -1
|
|
573
|
-
const errorCode =
|
|
582
|
+
const errorCode = IS_NAN_ERROR
|
|
574
583
|
beforeEach(function () {
|
|
575
|
-
integerToStringAdapter.revert?.
|
|
584
|
+
integerToStringAdapter.revert?.mockReturnValue({
|
|
576
585
|
type: UnreliableFieldConversionType.Failure,
|
|
577
586
|
error: errorCode,
|
|
578
587
|
value: [newValue],
|
|
@@ -632,7 +641,7 @@ describe('all', function () {
|
|
|
632
641
|
'$.*': integerToStringAdapter,
|
|
633
642
|
} as const
|
|
634
643
|
let originalValue: ValueOfType<typeof typeDef>
|
|
635
|
-
let model:
|
|
644
|
+
let model: TestFormModel<
|
|
636
645
|
typeof typeDef,
|
|
637
646
|
ValueToTypePathsOfType<typeof typeDef>,
|
|
638
647
|
typeof converters
|
|
@@ -800,9 +809,11 @@ describe('all', function () {
|
|
|
800
809
|
describe('addListItem', function () {
|
|
801
810
|
describe('adds default to start of the list', function () {
|
|
802
811
|
beforeEach(function () {
|
|
803
|
-
model.
|
|
804
|
-
model.
|
|
805
|
-
model.
|
|
812
|
+
model.setFieldValue('$.0', 'x')
|
|
813
|
+
model.setFieldValue('$.1', '3')
|
|
814
|
+
model.setFieldValue('$.2', 'z')
|
|
815
|
+
model.validateAll()
|
|
816
|
+
|
|
806
817
|
model.addListItem('$', null, 0)
|
|
807
818
|
})
|
|
808
819
|
|
|
@@ -822,7 +833,7 @@ describe('all', function () {
|
|
|
822
833
|
],
|
|
823
834
|
[
|
|
824
835
|
'$.1',
|
|
825
|
-
'
|
|
836
|
+
'x',
|
|
826
837
|
],
|
|
827
838
|
[
|
|
828
839
|
'$.2',
|
|
@@ -830,7 +841,7 @@ describe('all', function () {
|
|
|
830
841
|
],
|
|
831
842
|
[
|
|
832
843
|
'$.3',
|
|
833
|
-
'
|
|
844
|
+
'z',
|
|
834
845
|
],
|
|
835
846
|
] as const)('it reports the value of field %s as %s', function (path, fieldValue) {
|
|
836
847
|
expect(model.fields[path]?.value).toBe(fieldValue)
|
|
@@ -843,15 +854,15 @@ describe('all', function () {
|
|
|
843
854
|
],
|
|
844
855
|
[
|
|
845
856
|
'$.1',
|
|
846
|
-
|
|
857
|
+
IS_NAN_ERROR,
|
|
847
858
|
],
|
|
848
859
|
[
|
|
849
860
|
'$.2',
|
|
850
|
-
|
|
861
|
+
undefined,
|
|
851
862
|
],
|
|
852
863
|
[
|
|
853
864
|
'$.3',
|
|
854
|
-
|
|
865
|
+
IS_NAN_ERROR,
|
|
855
866
|
],
|
|
856
867
|
] as const)('it reports the error of field %s', function (path, error) {
|
|
857
868
|
expect(model.fields[path]?.error).toBe(error)
|
|
@@ -895,9 +906,10 @@ describe('all', function () {
|
|
|
895
906
|
|
|
896
907
|
describe('removeListItem', function () {
|
|
897
908
|
beforeEach(function () {
|
|
898
|
-
model.
|
|
899
|
-
model.
|
|
900
|
-
model.
|
|
909
|
+
model.setFieldValue('$.0', 'x')
|
|
910
|
+
model.setFieldValue('$.1', '3')
|
|
911
|
+
model.setFieldValue('$.2', 'z')
|
|
912
|
+
model.validateAll()
|
|
901
913
|
})
|
|
902
914
|
|
|
903
915
|
describe('remove first item', function () {
|
|
@@ -916,11 +928,11 @@ describe('all', function () {
|
|
|
916
928
|
expect(model.fields).toEqual({
|
|
917
929
|
'$.0': expect.objectContaining({
|
|
918
930
|
value: '3',
|
|
919
|
-
error:
|
|
931
|
+
error: undefined,
|
|
920
932
|
}),
|
|
921
933
|
'$.1': expect.objectContaining({
|
|
922
|
-
value: '
|
|
923
|
-
error:
|
|
934
|
+
value: 'z',
|
|
935
|
+
error: IS_NAN_ERROR,
|
|
924
936
|
}),
|
|
925
937
|
})
|
|
926
938
|
})
|
|
@@ -941,12 +953,12 @@ describe('all', function () {
|
|
|
941
953
|
it('updates the field values and errors', function () {
|
|
942
954
|
expect(model.fields).toEqual({
|
|
943
955
|
'$.0': expect.objectContaining({
|
|
944
|
-
value: '
|
|
945
|
-
error:
|
|
956
|
+
value: 'x',
|
|
957
|
+
error: IS_NAN_ERROR,
|
|
946
958
|
}),
|
|
947
959
|
'$.1': expect.objectContaining({
|
|
948
|
-
value: '
|
|
949
|
-
error:
|
|
960
|
+
value: 'z',
|
|
961
|
+
error: IS_NAN_ERROR,
|
|
950
962
|
}),
|
|
951
963
|
})
|
|
952
964
|
})
|
|
@@ -964,8 +976,8 @@ describe('all', function () {
|
|
|
964
976
|
it('updates the field values and errors', function () {
|
|
965
977
|
expect(model.fields).toEqual({
|
|
966
978
|
'$.0': expect.objectContaining({
|
|
967
|
-
value: '
|
|
968
|
-
error:
|
|
979
|
+
value: 'z',
|
|
980
|
+
error: IS_NAN_ERROR,
|
|
969
981
|
}),
|
|
970
982
|
})
|
|
971
983
|
})
|
|
@@ -987,7 +999,7 @@ describe('all', function () {
|
|
|
987
999
|
} as const
|
|
988
1000
|
type ValueToTypePaths = ValueToTypePathsOfType<typeof type>
|
|
989
1001
|
let originalValue: ValueOfType<typeof type>
|
|
990
|
-
let model:
|
|
1002
|
+
let model: TestFormModel<
|
|
991
1003
|
typeof type,
|
|
992
1004
|
ValueToTypePaths,
|
|
993
1005
|
typeof adapters
|
|
@@ -1201,7 +1213,7 @@ describe('all', function () {
|
|
|
1201
1213
|
}
|
|
1202
1214
|
})
|
|
1203
1215
|
describe('create mode', () => {
|
|
1204
|
-
let model:
|
|
1216
|
+
let model: TestFormModel<
|
|
1205
1217
|
typeof typeDef,
|
|
1206
1218
|
JsonPaths,
|
|
1207
1219
|
typeof adapters
|
package/dist/index.cjs
CHANGED
|
@@ -97,6 +97,7 @@ __export(index_exports, {
|
|
|
97
97
|
SelectStringConverter: () => SelectStringConverter,
|
|
98
98
|
TrimmingStringConverter: () => TrimmingStringConverter,
|
|
99
99
|
UnreliableFieldConversionType: () => UnreliableFieldConversionType,
|
|
100
|
+
Validation: () => Validation,
|
|
100
101
|
adapter: () => adapter,
|
|
101
102
|
adapterFromPrototype: () => adapterFromPrototype,
|
|
102
103
|
adapterFromTwoWayConverter: () => adapterFromTwoWayConverter,
|
|
@@ -356,19 +357,27 @@ function listAdapter() {
|
|
|
356
357
|
var import_base2 = require("@strictly/base");
|
|
357
358
|
var import_define = require("@strictly/define");
|
|
358
359
|
var import_mobx = require("mobx");
|
|
359
|
-
var
|
|
360
|
-
|
|
360
|
+
var Validation = /* @__PURE__ */ ((Validation2) => {
|
|
361
|
+
Validation2[Validation2["Changed"] = 1] = "Changed";
|
|
362
|
+
Validation2[Validation2["Always"] = 2] = "Always";
|
|
363
|
+
return Validation2;
|
|
364
|
+
})(Validation || {});
|
|
365
|
+
var _accessors_dec, _knownFields_dec, _fields_dec, _validation_dec, _fieldOverrides_dec, _value_dec, _init, _value, _fieldOverrides, _validation;
|
|
366
|
+
_value_dec = [import_mobx.observable.ref], _fieldOverrides_dec = [import_mobx.observable.shallow], _validation_dec = [import_mobx.observable.shallow], _fields_dec = [import_mobx.computed], _knownFields_dec = [import_mobx.computed], _accessors_dec = [import_mobx.computed];
|
|
361
367
|
var FormModel = class {
|
|
362
368
|
constructor(type, originalValue, adapters, mode) {
|
|
363
369
|
this.type = type;
|
|
364
|
-
this.originalValue = originalValue;
|
|
365
370
|
this.adapters = adapters;
|
|
366
371
|
this.mode = mode;
|
|
367
372
|
__runInitializers(_init, 5, this);
|
|
368
373
|
__privateAdd(this, _value, __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
|
|
369
374
|
__privateAdd(this, _fieldOverrides, __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
|
|
370
|
-
__privateAdd(this,
|
|
375
|
+
__privateAdd(this, _validation, __runInitializers(_init, 16, this, {})), __runInitializers(_init, 19, this);
|
|
371
376
|
__publicField(this, "flattenedTypeDefs");
|
|
377
|
+
// cannot be type safe
|
|
378
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
379
|
+
__publicField(this, "originalValues");
|
|
380
|
+
this.originalValues = (0, import_define.flattenValuesOfType)(type, originalValue);
|
|
372
381
|
this.value = (0, import_define.mobxCopy)(type, originalValue);
|
|
373
382
|
this.flattenedTypeDefs = (0, import_define.flattenTypesOfType)(type);
|
|
374
383
|
const conversions = (0, import_define.flattenValueTo)(
|
|
@@ -460,12 +469,14 @@ var FormModel = class {
|
|
|
460
469
|
}
|
|
461
470
|
const {
|
|
462
471
|
convert,
|
|
463
|
-
create
|
|
472
|
+
create,
|
|
473
|
+
revert
|
|
464
474
|
} = adapter2;
|
|
465
475
|
const fieldOverride = this.fieldOverrides[valuePath];
|
|
466
476
|
const accessor = this.getAccessorForValuePath(valuePath);
|
|
467
477
|
const fieldTypeDef = this.flattenedTypeDefs[typePath];
|
|
468
478
|
const context = this.toContext(this.value, valuePath);
|
|
479
|
+
const defaultValue = create(valuePath, context);
|
|
469
480
|
const {
|
|
470
481
|
value,
|
|
471
482
|
required,
|
|
@@ -473,14 +484,43 @@ var FormModel = class {
|
|
|
473
484
|
} = convert(
|
|
474
485
|
accessor != null ? accessor.value : fieldTypeDef != null ? (0, import_define.mobxCopy)(
|
|
475
486
|
fieldTypeDef,
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
) : create(valuePath, context),
|
|
487
|
+
defaultValue
|
|
488
|
+
) : defaultValue,
|
|
479
489
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
480
490
|
valuePath,
|
|
481
491
|
context
|
|
482
492
|
);
|
|
483
|
-
|
|
493
|
+
let error = void 0;
|
|
494
|
+
const displayedValue = fieldOverride != null ? fieldOverride[0] : value;
|
|
495
|
+
const validation = this.validation[valuePath];
|
|
496
|
+
switch (validation) {
|
|
497
|
+
case void 0:
|
|
498
|
+
break;
|
|
499
|
+
case 1 /* Changed */:
|
|
500
|
+
if (revert != null) {
|
|
501
|
+
const originalValue = valuePath in this.originalValues ? this.originalValues[valuePath] : defaultValue;
|
|
502
|
+
const { value: originalDisplayedValue } = convert(originalValue, valuePath, context);
|
|
503
|
+
if (displayedValue !== originalDisplayedValue) {
|
|
504
|
+
const revertResult = revert(displayedValue, valuePath, context);
|
|
505
|
+
if ((revertResult == null ? void 0 : revertResult.type) === 1 /* Failure */) {
|
|
506
|
+
;
|
|
507
|
+
({ error } = revertResult);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
case 2 /* Always */:
|
|
513
|
+
{
|
|
514
|
+
const revertResult = revert == null ? void 0 : revert(displayedValue, valuePath, context);
|
|
515
|
+
if ((revertResult == null ? void 0 : revertResult.type) === 1 /* Failure */) {
|
|
516
|
+
;
|
|
517
|
+
({ error } = revertResult);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
break;
|
|
521
|
+
default:
|
|
522
|
+
throw new import_base2.UnreachableError(validation);
|
|
523
|
+
}
|
|
484
524
|
return {
|
|
485
525
|
value: fieldOverride != null ? fieldOverride[0] : value,
|
|
486
526
|
error,
|
|
@@ -515,11 +555,8 @@ var FormModel = class {
|
|
|
515
555
|
typePath(valuePath) {
|
|
516
556
|
return (0, import_define.valuePathToTypePath)(this.type, valuePath, true);
|
|
517
557
|
}
|
|
518
|
-
|
|
519
|
-
return this.internalSetFieldValue(valuePath, value,
|
|
520
|
-
}
|
|
521
|
-
setFieldValue(valuePath, value) {
|
|
522
|
-
return this.internalSetFieldValue(valuePath, value, false);
|
|
558
|
+
setFieldValue(valuePath, value, validation = this.validation[valuePath]) {
|
|
559
|
+
return this.internalSetFieldValue(valuePath, value, validation);
|
|
523
560
|
}
|
|
524
561
|
addListItem(valuePath, elementValue = null, index) {
|
|
525
562
|
const listValuePath = valuePath;
|
|
@@ -578,9 +615,9 @@ var FormModel = class {
|
|
|
578
615
|
const fieldOverride = this.fieldOverrides[fromJsonPath];
|
|
579
616
|
delete this.fieldOverrides[fromJsonPath];
|
|
580
617
|
this.fieldOverrides[toJsonPath] = fieldOverride;
|
|
581
|
-
const
|
|
582
|
-
delete this.
|
|
583
|
-
this.
|
|
618
|
+
const validation = this.validation[fromJsonPath];
|
|
619
|
+
delete this.validation[fromJsonPath];
|
|
620
|
+
this.validation[toJsonPath] = validation;
|
|
584
621
|
});
|
|
585
622
|
accessor.set(newList);
|
|
586
623
|
delete this.fieldOverrides[listValuePath];
|
|
@@ -644,33 +681,34 @@ var FormModel = class {
|
|
|
644
681
|
const fieldOverride = this.fieldOverrides[fromJsonPath];
|
|
645
682
|
delete this.fieldOverrides[fromJsonPath];
|
|
646
683
|
this.fieldOverrides[toJsonPath] = fieldOverride;
|
|
647
|
-
const
|
|
648
|
-
delete this.
|
|
649
|
-
this.
|
|
684
|
+
const validation = this.validation[fromJsonPath];
|
|
685
|
+
delete this.validation[fromJsonPath];
|
|
686
|
+
this.validation[toJsonPath] = validation;
|
|
650
687
|
});
|
|
651
688
|
accessor.set(newList);
|
|
652
689
|
delete this.fieldOverrides[listValuePath];
|
|
653
690
|
});
|
|
654
691
|
});
|
|
655
692
|
}
|
|
656
|
-
internalSetFieldValue(valuePath, value,
|
|
693
|
+
internalSetFieldValue(valuePath, value, validation) {
|
|
657
694
|
const { revert } = this.getAdapterForValuePath(valuePath);
|
|
658
695
|
(0, import_base2.assertExists)(revert, "setting value not supported {}", valuePath);
|
|
659
696
|
const conversion = revert(value, valuePath, this.toContext(this.value, valuePath));
|
|
660
697
|
const accessor = this.getAccessorForValuePath(valuePath);
|
|
661
698
|
return (0, import_mobx.runInAction)(() => {
|
|
662
699
|
this.fieldOverrides[valuePath] = [value];
|
|
700
|
+
if (validation != null) {
|
|
701
|
+
this.validation[valuePath] = validation;
|
|
702
|
+
} else {
|
|
703
|
+
delete this.validation[valuePath];
|
|
704
|
+
}
|
|
663
705
|
switch (conversion.type) {
|
|
664
706
|
case 1 /* Failure */:
|
|
665
|
-
if (displayValidation) {
|
|
666
|
-
this.errors[valuePath] = conversion.error;
|
|
667
|
-
}
|
|
668
707
|
if (conversion.value != null && accessor != null) {
|
|
669
708
|
accessor.set(conversion.value[0]);
|
|
670
709
|
}
|
|
671
710
|
return false;
|
|
672
711
|
case 0 /* Success */:
|
|
673
|
-
delete this.errors[valuePath];
|
|
674
712
|
accessor == null ? void 0 : accessor.set(conversion.value);
|
|
675
713
|
return true;
|
|
676
714
|
default:
|
|
@@ -682,7 +720,7 @@ var FormModel = class {
|
|
|
682
720
|
const fieldOverride = this.fieldOverrides[valuePath];
|
|
683
721
|
if (fieldOverride != null) {
|
|
684
722
|
(0, import_mobx.runInAction)(() => {
|
|
685
|
-
delete this.
|
|
723
|
+
delete this.validation[valuePath];
|
|
686
724
|
});
|
|
687
725
|
}
|
|
688
726
|
}
|
|
@@ -704,11 +742,12 @@ var FormModel = class {
|
|
|
704
742
|
const key = valuePath;
|
|
705
743
|
(0, import_mobx.runInAction)(() => {
|
|
706
744
|
this.fieldOverrides[key] = [displayValue];
|
|
745
|
+
delete this.validation[key];
|
|
707
746
|
});
|
|
708
747
|
}
|
|
709
748
|
clearAll(value) {
|
|
710
749
|
(0, import_mobx.runInAction)(() => {
|
|
711
|
-
this.
|
|
750
|
+
this.validation = {};
|
|
712
751
|
this.fieldOverrides = {};
|
|
713
752
|
this.value = (0, import_define.mobxCopy)(this.type, value);
|
|
714
753
|
});
|
|
@@ -718,123 +757,37 @@ var FormModel = class {
|
|
|
718
757
|
const keys = new Set(Object.keys(values));
|
|
719
758
|
return keys.has(valuePath);
|
|
720
759
|
}
|
|
721
|
-
validateField(valuePath,
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
const fieldOverride = this.fieldOverrides[valuePath];
|
|
728
|
-
const accessor = this.getAccessorForValuePath(valuePath);
|
|
729
|
-
const context = this.toContext(this.value, valuePath);
|
|
730
|
-
const {
|
|
731
|
-
value: storedValue
|
|
732
|
-
} = convert(
|
|
733
|
-
accessor != null ? accessor.value : create(valuePath, context),
|
|
734
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
735
|
-
valuePath,
|
|
736
|
-
context
|
|
737
|
-
);
|
|
738
|
-
const value = fieldOverride != null ? fieldOverride[0] : storedValue;
|
|
739
|
-
const dirty = storedValue !== value;
|
|
740
|
-
(0, import_base2.assertExists)(revert, "changing field directly not supported {}", valuePath);
|
|
741
|
-
if (ignoreDefaultValue) {
|
|
742
|
-
const {
|
|
743
|
-
value: defaultDisplayValue
|
|
744
|
-
} = convert(create(valuePath, context), valuePath, context);
|
|
745
|
-
if (defaultDisplayValue === value) {
|
|
746
|
-
return true;
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
const conversion = revert(value, valuePath, context);
|
|
750
|
-
return (0, import_mobx.runInAction)(() => {
|
|
751
|
-
switch (conversion.type) {
|
|
752
|
-
case 1 /* Failure */:
|
|
753
|
-
this.errors[valuePath] = conversion.error;
|
|
754
|
-
if (conversion.value != null && accessor != null && dirty) {
|
|
755
|
-
accessor.set(conversion.value[0]);
|
|
756
|
-
}
|
|
757
|
-
return false;
|
|
758
|
-
case 0 /* Success */:
|
|
759
|
-
delete this.errors[valuePath];
|
|
760
|
-
if (accessor != null && dirty) {
|
|
761
|
-
accessor.set(conversion.value);
|
|
762
|
-
}
|
|
763
|
-
return true;
|
|
764
|
-
default:
|
|
765
|
-
throw new import_base2.UnreachableError(conversion);
|
|
766
|
-
}
|
|
760
|
+
validateField(valuePath, validation = Math.max(
|
|
761
|
+
this.mode === "create" ? 2 /* Always */ : 1 /* Changed */,
|
|
762
|
+
((_a) => (_a = this.validation[valuePath]) != null ? _a : 1 /* Changed */)()
|
|
763
|
+
)) {
|
|
764
|
+
(0, import_mobx.runInAction)(() => {
|
|
765
|
+
this.validation[valuePath] = validation;
|
|
767
766
|
});
|
|
767
|
+
return this.fields[valuePath].error == null;
|
|
768
768
|
}
|
|
769
|
-
validateAll(
|
|
770
|
-
const accessors = (0, import_base2.toArray)(this.accessors)
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
return accessors.reduce(
|
|
776
|
-
(success, [
|
|
777
|
-
valuePath,
|
|
778
|
-
accessor
|
|
779
|
-
]) => {
|
|
780
|
-
const adapterPath = valuePath;
|
|
781
|
-
const adapter2 = this.maybeGetAdapterForValuePath(adapterPath);
|
|
782
|
-
if (adapter2 == null) {
|
|
783
|
-
return success;
|
|
784
|
-
}
|
|
785
|
-
const {
|
|
786
|
-
convert,
|
|
787
|
-
revert
|
|
788
|
-
} = adapter2;
|
|
789
|
-
if (revert == null) {
|
|
790
|
-
return success;
|
|
791
|
-
}
|
|
792
|
-
const fieldOverride = this.fieldOverrides[adapterPath];
|
|
793
|
-
const context = this.toContext(this.value, valuePath);
|
|
794
|
-
const {
|
|
795
|
-
value: storedValue
|
|
796
|
-
} = convert(accessor.value, valuePath, context);
|
|
797
|
-
const value = fieldOverride != null ? fieldOverride[0] : storedValue;
|
|
798
|
-
const dirty = fieldOverride != null && fieldOverride[0] !== storedValue;
|
|
799
|
-
const needsValidation = force || !(valuePath in flattenedOriginalValues) || storedValue !== convert(
|
|
800
|
-
flattenedOriginalValues[valuePath],
|
|
801
|
-
valuePath,
|
|
802
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
803
|
-
this.toContext(this.originalValue, valuePath)
|
|
804
|
-
).value;
|
|
805
|
-
if (needsValidation) {
|
|
806
|
-
const conversion = revert(value, valuePath, context);
|
|
807
|
-
switch (conversion.type) {
|
|
808
|
-
case 1 /* Failure */:
|
|
809
|
-
this.errors[adapterPath] = conversion.error;
|
|
810
|
-
if (conversion.value != null && dirty) {
|
|
811
|
-
accessor.set(conversion.value[0]);
|
|
812
|
-
}
|
|
813
|
-
return false;
|
|
814
|
-
case 0 /* Success */:
|
|
815
|
-
if (dirty) {
|
|
816
|
-
accessor.set(conversion.value);
|
|
817
|
-
}
|
|
818
|
-
delete this.errors[adapterPath];
|
|
819
|
-
return success;
|
|
820
|
-
default:
|
|
821
|
-
throw new import_base2.UnreachableError(conversion);
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
return success;
|
|
825
|
-
},
|
|
826
|
-
true
|
|
827
|
-
);
|
|
769
|
+
validateAll(validation = this.mode === "create" ? 2 /* Always */ : 1 /* Changed */) {
|
|
770
|
+
const accessors = (0, import_base2.toArray)(this.accessors);
|
|
771
|
+
(0, import_mobx.runInAction)(() => {
|
|
772
|
+
accessors.forEach(([valuePath]) => {
|
|
773
|
+
this.validation[valuePath] = validation;
|
|
774
|
+
});
|
|
828
775
|
});
|
|
776
|
+
return accessors.every(
|
|
777
|
+
([valuePath]) => {
|
|
778
|
+
const field = this.fields[valuePath];
|
|
779
|
+
return (field == null ? void 0 : field.error) == null;
|
|
780
|
+
}
|
|
781
|
+
);
|
|
829
782
|
}
|
|
830
783
|
};
|
|
831
784
|
_init = __decoratorStart(null);
|
|
832
785
|
_value = new WeakMap();
|
|
833
786
|
_fieldOverrides = new WeakMap();
|
|
834
|
-
|
|
787
|
+
_validation = new WeakMap();
|
|
835
788
|
__decorateElement(_init, 4, "value", _value_dec, FormModel, _value);
|
|
836
789
|
__decorateElement(_init, 4, "fieldOverrides", _fieldOverrides_dec, FormModel, _fieldOverrides);
|
|
837
|
-
__decorateElement(_init, 4, "
|
|
790
|
+
__decorateElement(_init, 4, "validation", _validation_dec, FormModel, _validation);
|
|
838
791
|
__decorateElement(_init, 2, "fields", _fields_dec, FormModel);
|
|
839
792
|
__decorateElement(_init, 2, "knownFields", _knownFields_dec, FormModel);
|
|
840
793
|
__decorateElement(_init, 2, "accessors", _accessors_dec, FormModel);
|
|
@@ -848,8 +801,7 @@ function useDefaultMobxFormHooks(model, {
|
|
|
848
801
|
} = {}) {
|
|
849
802
|
const onFieldValueChange = (0, import_react.useCallback)(
|
|
850
803
|
function(path, value) {
|
|
851
|
-
model.
|
|
852
|
-
model.setFieldValue(path, value);
|
|
804
|
+
model.setFieldValue(path, value, null);
|
|
853
805
|
},
|
|
854
806
|
[model]
|
|
855
807
|
);
|
|
@@ -869,7 +821,7 @@ function useDefaultMobxFormHooks(model, {
|
|
|
869
821
|
function(path) {
|
|
870
822
|
setTimeout(function() {
|
|
871
823
|
if (model.isValuePathActive(path)) {
|
|
872
|
-
model.validateField(path
|
|
824
|
+
model.validateField(path);
|
|
873
825
|
}
|
|
874
826
|
}, 100);
|
|
875
827
|
},
|
|
@@ -958,10 +910,11 @@ function mergeAdaptersWithValidators(adapters, validators) {
|
|
|
958
910
|
readonly: readonly1 || readonly2
|
|
959
911
|
};
|
|
960
912
|
}
|
|
961
|
-
acc[key] =
|
|
913
|
+
acc[key] = {
|
|
914
|
+
create: adapter2.create.bind(adapter2),
|
|
962
915
|
convert,
|
|
963
916
|
revert: adapter2.revert && revert
|
|
964
|
-
}
|
|
917
|
+
};
|
|
965
918
|
return acc;
|
|
966
919
|
},
|
|
967
920
|
{}
|
|
@@ -1986,6 +1939,7 @@ function mergeValidators(validators1, validators2) {
|
|
|
1986
1939
|
SelectStringConverter,
|
|
1987
1940
|
TrimmingStringConverter,
|
|
1988
1941
|
UnreliableFieldConversionType,
|
|
1942
|
+
Validation,
|
|
1989
1943
|
adapter,
|
|
1990
1944
|
adapterFromPrototype,
|
|
1991
1945
|
adapterFromTwoWayConverter,
|