@react-typed-forms/schemas 7.2.0 → 7.3.0

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/lib/index.js CHANGED
@@ -434,11 +434,10 @@ function getDisplayOnlyOptions(d) {
434
434
  return isDataControlDefinition(d) && d.renderOptions && isDisplayOnlyRenderer(d.renderOptions) ? d.renderOptions : undefined;
435
435
  }
436
436
  function getTypeField(context) {
437
- var _context$groupControl;
438
437
  var typeSchemaField = context.fields.find(function (x) {
439
438
  return x.isTypeField;
440
439
  });
441
- return typeSchemaField ? (_context$groupControl = context.groupControl.fields) == null ? void 0 : _context$groupControl[typeSchemaField.field] : undefined;
440
+ return typeSchemaField ? lookupChildControl(context, typeSchemaField.field) : undefined;
442
441
  }
443
442
  function visitControlDataArray(controls, context, cb) {
444
443
  if (!controls) return undefined;
@@ -469,10 +468,13 @@ function visitControlData(definition, ctx, cb) {
469
468
  function processData(def, fieldName, children) {
470
469
  var fieldData = fieldName ? findField(ctx.fields, fieldName) : undefined;
471
470
  if (!fieldData) return !fieldName ? visitControlDataArray(children, ctx, cb) : undefined;
472
- var control = ctx.groupControl.fields[fieldData.field];
471
+ var thisPath = [].concat(ctx.path, [fieldData.field]);
472
+ var control = ctx.data.lookupControl(thisPath);
473
+ if (!control) throw "No control for field";
473
474
  var result = def ? cb(def, fieldData, control, false) : undefined;
474
475
  if (result !== undefined) return result;
475
476
  if (fieldData.collection) {
477
+ var cIndex = 0;
476
478
  for (var _iterator3 = _createForOfIteratorHelperLoose((_control$elements = control.elements) != null ? _control$elements : []), _step3; !(_step3 = _iterator3()).done;) {
477
479
  var _control$elements;
478
480
  var c = _step3.value;
@@ -481,14 +483,19 @@ function visitControlData(definition, ctx, cb) {
481
483
  if (isCompoundField(fieldData)) {
482
484
  var cfResult = visitControlDataArray(children, _extends({}, ctx, {
483
485
  fields: fieldData.children,
484
- groupControl: c
486
+ path: [].concat(thisPath, [cIndex])
485
487
  }), cb);
486
488
  if (cfResult !== undefined) return cfResult;
487
489
  }
490
+ cIndex++;
488
491
  }
489
492
  }
490
493
  }
491
494
  }
495
+ function lookupChildControl(data, child) {
496
+ var childPath = [].concat(data.path, [child]);
497
+ return data.data.lookupControl(childPath);
498
+ }
492
499
  function cleanDataForSchema(v, fields) {
493
500
  if (!v) return v;
494
501
  var typeField = fields.find(function (x) {
@@ -538,6 +545,18 @@ function getAllReferencedClasses(c) {
538
545
  if (childClasses) return [tc].concat(childClasses);
539
546
  return [tc];
540
547
  }
548
+ function jsonPathString(jsonPath) {
549
+ var out = "";
550
+ jsonPath.forEach(function (v, i) {
551
+ if (typeof v === "number") {
552
+ out += "[" + v + "]";
553
+ } else {
554
+ if (i > 0) out += ".";
555
+ out += v;
556
+ }
557
+ });
558
+ return out;
559
+ }
541
560
 
542
561
  function dataControl(field, title, options) {
543
562
  return _extends({
@@ -660,7 +679,44 @@ function useCalculatedControl(calculate) {
660
679
  function cc(n) {
661
680
  return n ? n : undefined;
662
681
  }
682
+ function trackedStructure(c, tracker) {
683
+ var cc = c.current;
684
+ var cv = cc.value;
685
+ if (cv == null) {
686
+ tracker(c, core.ControlChange.Structure);
687
+ return cv;
688
+ }
689
+ if (typeof cv !== "object") {
690
+ tracker(c, core.ControlChange.Value);
691
+ return cv;
692
+ }
693
+ return new Proxy(cv, {
694
+ get: function get(target, p, receiver) {
695
+ if (Array.isArray(cv)) {
696
+ tracker(c, core.ControlChange.Structure);
697
+ if (typeof p === "symbol" || p[0] >= "9" || p[0] < "0") return Reflect.get(cv, p);
698
+ var nc = cc.elements[p];
699
+ if (typeof nc === "function") return nc;
700
+ if (nc == null) return null;
701
+ return trackedStructure(nc, tracker);
702
+ }
703
+ if (p in cv) return trackedStructure(cc.fields[p], tracker);
704
+ return undefined;
705
+ }
706
+ });
707
+ }
663
708
 
709
+ function _finallyRethrows(body, finalizer) {
710
+ try {
711
+ var result = body();
712
+ } catch (e) {
713
+ return finalizer(true, e);
714
+ }
715
+ if (result && result.then) {
716
+ return result.then(finalizer.bind(null, false), finalizer.bind(null, true));
717
+ }
718
+ return finalizer(false, result);
719
+ }
664
720
  function useEvalVisibilityHook(useEvalExpressionHook, definition, schemaField) {
665
721
  var dynamicVisibility = useEvalDynamicHook(definition, exports.DynamicPropertyType.Visible, useEvalExpressionHook);
666
722
  var r = useUpdatedRef({
@@ -736,14 +792,14 @@ function useEvalDefaultValueHook(useEvalExpressionHook, definition, schemaField)
736
792
  }
737
793
  function useDataExpression(fvExpr, fields, data) {
738
794
  var refField = findField(fields, fvExpr.field);
739
- var otherField = refField ? data.fields[refField.field] : undefined;
795
+ var otherField = refField ? lookupChildControl(data, refField.field) : undefined;
740
796
  return useCalculatedControl(function () {
741
797
  return otherField == null ? void 0 : otherField.value;
742
798
  });
743
799
  }
744
800
  function useDataMatchExpression(fvExpr, fields, data) {
745
801
  var refField = findField(fields, fvExpr.field);
746
- var otherField = refField ? data.fields[refField.field] : undefined;
802
+ var otherField = refField ? lookupChildControl(data, refField.field) : undefined;
747
803
  return core.useComputed(function () {
748
804
  var fv = otherField == null ? void 0 : otherField.value;
749
805
  return Array.isArray(fv) ? fv.includes(fvExpr.value) : fv === fvExpr.value;
@@ -752,11 +808,11 @@ function useDataMatchExpression(fvExpr, fields, data) {
752
808
  function defaultEvalHooks(expr, context) {
753
809
  switch (expr.type) {
754
810
  case exports.ExpressionType.Jsonata:
755
- return useJsonataExpression(expr.expression, context.groupControl, context.root);
811
+ return useJsonataExpression(expr.expression, context);
756
812
  case exports.ExpressionType.Data:
757
- return useDataExpression(expr, context.fields, context.groupControl);
813
+ return useDataExpression(expr, context.fields, context);
758
814
  case exports.ExpressionType.DataMatch:
759
- return useDataMatchExpression(expr, context.fields, context.groupControl);
815
+ return useDataMatchExpression(expr, context.fields, context);
760
816
  default:
761
817
  return core.useControl(undefined);
762
818
  }
@@ -785,34 +841,62 @@ function matchesType(context, types) {
785
841
  return typeField && types.includes(typeField.value);
786
842
  }
787
843
  function hideDisplayOnly(context, field, definition, schemaInterface) {
844
+ var _lookupChildControl;
788
845
  var displayOptions = getDisplayOnlyOptions(definition);
789
- return displayOptions && !displayOptions.emptyText && schemaInterface.isEmptyValue(field, context.groupControl.fields[field.field].value);
846
+ return displayOptions && !displayOptions.emptyText && schemaInterface.isEmptyValue(field, (_lookupChildControl = lookupChildControl(context, field.field)) == null ? void 0 : _lookupChildControl.value);
790
847
  }
791
- function useJsonataExpression(jExpr, data, root) {
848
+ function useJsonataExpression(jExpr, dataContext, bindings) {
849
+ var pathString = jsonPathString(dataContext.path);
792
850
  var compiledExpr = React.useMemo(function () {
793
851
  try {
794
- return jsonata__default["default"](jExpr);
852
+ return jsonata__default["default"](pathString ? pathString + ".(" + jExpr + ")" : jExpr);
795
853
  } catch (e) {
796
854
  console.error(e);
797
855
  return jsonata__default["default"]("null");
798
856
  }
799
- }, [jExpr]);
857
+ }, [jExpr, pathString]);
800
858
  var control = core.useControl();
801
- core.useControlEffect(function () {
802
- return [data.value, root.value];
803
- }, function (_ref2) {
804
- var v = _ref2[0],
805
- rv = _ref2[1];
806
- try {
807
- return Promise.resolve(compiledExpr.evaluate(v, {
808
- root: rv
809
- })).then(function (_compiledExpr$evaluat) {
810
- control.value = _compiledExpr$evaluat;
859
+ var listenerRef = React.useRef();
860
+ var _useRefState = core.useRefState(function () {
861
+ return core.makeChangeTracker(function () {
862
+ var l = listenerRef.current;
863
+ if (l) {
864
+ listenerRef.current = undefined;
865
+ core.addAfterChangesCallback(function () {
866
+ l();
867
+ listenerRef.current = l;
868
+ });
869
+ }
811
870
  });
812
- } catch (e) {
813
- return Promise.reject(e);
814
- }
815
- }, true);
871
+ }),
872
+ ref = _useRefState[0];
873
+ React.useEffect(function () {
874
+ var apply = function apply() {
875
+ try {
876
+ var _ref$current = ref.current,
877
+ collect = _ref$current[0],
878
+ updateSubscriptions = _ref$current[1];
879
+ var _temp = _finallyRethrows(function () {
880
+ var bindingData = bindings ? core.collectChanges(collect, bindings) : undefined;
881
+ return Promise.resolve(compiledExpr.evaluate(trackedStructure(dataContext.data, collect), bindingData)).then(function (_compiledExpr$evaluat) {
882
+ control.value = _compiledExpr$evaluat;
883
+ });
884
+ }, function (_wasThrown, _result) {
885
+ updateSubscriptions();
886
+ if (_wasThrown) throw _result;
887
+ return _result;
888
+ });
889
+ return Promise.resolve(_temp && _temp.then ? _temp.then(function () {}) : void 0);
890
+ } catch (e) {
891
+ return Promise.reject(e);
892
+ }
893
+ };
894
+ listenerRef.current = apply;
895
+ apply();
896
+ return function () {
897
+ return ref.current[1](true);
898
+ };
899
+ }, [compiledExpr]);
816
900
  return control;
817
901
  }
818
902
 
@@ -843,7 +927,7 @@ function useValidationHook(definition) {
843
927
  }, validatorTypes ? validatorTypes : [null]);
844
928
  }
845
929
  function useJsonataValidator(control, context, expr, hidden, i) {
846
- var errorMsg = useJsonataExpression(expr.expression, context.groupControl, context.root);
930
+ var errorMsg = useJsonataExpression(expr.expression, context);
847
931
  core.useControlEffect(function () {
848
932
  return [hidden, errorMsg.value];
849
933
  }, function (_ref) {
@@ -932,27 +1016,29 @@ function useControlRenderer(definition, fields, renderer, options) {
932
1016
  schemaField: schemaField
933
1017
  });
934
1018
  var Component = React.useCallback(function (_ref) {
935
- var parentControl = _ref.control;
1019
+ var rootControl = _ref.control,
1020
+ _ref$parentPath = _ref.parentPath,
1021
+ parentPath = _ref$parentPath === void 0 ? [] : _ref$parentPath;
936
1022
  var stopTracking = core.useComponentTracking();
937
1023
  try {
938
- var _options$dataRoot, _c$children$map, _c$children, _definition$adornment, _definition$adornment2;
1024
+ var _c$children$map, _c$children, _definition$adornment, _definition$adornment2;
939
1025
  var _r$current = r.current,
940
1026
  c = _r$current.definition,
941
1027
  _options = _r$current.options,
942
1028
  _fields = _r$current.fields,
943
1029
  _schemaField = _r$current.schemaField;
944
- var dataContext = {
945
- groupControl: parentControl,
1030
+ var parentDataContext = {
946
1031
  fields: _fields,
947
1032
  schemaInterface: schemaInterface,
948
- root: (_options$dataRoot = _options.dataRoot) != null ? _options$dataRoot : parentControl
1033
+ data: rootControl,
1034
+ path: parentPath
949
1035
  };
950
- var readonlyControl = useIsReadonly(dataContext);
951
- var disabledControl = useIsDisabled(dataContext);
952
- var visibleControl = useIsVisible(dataContext);
953
- var displayControl = useDynamicDisplay(dataContext);
954
- var customStyle = useCustomStyle(dataContext).value;
955
- var layoutStyle = useLayoutStyle(dataContext).value;
1036
+ var readonlyControl = useIsReadonly(parentDataContext);
1037
+ var disabledControl = useIsDisabled(parentDataContext);
1038
+ var visibleControl = useIsVisible(parentDataContext);
1039
+ var displayControl = useDynamicDisplay(parentDataContext);
1040
+ var customStyle = useCustomStyle(parentDataContext).value;
1041
+ var layoutStyle = useLayoutStyle(parentDataContext).value;
956
1042
  var visible = visibleControl.current.value;
957
1043
  var visibility = core.useControl(function () {
958
1044
  return visible != null ? {
@@ -970,29 +1056,34 @@ function useControlRenderer(definition, fields, renderer, options) {
970
1056
  };
971
1057
  });
972
1058
  });
973
- var allowedOptions = useAllowedOptions(dataContext);
974
- var defaultValueControl = useDefaultValue(dataContext);
975
- var _getControlData = getControlData(_schemaField, dataContext),
976
- control = _getControlData[0],
977
- childContext = _getControlData[1];
1059
+ var allowedOptions = useAllowedOptions(parentDataContext);
1060
+ var defaultValueControl = useDefaultValue(parentDataContext);
1061
+ var _getControlData = getControlData(_schemaField, parentDataContext),
1062
+ parentControl = _getControlData[0],
1063
+ control = _getControlData[1],
1064
+ controlDataContext = _getControlData[2];
978
1065
  core.useControlEffect(function () {
979
- return [visibility.value, defaultValueControl.value, control, parentControl.isNull, isDataControlDefinition(definition) && definition.dontClearHidden];
1066
+ return [visibility.value, defaultValueControl.value, control, isDataControlDefinition(definition) && definition.dontClearHidden, parentControl == null ? void 0 : parentControl.isNull];
980
1067
  }, function (_ref2) {
981
1068
  var vc = _ref2[0],
982
1069
  dv = _ref2[1],
983
1070
  cd = _ref2[2],
984
- pn = _ref2[3],
985
- dontClear = _ref2[4];
986
- if (pn) {
987
- parentControl.value = {};
988
- }
1071
+ dontClear = _ref2[3],
1072
+ parentNull = _ref2[4];
989
1073
  if (vc && cd && vc.visible === vc.showing) {
990
1074
  if (!vc.visible) {
991
- if (_options.clearHidden && !dontClear) cd.value = undefined;
1075
+ if (_options.clearHidden && !dontClear) {
1076
+ console.log("Clearing ", _schemaField == null ? void 0 : _schemaField.field);
1077
+ cd.value = undefined;
1078
+ }
992
1079
  } else if (cd.value == null) {
1080
+ console.log("Defaulting ", _schemaField == null ? void 0 : _schemaField.field, dv);
993
1081
  cd.value = dv;
994
1082
  }
995
1083
  }
1084
+ if (parentNull && parentControl != null && parentControl.isNull) {
1085
+ parentControl.value = {};
1086
+ }
996
1087
  }, true);
997
1088
  var myOptions = useCalculatedControl(function () {
998
1089
  var _visibility$fields;
@@ -1002,16 +1093,14 @@ function useControlRenderer(definition, fields, renderer, options) {
1002
1093
  disabled: _options.disabled || disabledControl.value
1003
1094
  };
1004
1095
  }).value;
1005
- useValidation(control, !!myOptions.hidden, dataContext);
1096
+ useValidation(control != null ? control : core.newControl(null), !!myOptions.hidden, parentDataContext);
1006
1097
  var childRenderers = (_c$children$map = (_c$children = c.children) == null ? void 0 : _c$children.map(function (cd) {
1007
- return useControlRenderer(cd, childContext.fields, renderer, _extends({}, _options, myOptions, {
1008
- dataRoot: dataContext.root
1009
- }));
1098
+ return useControlRenderer(cd, controlDataContext.fields, renderer, _extends({}, _options, myOptions));
1010
1099
  })) != null ? _c$children$map : [];
1011
1100
  React.useEffect(function () {
1012
1101
  if (control && typeof myOptions.disabled === "boolean") control.disabled = myOptions.disabled;
1013
1102
  }, [control, myOptions.disabled]);
1014
- if (parentControl.isNull) return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null);
1103
+ if (parentControl != null && parentControl.isNull) return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null);
1015
1104
  var adornments = (_definition$adornment = (_definition$adornment2 = definition.adornments) == null ? void 0 : _definition$adornment2.map(function (x) {
1016
1105
  return renderer.renderAdornment({
1017
1106
  adornment: x
@@ -1029,7 +1118,7 @@ function useControlRenderer(definition, fields, renderer, options) {
1029
1118
  },
1030
1119
  createDataProps: dataProps,
1031
1120
  formOptions: myOptions,
1032
- dataContext: dataContext,
1121
+ dataContext: controlDataContext,
1033
1122
  control: displayControl != null ? displayControl : control,
1034
1123
  schemaField: _schemaField,
1035
1124
  displayControl: displayControl,
@@ -1056,14 +1145,17 @@ function lookupSchemaField(c, fields) {
1056
1145
  return fieldName ? findField(fields, fieldName) : undefined;
1057
1146
  }
1058
1147
  function getControlData(schemaField, parentContext) {
1059
- var _parentContext$groupC, _parentContext$groupC2;
1060
- var childControl = schemaField ? (_parentContext$groupC = (_parentContext$groupC2 = parentContext.groupControl.fields) == null ? void 0 : _parentContext$groupC2[schemaField.field]) != null ? _parentContext$groupC : core.newControl({}) : undefined;
1061
- return [childControl, schemaField && isCompoundField(schemaField) ? _extends({}, parentContext, {
1062
- groupControl: childControl,
1063
- fields: schemaField.children
1148
+ var data = parentContext.data,
1149
+ path = parentContext.path;
1150
+ var parentControl = data.lookupControl(path);
1151
+ var childPath = schemaField ? [].concat(path, [schemaField.field]) : path;
1152
+ var childControl = schemaField && parentControl ? parentControl.fields[schemaField.field] : undefined;
1153
+ return [parentControl, childControl, schemaField ? _extends({}, parentContext, {
1154
+ path: childPath,
1155
+ fields: isCompoundField(schemaField) ? schemaField.children : parentContext.fields
1064
1156
  }) : parentContext];
1065
1157
  }
1066
- function groupProps(renderOptions, childCount, _renderChild, control, className, style) {
1158
+ function groupProps(renderOptions, childCount, _renderChild, data, className, style) {
1067
1159
  if (renderOptions === void 0) {
1068
1160
  renderOptions = {
1069
1161
  type: "Standard"
@@ -1073,7 +1165,8 @@ function groupProps(renderOptions, childCount, _renderChild, control, className,
1073
1165
  childCount: childCount,
1074
1166
  renderChild: function renderChild(i) {
1075
1167
  return _renderChild(i, i, {
1076
- control: control
1168
+ control: data.data,
1169
+ parentPath: data.path
1077
1170
  });
1078
1171
  },
1079
1172
  renderOptions: renderOptions,
@@ -1096,6 +1189,7 @@ function defaultDataProps(_ref3) {
1096
1189
  var fieldOptions = ((_field$options$length = (_field$options = field.options) == null ? void 0 : _field$options.length) != null ? _field$options$length : 0) === 0 ? null : field.options;
1097
1190
  var allowed = (_allowedOptions$value = allowedOptions == null ? void 0 : allowedOptions.value) != null ? _allowedOptions$value : [];
1098
1191
  return _extends({
1192
+ definition: definition,
1099
1193
  control: control,
1100
1194
  field: field,
1101
1195
  id: "c" + control.uniqueId,
@@ -1144,7 +1238,7 @@ function defaultArrayProps(arrayControl, field, required, style, className, _ren
1144
1238
  };
1145
1239
  },
1146
1240
  renderElement: function renderElement(i) {
1147
- return _renderElement(elems[i]);
1241
+ return _renderElement(i);
1148
1242
  },
1149
1243
  className: cc(className),
1150
1244
  style: style
@@ -1176,7 +1270,7 @@ function renderControlLayout(_ref4) {
1176
1270
  }));
1177
1271
  }
1178
1272
  return {
1179
- processLayout: renderer.renderGroup(groupProps(c.groupOptions, childCount, childRenderer, dataContext.groupControl, c.styleClass, style)),
1273
+ processLayout: renderer.renderGroup(groupProps(c.groupOptions, childCount, childRenderer, dataContext, c.styleClass, style)),
1180
1274
  label: {
1181
1275
  label: c.title,
1182
1276
  type: exports.LabelType.Group,
@@ -1208,22 +1302,27 @@ function renderControlLayout(_ref4) {
1208
1302
  };
1209
1303
  }
1210
1304
  return {};
1211
- function renderData(c, elementControl) {
1305
+ function renderData(c, elemIndex) {
1212
1306
  if (!schemaField) return {
1213
1307
  children: "No schema field for: " + c.field
1214
1308
  };
1309
+ if (!childControl) return {
1310
+ children: "No control for: " + c.field
1311
+ };
1215
1312
  var props = dataProps({
1216
1313
  definition: c,
1217
1314
  field: schemaField,
1218
- dataContext: dataContext,
1219
- control: elementControl != null ? elementControl : childControl,
1315
+ dataContext: elemIndex != null ? _extends({}, dataContext, {
1316
+ path: [].concat(dataContext.path, [elemIndex])
1317
+ }) : dataContext,
1318
+ control: elemIndex != null ? childControl.elements[elemIndex] : childControl,
1220
1319
  options: dataOptions,
1221
1320
  style: style,
1222
1321
  childCount: childCount,
1223
1322
  allowedOptions: allowedOptions,
1224
1323
  renderChild: childRenderer,
1225
- elementRenderer: elementControl == null && schemaField.collection ? function (element) {
1226
- return renderLayoutParts(renderData(c, element), renderer).children;
1324
+ elementRenderer: elemIndex == null && schemaField.collection ? function (ei) {
1325
+ return renderLayoutParts(renderData(c, ei), renderer).children;
1227
1326
  } : undefined
1228
1327
  });
1229
1328
  var labelText = !c.hideTitle ? controlTitle(c.title, schemaField) : undefined;
@@ -1623,7 +1722,8 @@ function createDefaultDataRenderer(options) {
1623
1722
  },
1624
1723
  renderChild: function renderChild(i) {
1625
1724
  return props.renderChild(i, i, {
1626
- control: props.control
1725
+ control: props.dataContext.data,
1726
+ parentPath: props.dataContext.path
1627
1727
  });
1628
1728
  },
1629
1729
  childCount: props.childCount
@@ -2151,8 +2251,10 @@ exports.isGroupControl = isGroupControl;
2151
2251
  exports.isGroupControlsDefinition = isGroupControlsDefinition;
2152
2252
  exports.isIconAdornment = isIconAdornment;
2153
2253
  exports.isScalarField = isScalarField;
2254
+ exports.jsonPathString = jsonPathString;
2154
2255
  exports.jsonataExpr = jsonataExpr;
2155
2256
  exports.layoutKeyForPlacement = layoutKeyForPlacement;
2257
+ exports.lookupChildControl = lookupChildControl;
2156
2258
  exports.lookupSchemaField = lookupSchemaField;
2157
2259
  exports.makeCompoundField = makeCompoundField;
2158
2260
  exports.makeEvalExpressionHook = makeEvalExpressionHook;