@react-typed-forms/schemas 7.2.0 → 7.3.1

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,32 @@ 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
+ cd.value = undefined;
1077
+ }
992
1078
  } else if (cd.value == null) {
993
1079
  cd.value = dv;
994
1080
  }
995
1081
  }
1082
+ if (parentNull && parentControl != null && parentControl.isNull) {
1083
+ parentControl.value = {};
1084
+ }
996
1085
  }, true);
997
1086
  var myOptions = useCalculatedControl(function () {
998
1087
  var _visibility$fields;
@@ -1002,16 +1091,14 @@ function useControlRenderer(definition, fields, renderer, options) {
1002
1091
  disabled: _options.disabled || disabledControl.value
1003
1092
  };
1004
1093
  }).value;
1005
- useValidation(control, !!myOptions.hidden, dataContext);
1094
+ useValidation(control != null ? control : core.newControl(null), !!myOptions.hidden, parentDataContext);
1006
1095
  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
- }));
1096
+ return useControlRenderer(cd, controlDataContext.fields, renderer, _extends({}, _options, myOptions));
1010
1097
  })) != null ? _c$children$map : [];
1011
1098
  React.useEffect(function () {
1012
1099
  if (control && typeof myOptions.disabled === "boolean") control.disabled = myOptions.disabled;
1013
1100
  }, [control, myOptions.disabled]);
