@strictly/react-form 0.0.16 → 0.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.
- package/.out/core/mobx/form_model.d.ts +6 -2
- package/.out/core/mobx/form_model.js +53 -22
- package/.out/core/mobx/specs/form_model.tests.js +62 -12
- package/.out/core/props.d.ts +2 -0
- package/.out/mantine/hooks.js +1 -1
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-check-types.log +1 -1
- package/core/mobx/form_model.ts +49 -23
- package/core/mobx/specs/form_model.tests.ts +103 -0
- package/core/props.ts +4 -0
- package/dist/index.cjs +44 -22
- package/dist/index.d.cts +20 -15
- package/dist/index.d.ts +20 -15
- package/dist/index.js +44 -22
- package/mantine/hooks.tsx +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -329,15 +329,17 @@ import {
|
|
|
329
329
|
var _accessors_dec, _knownFields_dec, _fields_dec, _errors_dec, _fieldOverrides_dec, _value_dec, _init, _value, _fieldOverrides, _errors;
|
|
330
330
|
_value_dec = [observable.ref], _fieldOverrides_dec = [observable.shallow], _errors_dec = [observable.shallow], _fields_dec = [computed], _knownFields_dec = [computed], _accessors_dec = [computed];
|
|
331
331
|
var FormModel = class {
|
|
332
|
-
constructor(type,
|
|
332
|
+
constructor(type, originalValue, adapters, mode) {
|
|
333
333
|
this.type = type;
|
|
334
|
+
this.originalValue = originalValue;
|
|
334
335
|
this.adapters = adapters;
|
|
336
|
+
this.mode = mode;
|
|
335
337
|
__runInitializers(_init, 5, this);
|
|
336
338
|
__privateAdd(this, _value, __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
|
|
337
339
|
__privateAdd(this, _fieldOverrides, __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
|
|
338
340
|
__privateAdd(this, _errors, __runInitializers(_init, 16, this, {})), __runInitializers(_init, 19, this);
|
|
339
341
|
__publicField(this, "flattenedTypeDefs");
|
|
340
|
-
this.value = mobxCopy(type,
|
|
342
|
+
this.value = mobxCopy(type, originalValue);
|
|
341
343
|
this.flattenedTypeDefs = flattenTypesOfType(type);
|
|
342
344
|
const conversions = flattenValueTo(
|
|
343
345
|
type,
|
|
@@ -345,7 +347,7 @@ var FormModel = class {
|
|
|
345
347
|
() => {
|
|
346
348
|
},
|
|
347
349
|
(_t, fieldValue, _setter, typePath, valuePath) => {
|
|
348
|
-
const contextValue = this.toContext(
|
|
350
|
+
const contextValue = this.toContext(originalValue, valuePath);
|
|
349
351
|
const adapter2 = this.adapters[typePath];
|
|
350
352
|
if (adapter2 == null) {
|
|
351
353
|
return;
|
|
@@ -364,6 +366,16 @@ var FormModel = class {
|
|
|
364
366
|
return v && [v.value];
|
|
365
367
|
});
|
|
366
368
|
}
|
|
369
|
+
get forceMutableFields() {
|
|
370
|
+
switch (this.mode) {
|
|
371
|
+
case "create":
|
|
372
|
+
return true;
|
|
373
|
+
case "edit":
|
|
374
|
+
return false;
|
|
375
|
+
default:
|
|
376
|
+
return this.mode;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
367
379
|
get fields() {
|
|
368
380
|
return new Proxy(
|
|
369
381
|
this.knownFields,
|
|
@@ -442,7 +454,7 @@ var FormModel = class {
|
|
|
442
454
|
return {
|
|
443
455
|
value: fieldOverride != null ? fieldOverride[0] : value,
|
|
444
456
|
error,
|
|
445
|
-
readonly,
|
|
457
|
+
readonly: readonly && !this.forceMutableFields,
|
|
446
458
|
required
|
|
447
459
|
};
|
|
448
460
|
}
|
|
@@ -724,10 +736,11 @@ var FormModel = class {
|
|
|
724
736
|
}
|
|
725
737
|
});
|
|
726
738
|
}
|
|
727
|
-
validateAll() {
|
|
739
|
+
validateAll(force = this.mode === "create") {
|
|
728
740
|
const accessors = toArray(this.accessors).toSorted(function([a], [b]) {
|
|
729
741
|
return a.length - b.length;
|
|
730
742
|
});
|
|
743
|
+
const flattenedOriginalValues = flattenValuesOfType(this.type, this.originalValue);
|
|
731
744
|
return runInAction(() => {
|
|
732
745
|
return accessors.reduce(
|
|
733
746
|
(success, [
|
|
@@ -753,23 +766,32 @@ var FormModel = class {
|
|
|
753
766
|
} = convert(accessor.value, valuePath, context);
|
|
754
767
|
const value = fieldOverride != null ? fieldOverride[0] : storedValue;
|
|
755
768
|
const dirty = fieldOverride != null && fieldOverride[0] !== storedValue;
|
|
756
|
-
const
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
769
|
+
const needsValidation = force || !(valuePath in flattenedOriginalValues) || storedValue !== convert(
|
|
770
|
+
flattenedOriginalValues[valuePath],
|
|
771
|
+
valuePath,
|
|
772
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
773
|
+
this.toContext(this.originalValue, valuePath)
|
|
774
|
+
).value;
|
|
775
|
+
if (needsValidation) {
|
|
776
|
+
const conversion = revert(value, valuePath, context);
|
|
777
|
+
switch (conversion.type) {
|
|
778
|
+
case 1 /* Failure */:
|
|
779
|
+
this.errors[adapterPath] = conversion.error;
|
|
780
|
+
if (conversion.value != null && dirty) {
|
|
781
|
+
accessor.set(conversion.value[0]);
|
|
782
|
+
}
|
|
783
|
+
return false;
|
|
784
|
+
case 0 /* Success */:
|
|
785
|
+
if (dirty) {
|
|
786
|
+
accessor.set(conversion.value);
|
|
787
|
+
}
|
|
788
|
+
delete this.errors[adapterPath];
|
|
789
|
+
return success;
|
|
790
|
+
default:
|
|
791
|
+
throw new UnreachableError2(conversion);
|
|
792
|
+
}
|
|
772
793
|
}
|
|
794
|
+
return success;
|
|
773
795
|
},
|
|
774
796
|
true
|
|
775
797
|
);
|
|
@@ -1762,7 +1784,7 @@ function useMantineFormFields({
|
|
|
1762
1784
|
function() {
|
|
1763
1785
|
return new MantineFormImpl(fields);
|
|
1764
1786
|
},
|
|
1765
|
-
// handled separately below
|
|
1787
|
+
// fields handled separately below
|
|
1766
1788
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1767
1789
|
[]
|
|
1768
1790
|
);
|
package/mantine/hooks.tsx
CHANGED