@strictly/react-form 0.0.14 → 0.0.16

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.
Files changed (42) hide show
  1. package/.out/core/mobx/field_adapter_builder.d.ts +1 -1
  2. package/.out/core/mobx/form_model.d.ts +7 -5
  3. package/.out/core/mobx/form_model.js +27 -23
  4. package/.out/core/mobx/merge_field_adapters_with_validators.d.ts +1 -1
  5. package/.out/core/mobx/specs/form_model.tests.js +18 -8
  6. package/.out/core/mobx/types.d.ts +4 -4
  7. package/.out/index.d.ts +0 -1
  8. package/.out/index.js +0 -1
  9. package/.out/mantine/create_field_view.d.ts +20 -0
  10. package/.out/mantine/create_field_view.js +54 -0
  11. package/.out/mantine/create_list.js +3 -2
  12. package/.out/mantine/hooks.d.ts +4 -1
  13. package/.out/mantine/hooks.js +13 -1
  14. package/.out/mantine/specs/field_view_hooks.stories.d.ts +12 -0
  15. package/.out/mantine/specs/field_view_hooks.stories.js +61 -0
  16. package/.out/mantine/specs/field_view_hooks.tests.d.ts +1 -0
  17. package/.out/mantine/specs/field_view_hooks.tests.js +12 -0
  18. package/.out/tsconfig.tsbuildinfo +1 -1
  19. package/.out/types/merge_validators.d.ts +1 -1
  20. package/.turbo/turbo-build.log +8 -8
  21. package/.turbo/turbo-check-types.log +1 -1
  22. package/core/mobx/field_adapter_builder.ts +2 -2
  23. package/core/mobx/form_model.ts +48 -23
  24. package/core/mobx/merge_field_adapters_with_validators.ts +1 -1
  25. package/core/mobx/specs/form_model.tests.ts +28 -11
  26. package/core/mobx/types.ts +4 -4
  27. package/dist/index.cjs +124 -70
  28. package/dist/index.d.cts +31 -28
  29. package/dist/index.d.ts +31 -28
  30. package/dist/index.js +121 -62
  31. package/index.ts +0 -1
  32. package/mantine/create_field_view.tsx +94 -0
  33. package/mantine/create_list.tsx +9 -2
  34. package/mantine/hooks.tsx +18 -1
  35. package/mantine/specs/__snapshots__/field_view_hooks.tests.tsx.snap +153 -0
  36. package/mantine/specs/field_view_hooks.stories.tsx +112 -0
  37. package/mantine/specs/field_view_hooks.tests.tsx +15 -0
  38. package/package.json +1 -1
  39. package/types/merge_validators.ts +2 -2
  40. package/.out/mantine/field_view.d.ts +0 -18
  41. package/.out/mantine/field_view.js +0 -16
  42. package/mantine/field_view.tsx +0 -39