1014
- if (parentControl.isNull) return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null);
1101
+ if (parentControl != null && parentControl.isNull) return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null);
1015
1102
  var adornments = (_definition$adornment = (_definition$adornment2 = definition.adornments) == null ? void 0 : _definition$adornment2.map(function (x) {
1016
1103
  return renderer.renderAdornment({
1017
1104
  adornment: x
@@ -1029,7 +1116,7 @@ function useControlRenderer(definition, fields, renderer, options) {
1029
1116
  },
1030
1117
  createDataProps: dataProps,
1031
1118
  formOptions: myOptions,
1032
- dataContext: dataContext,
1119
+ dataContext: controlDataContext,
1033
1120
  control: displayControl != null ? displayControl : control,
1034
1121
  schemaField: _schemaField,
1035
1122
  displayControl: displayControl,
@@ -1056,14 +1143,17 @@ function lookupSchemaField(c, fields) {
1056
1143
  return fieldName ? findField(fields, fieldName) : undefined;
1057
1144
  }
1058
1145
  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
1146
+ var data = parentContext.data,
1147
+ path = parentContext.path;
1148
+ var parentControl = data.lookupControl(path);
1149
+ var childPath = schemaField ? [].concat(path, [schemaField.field]) : path;
1150
+ var childControl = schemaField && parentControl ? parentControl.fields[schemaField.field] : undefined;
1151
+ return [parentControl, childControl, schemaField ? _extends({}, parentContext, {
1152
+ path: childPath,
1153
+ fields: isCompoundField(schemaField) ? schemaField.children : parentContext.fields
1064
1154
  }) : parentContext];
1065
1155
  }
1066
- function groupProps(renderOptions, childCount, _renderChild, control, className, style) {
1156
+ function groupProps(renderOptions, childCount, _renderChild, data, className, style) {
1067
1157
  if (renderOptions === void 0) {
1068
1158
  renderOptions = {
1069
1159
  type: "Standard"
@@ -1073,7 +1163,8 @@ function groupProps(renderOptions, childCount, _renderChild, control, className,
1073
1163
  childCount: childCount,
1074
1164
  renderChild: function renderChild(i) {
1075
1165
  return _renderChild(i, i, {
1076
- control: control
1166
+ control: data.data,
1167
+ parentPath: data.path
1077
1168
  });
1078
1169
  },
1079
1170
  renderOptions: renderOptions,
@@ -1096,6 +1187,7 @@ function defaultDataProps(_ref3) {
1096
1187
  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
1188
  var allowed = (_allowedOptions$value = allowedOptions == null ? void 0 : allowedOptions.value) != null ? _allowedOptions$value : [];
1098
1189
  return _extends({
1190
+ definition: definition,
1099
1191
  control: control,
1100
1192
  field: field,
1101
1193
  id: "c" + control.uniqueId,
@@ -1144,7 +1236,7 @@ function defaultArrayProps(arrayControl, field, required, style, className, _ren
1144
1236
  };
1145
1237
  },
1146
1238
  renderElement: function renderElement(i) {
1147
- return _renderElement(elems[i]);
1239
+ return _renderElement(i);
1148
1240
  },
1149
1241
  className: cc(className),
1150
1242
  style: style
@@ -1176,7 +1268,7 @@ function renderControlLayout(_ref4) {
1176
1268
  }));
1177
1269
  }
1178
1270
  return {
1179
- processLayout: renderer.renderGroup(groupProps(c.groupOptions, childCount, childRenderer, dataContext.groupControl, c.styleClass, style)),
1271
+ processLayout: renderer.renderGroup(groupProps(c.groupOptions, childCount, childRenderer, dataContext, c.styleClass, style)),
1180
1272
  label: {
1181
1273
  label: c.title,
1182
1274
  type: exports.LabelType.Group,
@@ -1208,22 +1300,27 @@ function renderControlLayout(_ref4) {
1208
1300
  };
1209
1301
  }
1210
1302
  return {};
1211
- function renderData(c, elementControl) {
1303
+ function renderData(c, elemIndex) {
1212
1304
  if (!schemaField) return {
1213
1305
  children: "No schema field for: " + c.field
1214
1306
  };
1307
+ if (!childControl) return {
1308
+ children: "No control for: " + c.field
1309
+ };
1215
1310
  var props = dataProps({
1216
1311
  definition: c,
1217
1312
  field: schemaField,
1218
- dataContext: dataContext,
1219
- control: elementControl != null ? elementControl : childControl,
1313
+ dataContext: elemIndex != null ? _extends({}, dataContext, {
1314
+ path: [].concat(dataContext.path, [elemIndex])
1315
+ }) : dataContext,
1316
+ control: elemIndex != null ? childControl.elements[elemIndex] : childControl,
1220
1317
  options: dataOptions,
1221
1318
  style: style,
1222
1319
  childCount: childCount,
1223
1320
  allowedOptions: allowedOptions,
1224
1321
  renderChild: childRenderer,
1225
- elementRenderer: elementControl == null && schemaField.collection ? function (element) {
1226
- return renderLayoutParts(renderData(c, element), renderer).children;
1322
+ elementRenderer: elemIndex == null && schemaField.collection ? function (ei) {
1323
+ return renderLayoutParts(renderData(c, ei), renderer).children;
1227
1324
  } : undefined
1228
1325
  });
1229
1326
  var labelText = !c.hideTitle ? controlTitle(c.title, schemaField) : undefined;
@@ -1623,7 +1720,8 @@ function createDefaultDataRenderer(options) {
1623
1720
  },
1624
1721
  renderChild: function renderChild(i) {
1625
1722
  return props.renderChild(i, i, {
1626
- control: props.control
1723
+ control: props.dataContext.data,
1724
+ parentPath: props.dataContext.path
1627
1725
  });
1628
1726
  },
1629
1727
  childCount: props.childCount
@@ -2151,8 +2249,10 @@ exports.isGroupControl = isGroupControl;
2151
2249
  exports.isGroupControlsDefinition = isGroupControlsDefinition;
2152
2250
  exports.isIconAdornment = isIconAdornment;
2153
2251
  exports.isScalarField = isScalarField;
2252
+ exports.jsonPathString = jsonPathString;
2154
2253
  exports.jsonataExpr = jsonataExpr;
2155
2254
  exports.layoutKeyForPlacement = layoutKeyForPlacement;
2255
+ exports.lookupChildControl = lookupChildControl;
2156
2256
  exports.lookupSchemaField = lookupSchemaField;
2157
2257
  exports.makeCompoundField = makeCompoundField;
2158
2258
  exports.makeEvalExpressionHook = makeEvalExpressionHook;