@strictly/react-form 0.0.16 → 0.0.18

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/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, value, adapters) {
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, value);
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(value, valuePath);
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 conversion = revert(value, valuePath, context);
757
- switch (conversion.type) {
758
- case 1 /* Failure */:
759
- this.errors[adapterPath] = conversion.error;
760
- if (conversion.value != null && dirty) {
761
- accessor.set(conversion.value[0]);
762
- }
763
- return false;
764
- case 0 /* Success */:
765
- if (dirty) {
766
- accessor.set(conversion.value);
767
- }
768
- delete this.errors[adapterPath];
769
- return success;
770
- default:
771
- throw new UnreachableError2(conversion);
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
@@ -103,7 +103,7 @@ export function useMantineFormFields<
103
103
  function () {
104
104
  return new MantineFormImpl(fields)
105
105
  },
106
- // handled separately below
106
+ // fields handled separately below
107
107
  // eslint-disable-next-line react-hooks/exhaustive-deps
108
108
  [],
109
109
  )
package/package.json CHANGED
@@ -70,7 +70,7 @@
70
70
  "test:watch": "vitest"
71
71
  },
72
72
  "type": "module",
73
- "version": "0.0.16",
73
+ "version": "0.0.18",
74
74
  "exports": {
75
75
  ".": {
76
76
  "import": {