package/dist/index.cjs CHANGED
@@ -89,7 +89,6 @@ __export(index_exports, {
89
89
  AbstractSelectValueTypeConverter: () => AbstractSelectValueTypeConverter,
90
90
  DefaultErrorRenderer: () => DefaultErrorRenderer,
91
91
  Empty: () => Empty,
92
- FieldView: () => FieldView,
93
92
  FormModel: () => FormModel,
94
93
  IntegerToStringConverter: () => IntegerToStringConverter,
95
94
  NullableToBooleanConverter: () => NullableToBooleanConverter,
@@ -357,8 +356,8 @@ function listAdapter() {
357
356
  var import_base2 = require("@strictly/base");
358
357
  var import_define = require("@strictly/define");
359
358
  var import_mobx = require("mobx");
360
- var _accessors_dec, _knownFields_dec, _fields_dec, _context_dec, _errors_dec, _fieldOverrides_dec, _value_dec, _init, _value, _fieldOverrides, _errors;
361
- _value_dec = [import_mobx.observable.ref], _fieldOverrides_dec = [import_mobx.observable.shallow], _errors_dec = [import_mobx.observable.shallow], _context_dec = [import_mobx.computed.struct], _fields_dec = [import_mobx.computed], _knownFields_dec = [import_mobx.computed], _accessors_dec = [import_mobx.computed];
359
+ var _accessors_dec, _knownFields_dec, _fields_dec, _errors_dec, _fieldOverrides_dec, _value_dec, _init, _value, _fieldOverrides, _errors;
360
+ _value_dec = [import_mobx.observable.ref], _fieldOverrides_dec = [import_mobx.observable.shallow], _errors_dec = [import_mobx.observable.shallow], _fields_dec = [import_mobx.computed], _knownFields_dec = [import_mobx.computed], _accessors_dec = [import_mobx.computed];
362
361
  var FormModel = class {
363
362
  constructor(type, value, adapters) {
364
363
  this.type = type;
@@ -370,13 +369,13 @@ var FormModel = class {
370
369
  __publicField(this, "flattenedTypeDefs");
371
370
  this.value = (0, import_define.mobxCopy)(type, value);
372
371
  this.flattenedTypeDefs = (0, import_define.flattenTypesOfType)(type);
373
- const contextValue = this.toContext(value);
374
372
  const conversions = (0, import_define.flattenValueTo)(
375
373
  type,
376
374
  this.value,
377
375
  () => {
378
376
  },
379
- (_t, value2, _setter, typePath, valuePath) => {
377
+ (_t, fieldValue, _setter, typePath, valuePath) => {
378
+ const contextValue = this.toContext(value, valuePath);
380
379
  const adapter2 = this.adapters[typePath];
381
380
  if (adapter2 == null) {
382
381
  return;
@@ -388,16 +387,13 @@ var FormModel = class {
388
387
  if (revert == null) {
389
388
  return;
390
389
  }
391
- return convert(value2, valuePath, contextValue);
390
+ return convert(fieldValue, valuePath, contextValue);
392
391
  }
393
392
  );
394
393
  this.fieldOverrides = (0, import_base2.map)(conversions, function(_k, v) {
395
394
  return v && [v.value];
396
395
  });
397
396
  }
398
- get context() {
399
- return this.toContext(this.value);
400
- }
401
397
  get fields() {
402
398
  return new Proxy(
403
399
  this.knownFields,
@@ -457,6 +453,7 @@ var FormModel = class {
457
453
  const fieldOverride = this.fieldOverrides[valuePath];
458
454
  const accessor = this.getAccessorForValuePath(valuePath);
459
455
  const fieldTypeDef = this.flattenedTypeDefs[typePath];
456
+ const context = this.toContext(this.value, valuePath);
460
457
  const {
461
458
  value,
462
459
  required,
@@ -465,11 +462,11 @@ var FormModel = class {
465
462
  accessor != null ? accessor.value : fieldTypeDef != null ? (0, import_define.mobxCopy)(
466
463
  fieldTypeDef,
467
464
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
468
- create(valuePath, this.context)
469
- ) : create(valuePath, this.context),
465
+ create(valuePath, context)
466
+ ) : create(valuePath, context),
470
467
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
471
468
  valuePath,
472
- this.context
469
+ context
473
470
  );
474
471
  const error = this.errors[valuePath];
475
472
  return {
@@ -527,7 +524,9 @@ var FormModel = class {
527
524
  const element = elementValue != null ? elementValue[0] : elementAdapter.create(
528
525
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
529
526
  elementTypePath,
530
- this.context
527
+ // TODO what can we use for the value path here?
528
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
529
+ this.toContext(this.value, valuePath)
531
530
  );
532
531
  const originalList = accessor.value;
533
532
  const newList = [
@@ -645,7 +644,7 @@ var FormModel = class {
645
644
  internalSetFieldValue(valuePath, value, displayValidation) {
646
645
  const { revert } = this.getAdapterForValuePath(valuePath);
647
646
  (0, import_base2.assertExists)(revert, "setting value not supported {}", valuePath);
648
- const conversion = revert(value, valuePath, this.context);
647
+ const conversion = revert(value, valuePath, this.toContext(this.value, valuePath));
649
648
  const accessor = this.getAccessorForValuePath(valuePath);
650
649
  return (0, import_mobx.runInAction)(() => {
651
650
  this.fieldOverrides[valuePath] = [value];
@@ -685,10 +684,11 @@ var FormModel = class {
685
684
  convert,
686
685
  create
687
686
  } = adapter2;
688
- const value = create(valuePath, this.context);
687
+ const context = this.toContext(this.value, valuePath);
688
+ const value = create(valuePath, context);
689
689
  const {
690
690
  value: displayValue
691
- } = convert(value, valuePath, this.context);
691
+ } = convert(value, valuePath, context);
692
692
  const key = valuePath;
693
693
  (0, import_mobx.runInAction)(() => {
694
694
  this.fieldOverrides[key] = [displayValue];
@@ -714,13 +714,14 @@ var FormModel = class {
714
714
  } = this.getAdapterForValuePath(valuePath);
715
715
  const fieldOverride = this.fieldOverrides[valuePath];
716
716
  const accessor = this.getAccessorForValuePath(valuePath);
717
+ const context = this.toContext(this.value, valuePath);
717
718
  const {
718
719
  value: storedValue
719
720
  } = convert(
720
- accessor != null ? accessor.value : create(valuePath, this.context),
721
+ accessor != null ? accessor.value : create(valuePath, context),
721
722
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
722
723
  valuePath,
723
- this.context
724
+ context
724
725
  );
725
726
  const value = fieldOverride != null ? fieldOverride[0] : storedValue;
726
727
  const dirty = storedValue !== value;
@@ -728,12 +729,12 @@ var FormModel = class {
728
729
  if (ignoreDefaultValue) {
729
730
  const {
730
731
  value: defaultDisplayValue
731
- } = convert(create(valuePath, this.context), valuePath, this.context);
732
+ } = convert(create(valuePath, context), valuePath, context);
732
733
  if (defaultDisplayValue === value) {
733
734
  return true;
734
735
  }
735
736
  }
736
- const conversion = revert(value, valuePath, this.context);
737
+ const conversion = revert(value, valuePath, context);
737
738
  return (0, import_mobx.runInAction)(() => {
738
739
  switch (conversion.type) {
739
740
  case 1 /* Failure */:
@@ -776,12 +777,13 @@ var FormModel = class {
776
777
  return success;
777
778
  }
778
779
  const fieldOverride = this.fieldOverrides[adapterPath];
780
+ const context = this.toContext(this.value, valuePath);
779
781
  const {
780
782
  value: storedValue
781
- } = convert(accessor.value, valuePath, this.context);
783
+ } = convert(accessor.value, valuePath, context);
782
784
  const value = fieldOverride != null ? fieldOverride[0] : storedValue;
783
785
  const dirty = fieldOverride != null && fieldOverride[0] !== storedValue;
784
- const conversion = revert(value, valuePath, this.context);
786
+ const conversion = revert(value, valuePath, context);
785
787
  switch (conversion.type) {
786
788
  case 1 /* Failure */:
787
789
  this.errors[adapterPath] = conversion.error;
@@ -811,7 +813,6 @@ _errors = new WeakMap();
811
813
  __decorateElement(_init, 4, "value", _value_dec, FormModel, _value);
812
814
  __decorateElement(_init, 4, "fieldOverrides", _fieldOverrides_dec, FormModel, _fieldOverrides);
813
815
  __decorateElement(_init, 4, "errors", _errors_dec, FormModel, _errors);
814
- __decorateElement(_init, 2, "context", _context_dec, FormModel);
815
816
  __decorateElement(_init, 2, "fields", _fields_dec, FormModel);
816
817
  __decorateElement(_init, 2, "knownFields", _knownFields_dec, FormModel);
817
818
  __decorateElement(_init, 2, "accessors", _accessors_dec, FormModel);
@@ -1167,49 +1168,21 @@ function DefaultErrorRenderer({
1167
1168
  return JSON.stringify(error);
1168
1169
  }
1169
1170
 
1170
- // mantine/field_view.tsx
1171
- var import_mobx_react = require("mobx-react");
1172
-
1173
- // util/empty.tsx
1174
- function Empty() {
1175
- return null;
1176
- }
1177
-
1178
- // mantine/field_view.tsx
1179
- var import_jsx_runtime = require("react/jsx-runtime");
1180
- function FieldView({
1181
- fields,
1182
- valuePath,
1183
- children
1184
- }) {
1185
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_mobx_react.Observer, { children: () => {
1186
- const {
1187
- value,
1188
- error
1189
- } = fields[valuePath];
1190
- return children({
1191
- value,
1192
- error,
1193
- ErrorSink: Empty
1194
- });
1195
- } });
1196
- }
1197
-
1198
1171
  // mantine/hooks.tsx
1199
1172
  var import_core = require("@mantine/core");
1200
1173
  var import_base6 = require("@strictly/base");
1201
1174
  var import_mobx2 = require("mobx");
1202
- var import_react4 = require("react");
1175
+ var import_react6 = require("react");
1203
1176
 
1204
1177
  // util/partial.tsx
1205
- var import_mobx_react2 = require("mobx-react");
1178
+ var import_mobx_react = require("mobx-react");
1206
1179
  var import_react2 = require("react");
1207
- var import_jsx_runtime2 = require("react/jsx-runtime");
1180
+ var import_jsx_runtime = require("react/jsx-runtime");
1208
1181
  function createSimplePartialComponent(Component, curriedProps) {
1209
1182
  return (0, import_react2.forwardRef)(
1210
1183
  function(exposedProps, ref) {
1211
1184
  const C = Component;
1212
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1185
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1213
1186
  C,
1214
1187
  __spreadValues(__spreadValues({
1215
1188
  ref
@@ -1249,7 +1222,7 @@ function createPartialComponent(Component, curriedPropsSource, additionalPropKey
1249
1222
  ]
1250
1223
  );
1251
1224
  const curriedProps = curriedPropsSource(additionalProps);
1252
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1225
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1253
1226
  C,
1254
1227
  __spreadValues(__spreadValues({
1255
1228
  ref
@@ -1285,7 +1258,7 @@ function createPartialObserverComponent(Component, curriedPropsSource, additiona
1285
1258
  );
1286
1259
  }
1287
1260
  function createUnsafePartialObserverComponent(Component, curriedPropsSource, additionalPropKeys = []) {
1288
- return (0, import_mobx_react2.observer)(
1261
+ return (0, import_mobx_react.observer)(
1289
1262
  (0, import_react2.forwardRef)(
1290
1263
  function(props, ref) {
1291
1264
  const C = Component;
@@ -1316,7 +1289,7 @@ function createUnsafePartialObserverComponent(Component, curriedPropsSource, add
1316
1289
  ]
1317
1290
  );
1318
1291
  const curriedProps = curriedPropsSource(additionalProps);
1319
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1292
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1320
1293
  C,
1321
1294
  __spreadValues(__spreadValues({
1322
1295
  ref
@@ -1347,7 +1320,7 @@ function usePartialObserverComponent(curriedPropsSource, deps, Component, additi
1347
1320
  }
1348
1321
 
1349
1322
  // mantine/create_checkbox.tsx
1350
- var import_jsx_runtime3 = require("react/jsx-runtime");
1323
+ var import_jsx_runtime2 = require("react/jsx-runtime");
1351
1324
  function createCheckbox(valuePath, Checkbox) {
1352
1325
  const onChange = (e) => {
1353
1326
  var _a;
@@ -1384,7 +1357,7 @@ function createCheckbox(valuePath, Checkbox) {
1384
1357
  checked: value,
1385
1358
  disabled: readonly,
1386
1359
  required,
1387
- error: error && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorRenderer, { error }),
1360
+ error: error && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorRenderer, { error }),
1388
1361
  onChange,
1389
1362
  onFocus,
1390
1363
  onBlur,
@@ -1398,6 +1371,78 @@ function createCheckbox(valuePath, Checkbox) {
1398
1371
  );
1399
1372
  }
1400
1373
 
1374
+ // mantine/create_field_view.tsx
1375
+ var import_mobx_react2 = require("mobx-react");
1376
+ var import_react3 = require("react");
1377
+
1378
+ // util/empty.tsx
1379
+ function Empty() {
1380
+ return null;
1381
+ }
1382
+
1383
+ // mantine/create_field_view.tsx
1384
+ var import_jsx_runtime3 = require("react/jsx-runtime");
1385
+ function FieldView({
1386
+ valuePath,
1387
+ form,
1388
+ children
1389
+ }) {
1390
+ const onFocus = (0, import_react3.useCallback)(() => {
1391
+ var _a;
1392
+ (_a = form.onFieldFocus) == null ? void 0 : _a.call(form, valuePath);
1393
+ }, [
1394
+ form,
1395
+ valuePath
1396
+ ]);
1397
+ const onBlur = (0, import_react3.useCallback)(() => {
1398
+ var _a;
1399
+ (_a = form.onFieldBlur) == null ? void 0 : _a.call(form, valuePath);
1400
+ }, [
1401
+ form,
1402
+ valuePath
1403
+ ]);
1404
+ const onValueChange = (0, import_react3.useCallback)((value) => {
1405
+ var _a;
1406
+ (_a = form.onFieldValueChange) == null ? void 0 : _a.call(form, valuePath, value);
1407
+ }, [
1408
+ form,
1409
+ valuePath
1410
+ ]);
1411
+ const onSubmit = (0, import_react3.useCallback)(() => {
1412
+ var _a;
1413
+ (_a = form.onFieldSubmit) == null ? void 0 : _a.call(form, valuePath);
1414
+ }, [
1415
+ form,
1416
+ valuePath
1417
+ ]);
1418
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_mobx_react2.Observer, { children: () => {
1419
+ const {
1420
+ value,
1421
+ error
1422
+ } = form.fields[valuePath];
1423
+ return children({
1424
+ value,
1425
+ error,
1426
+ ErrorSink: Empty,
1427
+ onFocus,
1428
+ onBlur,
1429
+ onValueChange,
1430
+ onSubmit
1431
+ });
1432
+ } });
1433
+ }
1434
+ function createFieldView(valuePath) {
1435
+ return (props) => {
1436
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1437
+ FieldView,
1438
+ __spreadValues({
1439
+ form: this,
1440
+ valuePath
1441
+ }, props)
1442
+ );
1443
+ };
1444
+ }
1445
+
1401
1446
  // mantine/create_fields_view.tsx
1402
1447
  var import_define6 = require("@strictly/define");
1403
1448
  var import_mobx_react3 = require("mobx-react");
@@ -1465,12 +1510,12 @@ function createFieldsView(valuePath, FieldsView, observableProps) {
1465
1510
 
1466
1511
  // mantine/create_form.tsx
1467
1512
  var import_mobx_react4 = require("mobx-react");
1468
- var import_react3 = require("react");
1513
+ var import_react4 = require("react");
1469
1514
  var import_jsx_runtime5 = require("react/jsx-runtime");
1470
1515
  function createForm(valuePath, Form, observableProps) {
1471
1516
  return (0, import_mobx_react4.observer)((props) => {
1472
1517
  const { value } = observableProps.fields[valuePath];
1473
- const onValueChange = (0, import_react3.useCallback)((value2) => {
1518
+ const onValueChange = (0, import_react4.useCallback)((value2) => {
1474
1519
  observableProps.onFieldValueChange(valuePath, value2);
1475
1520
  }, []);
1476
1521
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
@@ -1484,6 +1529,7 @@ function createForm(valuePath, Form, observableProps) {
1484
1529
  }
1485
1530
 
1486
1531
  // mantine/create_list.tsx
1532
+ var import_react5 = require("react");
1487
1533
  var import_jsx_runtime6 = require("react/jsx-runtime");
1488
1534
  function createList(valuePath, List) {
1489
1535
  const propSource = () => {
@@ -1502,7 +1548,7 @@ function DefaultList({
1502
1548
  }) {
1503
1549
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_jsx_runtime6.Fragment, { children: values.map(function(value, index) {
1504
1550
  const valuePath = `${listPath}.${index}`;
1505
- return children(valuePath, value, index);
1551
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_react5.Fragment, { children: children(valuePath, value, index) }, valuePath);
1506
1552
  }) });
1507
1553
  }
1508
1554
 
@@ -1701,8 +1747,9 @@ function useMantineFormFields({
1701
1747
  onFieldFocus,
1702
1748
  onFieldSubmit,
1703
1749
  fields
1750
+ // should use FieldView rather than observing fields directly from here
1704
1751
  }) {
1705
- const form = (0, import_react4.useMemo)(
1752
+ const form = (0, import_react6.useMemo)(
1706
1753
  function() {
1707
1754
  return new MantineFormImpl(fields);
1708
1755
  },
@@ -1710,7 +1757,7 @@ function useMantineFormFields({
1710
1757
  // eslint-disable-next-line react-hooks/exhaustive-deps
1711
1758
  []
1712
1759
  );
1713
- (0, import_react4.useEffect)(function() {
1760
+ (0, import_react6.useEffect)(function() {
1714
1761
  (0, import_mobx2.runInAction)(function() {
1715
1762
  form.fields = fields;
1716
1763
  });
@@ -1718,25 +1765,25 @@ function useMantineFormFields({
1718
1765
  form,
1719
1766
  fields
1720
1767
  ]);
1721
- (0, import_react4.useEffect)(function() {
1768
+ (0, import_react6.useEffect)(function() {
1722
1769
  form.onFieldValueChange = onFieldValueChange;
1723
1770
  }, [
1724
1771
  form,
1725
1772
  onFieldValueChange
1726
1773
  ]);
1727
- (0, import_react4.useEffect)(function() {
1774
+ (0, import_react6.useEffect)(function() {
1728
1775
  form.onFieldBlur = onFieldBlur;
1729
1776
  }, [
1730
1777
  form,
1731
1778
  onFieldBlur
1732
1779
  ]);
1733
- (0, import_react4.useEffect)(function() {
1780
+ (0, import_react6.useEffect)(function() {
1734
1781
  form.onFieldFocus = onFieldFocus;
1735
1782
  }, [
1736
1783
  form,
1737
1784
  onFieldFocus
1738
1785
  ]);
1739
- (0, import_react4.useEffect)(function() {
1786
+ (0, import_react6.useEffect)(function() {
1740
1787
  form.onFieldSubmit = onFieldSubmit;
1741
1788
  }, [
1742
1789
  form,
@@ -1769,6 +1816,9 @@ var MantineFormImpl = class {
1769
1816
  __publicField(this, "listCache", new import_base6.Cache(
1770
1817
  createList.bind(this)
1771
1818
  ));
1819
+ __publicField(this, "fieldViewCache", new import_base6.Cache(
1820
+ createFieldView.bind(this)
1821
+ ));
1772
1822
  __publicField(this, "fieldsViewCache", new import_base6.Cache(
1773
1823
  createFieldsView.bind(this)
1774
1824
  ));
@@ -1836,6 +1886,11 @@ var MantineFormImpl = class {
1836
1886
  DefaultList
1837
1887
  );
1838
1888
  }
1889
+ fieldView(valuePath) {
1890
+ return this.fieldViewCache.retrieveOrCreate(
1891
+ valuePath
1892
+ );
1893
+ }
1839
1894
  fieldsView(valuePath, FieldsView) {
1840
1895
  return this.fieldsViewCache.retrieveOrCreate(
1841
1896
  valuePath,
@@ -1901,7 +1956,6 @@ function mergeValidators(validators1, validators2) {
1901
1956
  AbstractSelectValueTypeConverter,
1902
1957
  DefaultErrorRenderer,
1903
1958
  Empty,
1904
- FieldView,
1905
1959
  FormModel,
1906
1960
  IntegerToStringConverter,
1907
1961
  NullableToBooleanConverter,
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Maybe, ElementOfArray, StringConcatOf, StringKeyOf as StringKeyOf$1, ExhaustiveArrayOfUnion, FriendlyExhaustiveArrayOfUnion } from '@strictly/base';
2
2
  import { Type, ValueOfType, ReadonlyTypeOfType, FlattenedTypesOfType, ListTypeDef, FlattenedValuesOfType, MobxValueOfType, Accessor, Validator, UnionTypeDef, ValueTypesOfDiscriminatedUnion, LiteralTypeDef } from '@strictly/define';
3
- import { ValueOf, SimplifyDeep, ReadonlyDeep, StringKeyOf, Simplify } from 'type-fest';
3
+ import { ValueOf, SimplifyDeep, ReadonlyDeep, UnionToIntersection, StringKeyOf, Simplify } from 'type-fest';
4
4
  import { ComponentType, ComponentProps, ForwardRefExoticComponent, PropsWithoutRef, DependencyList } from 'react';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { CheckboxProps, PillProps, RadioProps, RadioGroupProps, TextInputProps, SelectProps } from '@mantine/core';
@@ -73,7 +73,7 @@ declare function adapterFromPrototype<From, To, ValuePath extends string, Contex
73
73
  declare function adapterFromPrototype<From, To, E, ValuePath extends string, Context>(converter: TwoWayFieldConverter<From, To, E, ValuePath, Context>, prototype: From): FieldAdapterBuilder<From, To, E, ValuePath, Context>;
74
74
  declare function identityAdapter<V, ValuePath extends string, Context>(prototype: V, required?: boolean): FieldAdapterBuilder<V, V, never, ValuePath, Context>;
75
75
  declare function trimmingStringAdapter<ValuePath extends string, Context>(): FieldAdapterBuilder<string, string, never, ValuePath, Context>;
76
- declare function listAdapter<E, ValuePath extends string, Context>(): FieldAdapterBuilder<readonly E[], readonly E[], never, ValuePath, Context>;
76
+ declare function listAdapter<E, ValuePath extends string = string, Context = unknown>(): FieldAdapterBuilder<readonly E[], readonly E[], never, ValuePath, Context>;
77
77
 
78
78
  type FieldAdaptersOfValues<FlattenedValues extends Readonly<Record<string, any>>, TypePathsToValuePaths extends Readonly<Record<keyof FlattenedValues, string>> = Readonly<Record<keyof FlattenedValues, any>>, Context = any> = {
79
79
  readonly [K in keyof FlattenedValues]: FieldAdapter<FlattenedValues[K], any, any, TypePathsToValuePaths[K], Context>;
@@ -118,7 +118,10 @@ type FlattenedErrors<ValuePathsToAdapters extends Readonly<Record<string, FieldA
118
118
  type ValuePathsToAdaptersOf<TypePathsToAdapters extends Partial<Readonly<Record<string, FieldAdapter>>>, ValuePathsToTypePaths extends Readonly<Record<string, string>>> = keyof TypePathsToAdapters extends ValueOf<ValuePathsToTypePaths> ? {
119
119
  readonly [K in keyof ValuePathsToTypePaths as unknown extends TypePathsToAdapters[ValuePathsToTypePaths[K]] ? never : K]: NonNullable<TypePathsToAdapters[ValuePathsToTypePaths[K]]>;
120
120
  } : never;
121
- declare abstract class FormModel<T extends Type, ValueToTypePaths extends Readonly<Record<string, string>>, TypePathsToAdapters extends FlattenedTypePathsToAdaptersOf<FlattenedValuesOfType<T, '*'>, ContextType>, ContextType = {}, ValuePathsToAdapters extends ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths> = ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths>> {
121
+ type ContextOf<TypePathsToAdapters extends Partial<Readonly<Record<string, FieldAdapter>>>> = UnionToIntersection<{
122
+ readonly [K in keyof TypePathsToAdapters]: TypePathsToAdapters[K] extends undefined ? undefined : unknown extends ContextOfFieldAdapter<NonNullable<TypePathsToAdapters[K]>> ? never : ContextOfFieldAdapter<NonNullable<TypePathsToAdapters[K]>>;
123
+ }[keyof TypePathsToAdapters] | {}>;
124
+ declare abstract class FormModel<T extends Type, ValueToTypePaths extends Readonly<Record<string, string>>, TypePathsToAdapters extends FlattenedTypePathsToAdaptersOf<FlattenedValuesOfType<T, '*'>, ContextType>, ContextType = ContextOf<TypePathsToAdapters>, ValuePathsToAdapters extends ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths> = ValuePathsToAdaptersOf<TypePathsToAdapters, ValueToTypePaths>> {
122
125
  readonly type: T;
123
126
  protected readonly adapters: TypePathsToAdapters;
124
127
  accessor value: MobxValueOfType<T>;
@@ -126,8 +129,7 @@ declare abstract class FormModel<T extends Type, ValueToTypePaths extends Readon
126
129
  accessor errors: FlattenedErrors<ValuePathsToAdapters>;
127
130
  private readonly flattenedTypeDefs;
128
131
  constructor(type: T, value: ValueOfType<ReadonlyTypeOfType<T>>, adapters: TypePathsToAdapters);
129
- get context(): ContextType;
130
- protected abstract toContext(value: ValueOfType<ReadonlyTypeOfType<T>>): ContextType;
132
+ protected abstract toContext(value: ValueOfType<ReadonlyTypeOfType<T>>, valuePath: keyof ValuePathsToAdapters): ContextType;
131
133
  get fields(): SimplifyDeep<FlattenedConvertedFieldsOf<ValuePathsToAdapters>>;
132
134
  private get knownFields();
133
135
  private maybeSynthesizeFieldByValuePath;
@@ -151,18 +153,18 @@ declare abstract class FormModel<T extends Type, ValueToTypePaths extends Readon
151
153
  }
152
154
 
153
155
  /**
154
- * Used to extract the supported value paths from a presenter
156
+ * Used to extract the supported value paths from a model
155
157
  */
156
158
  type ValuePathsOfModel<Model extends FormModel<any, any, any, any, any>> = Model extends FormModel<infer _1, infer _2, infer _3, infer _4, infer ValuePathsToAdapters> ? keyof ValuePathsToAdapters : never;
157
159
  /**
158
160
  * Used to extract the render type (so the value that is passed to the view) of a given value path
159
- * from a presenter
161
+ * from a model
160
162
  */
161
163
  type ToValueOfModelValuePath<Model extends FormModel<any, any, any, any, any>, K extends ValuePathsOfModel<Model>> = Model extends FormModel<infer _1, infer _2, infer _3, infer _4, infer ValuePathsToAdapters> ? ToOfFieldAdapter<ValuePathsToAdapters[K]> : never;
162
164
  /**
163
- * Extracts the form fields from the presenter. The recommended way is to
165
+ * Extracts the form fields from a form model. The recommended way is to
164
166
  * define the form fields explicitly and use that type to enforce the types
165
- * of your converters, but generating the FormFields from your presenter
167
+ * of your converters, but generating the FormFields from your model
166
168
  * is less typing, albeit at the cost of potentially getting type errors
167
169
  * reported a long way away from the source
168
170
  */
@@ -196,7 +198,7 @@ type MergedOfFieldAdaptersWithValidators<FieldAdapters extends Readonly<Record<K
196
198
  } & {
197
199
  readonly [K in Exclude<keyof FieldAdapters, Key>]: FieldAdapters[K];
198
200
  }>;
199
- type MergedOfFieldAdapterWithValidator<A extends FieldAdapter, V extends Validator | undefined> = undefined extends V ? A : A extends FieldAdapter<infer From, infer To, infer E1, infer P1, infer C1> ? V extends Validator<From, infer E2, infer P2, infer C2> ? FieldAdapter<From, To, E1 | E2, P1 | P2, C1 | C2> : never : never;
201
+ type MergedOfFieldAdapterWithValidator<A extends FieldAdapter, V extends Validator | undefined> = undefined extends V ? A : A extends FieldAdapter<infer From, infer To, infer E1, infer P1, infer C1> ? V extends Validator<From, infer E2, infer P2, infer C2> ? FieldAdapter<From, To, E1 | E2, P1 | P2, C1 & C2> : never : never;
200
202
  declare function mergeAdaptersWithValidators<FieldAdapters extends Readonly<Record<Key, FieldAdapter>>, Validators extends Readonly<Record<string, Validator>>, Key extends keyof Validators = keyof Validators>(adapters: FieldAdapters, validators: Validators): MergedOfFieldAdaptersWithValidators<FieldAdapters, Validators, Key>;
201
203
 
202
204
  type SubFormFieldAdapter<F extends FieldAdapter, ValuePath extends string> = FieldAdapter<FromOfFieldAdapter<F>, ToOfFieldAdapter<F>, ErrorOfFieldAdapter<F>, ValuePathOfFieldAdapter<F> extends StringConcatOf<'$', infer ValuePathSuffix> ? `${ValuePath}${ValuePathSuffix}` : string, ContextOfFieldAdapter<F>>;
@@ -283,21 +285,6 @@ declare function DefaultErrorRenderer({ error, }: InternalErrorRendererProps<any
283
285
 
284
286
  type ValueTypeOfField<F extends Field> = F extends Field<infer V> ? V : never;
285
287
 
286
- /**
287
- * Displays current value and error of a field
288
- */
289
- declare function FieldView<F extends Fields, K extends keyof F>({ fields, valuePath, children, }: {
290
- fields: F;
291
- valuePath: K;
292
- children: (props: {
293
- value: ValueTypeOfField<F[K]>;
294
- error: ErrorOfField<F[K]> | undefined;
295
- ErrorSink: ComponentType<{
296
- error: ErrorOfField<F[K]>;
297
- }>;
298
- }) => JSX.Element;
299
- }): react_jsx_runtime.JSX.Element;
300
-
301
288
  type AllFieldsOfFields<F extends Fields> = {
302
289
  [K in keyof F as ValueTypeOfField<F[K]> extends any ? K : never]: F[K];
303
290
  };
@@ -350,6 +337,20 @@ type MantineFieldComponent<T, P = T, E = any> = UnsafePartialComponent<Component
350
337
 
351
338
  type SuppliedCheckboxProps = Pick<CheckboxProps, 'name' | 'checked' | 'disabled' | 'required' | 'error' | 'onChange' | 'onFocus' | 'onBlur' | 'onKeyUp'>;
352
339
 
340
+ type FieldViewProps<F extends Fields, K extends keyof F> = {
341
+ children: (props: {
342
+ value: ValueTypeOfField<F[K]>;
343
+ error: ErrorOfField<F[K]> | undefined;
344
+ ErrorSink: ComponentType<{
345
+ error: ErrorOfField<F[K]>;
346
+ }>;
347
+ onFocus: () => void;
348
+ onBlur: () => void;
349
+ onValueChange: (v: ValueTypeOfField<F[K]>) => void;
350
+ onSubmit: () => void;
351
+ }) => JSX.Element;
352
+ };
353
+
353
354
  type CallbackMapper<ValuePath extends string> = {
354
355
  <Cb extends (...args: any[]) => any>(cb: Cb): Parameters<Cb> extends [infer SubFormValuePath extends string, ...(infer Rest)] ? SubFormValuePath extends StringConcatOf<ValuePath, infer Postfix> ? (valuePath: `$${Postfix}`, ...rest: Rest) => ReturnType<Cb> : never : never;
355
356
  };
@@ -400,7 +401,7 @@ type SuppliedValueInputProps<V, T extends Element = Element> = Partial<{
400
401
  declare function SimpleSelect(props: SelectProps & {
401
402
  onChange?: (value: string | null) => void;
402
403
  }): react_jsx_runtime.JSX.Element;
403
- declare function useMantineFormFields<F extends Fields>({ onFieldValueChange, onFieldBlur, onFieldFocus, onFieldSubmit, fields, }: FieldsViewProps<F>): MantineFormImpl<F>;
404
+ declare function useMantineFormFields<F extends Fields>({ onFieldValueChange, onFieldBlur, onFieldFocus, onFieldSubmit, fields, }: FieldsViewProps<F>): Omit<MantineFormImpl<F>, 'fields'>;
404
405
  declare class MantineFormImpl<F extends Fields> implements MantineForm<F> {
405
406
  private readonly textInputCache;
406
407
  private readonly valueInputCache;
@@ -409,6 +410,7 @@ declare class MantineFormImpl<F extends Fields> implements MantineForm<F> {
409
410
  private readonly radioCache;
410
411
  private readonly pillCache;
411
412
  private readonly listCache;
413
+ private readonly fieldViewCache;
412
414
  private readonly fieldsViewCache;
413
415
  private readonly formCache;
414
416
  accessor fields: F;
@@ -430,6 +432,7 @@ declare class MantineFormImpl<F extends Fields> implements MantineForm<F> {
430
432
  pill<K extends keyof AllFieldsOfFields<F>>(valuePath: K): MantineFieldComponent<SuppliedPillProps, PillProps, ErrorOfField<F[K]>>;
431
433
  pill<K extends keyof AllFieldsOfFields<F>, P extends SuppliedPillProps>(valuePath: K, Pill: ComponentType<P>): MantineFieldComponent<SuppliedPillProps, P, ErrorOfField<F[K]>>;
432
434
  list<K extends keyof ListFieldsOfFields<F>>(valuePath: K): MantineFieldComponent<SuppliedListProps<`${K}.${number}`>, ComponentProps<typeof DefaultList<ElementOfArray<F[K]['value']>, K>>, never>;
435
+ fieldView<K extends keyof AllFieldsOfFields<F>>(valuePath: K): ComponentType<FieldViewProps<F, K>>;
433
436
  fieldsView<K extends keyof AllFieldsOfFields<F>, P extends FieldsViewProps<Fields> = FieldsViewProps<SubFormFields<F, K>>>(valuePath: K, FieldsView: ComponentType<P>): FieldsView<K, MantineFieldComponent<FieldsViewProps<P['fields']>, P, never>>;
434
437
  form<K extends keyof AllFieldsOfFields<F>, P extends FormProps<ValueTypeOfField<F[K]>> = FormProps<ValueTypeOfField<F[K]>>>(valuePath: K, Form: ComponentType<P>): MantineFieldComponent<FormProps<ValueTypeOfField<F[K]>>, P, never>;
435
438
  }
@@ -437,9 +440,9 @@ declare class MantineFormImpl<F extends Fields> implements MantineForm<F> {
437
440
  type MergedOfValidators<Validators1 extends Partial<Readonly<Record<Keys, Validator>>>, Validators2 extends Partial<Readonly<Record<Keys, Validator>>>, Keys extends string = Extract<keyof Validators1 | keyof Validators2, string>> = Simplify<{
438
441
  readonly [K in Keys]: undefined extends Validators1[K] ? undefined extends Validators2[K] ? never : Validators2[K] : undefined extends Validators2[K] ? Validators1[K] : MergedOfValidator<NonNullable<Validators1[K]>, NonNullable<Validators2[K]>>;
439
442
  }>;
440
- type MergedOfValidator<Validator1 extends Validator, Validator2 extends Validator> = Validator1 extends Validator<infer V, infer E1, infer P, infer C> ? Validator2 extends Validator<V, infer E2, P, C> ? Validator<V, E1 | E2, P, C> : never : never;
443
+ type MergedOfValidator<Validator1 extends Validator, Validator2 extends Validator> = Validator1 extends Validator<infer V, infer E1, infer P, infer C1> ? Validator2 extends Validator<V, infer E2, P, infer C2> ? Validator<V, E1 | E2, P, C1 & C2> : never : never;
441
444
  declare function mergeValidators<Validators1 extends Partial<Readonly<Record<Keys, Validator>>>, Validators2 extends Partial<Readonly<Record<Keys, Validator>>>, Keys extends string = Extract<keyof Validators1 | keyof Validators2, string>>(validators1: Validators1, validators2: Validators2): MergedOfValidators<Validators1, Validators2, Keys>;
442
445
 
443
446
  declare function Empty(): null;
444
447
 
445
- export { AbstractSelectValueTypeConverter, type AnnotatedFieldConversion, type AnnotatedFieldConverter, type Annotation, type ContextOfFieldAdapter, DefaultErrorRenderer, Empty, type ErrorOfField, type ErrorOfFieldAdapter, type ErrorRenderer, type ErrorRendererProps, type Field, type FieldAdapter, type FieldAdaptersOfValues, type FieldValueFactory, FieldView, type Fields, type FieldsViewProps, type FlattenedAdaptersOfFields, type FlattenedConvertedFieldsOf, type FlattenedTypePathsToAdaptersOf, type FormFieldsOfFieldAdapters, type FormFieldsOfModel, FormModel, type FormProps, type FromOfFieldAdapter, IntegerToStringConverter, type MergedOfFieldAdaptersWithTwoWayConverter, type MergedOfFieldAdaptersWithValidators, type MergedOfValidator, type MergedOfValidators, NullableToBooleanConverter, type PartialComponent, SelectDiscriminatedUnionConverter, SelectLiteralConverter, SelectStringConverter, type ToOfFieldAdapter, type ToValueOfModelValuePath, TrimmingStringConverter, type TwoWayFieldConverter, type TwoWayFieldConverterWithValueFactory, type UnreliableFieldConversion, UnreliableFieldConversionType, type UnreliableFieldConverter, type UnsafePartialComponent, type ValuePathOfFieldAdapter, type ValuePathsOfModel, type ValuePathsToAdaptersOf, adapter, adapterFromPrototype, adapterFromTwoWayConverter, createPartialComponent, createPartialObserverComponent, createSimplePartialComponent, createUnsafePartialObserverComponent, identityAdapter, listAdapter, mergeAdaptersWithValidators, mergeFieldAdaptersWithTwoWayConverter, mergeValidators, prototypingFieldValueFactory, subFormFieldAdapters, trimmingStringAdapter, useDefaultMobxFormHooks, useMantineFormFields, usePartialComponent, usePartialObserverComponent, validatingConverter };
448
+ export { AbstractSelectValueTypeConverter, type AnnotatedFieldConversion, type AnnotatedFieldConverter, type Annotation, type ContextOf, type ContextOfFieldAdapter, DefaultErrorRenderer, Empty, type ErrorOfField, type ErrorOfFieldAdapter, type ErrorRenderer, type ErrorRendererProps, type Field, type FieldAdapter, type FieldAdaptersOfValues, type FieldValueFactory, type Fields, type FieldsViewProps, type FlattenedAdaptersOfFields, type FlattenedConvertedFieldsOf, type FlattenedTypePathsToAdaptersOf, type FormFieldsOfFieldAdapters, type FormFieldsOfModel, FormModel, type FormProps, type FromOfFieldAdapter, IntegerToStringConverter, type MergedOfFieldAdaptersWithTwoWayConverter, type MergedOfFieldAdaptersWithValidators, type MergedOfValidator, type MergedOfValidators, NullableToBooleanConverter, type PartialComponent, SelectDiscriminatedUnionConverter, SelectLiteralConverter, SelectStringConverter, type ToOfFieldAdapter, type ToValueOfModelValuePath, TrimmingStringConverter, type TwoWayFieldConverter, type TwoWayFieldConverterWithValueFactory, type UnreliableFieldConversion, UnreliableFieldConversionType, type UnreliableFieldConverter, type UnsafePartialComponent, type ValuePathOfFieldAdapter, type ValuePathsOfModel, type ValuePathsToAdaptersOf, adapter, adapterFromPrototype, adapterFromTwoWayConverter, createPartialComponent, createPartialObserverComponent, createSimplePartialComponent, createUnsafePartialObserverComponent, identityAdapter, listAdapter, mergeAdaptersWithValidators, mergeFieldAdaptersWithTwoWayConverter, mergeValidators, prototypingFieldValueFactory, subFormFieldAdapters, trimmingStringAdapter, useDefaultMobxFormHooks, useMantineFormFields, usePartialComponent, usePartialObserverComponent, validatingConverter };