@react-typed-forms/schemas 10.2.0 → 10.4.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.
@@ -117,6 +117,7 @@ export interface DataRendererProps extends ParentRendererProps {
117
117
  export interface ActionRendererProps {
118
118
  actionId: string;
119
119
  actionText: string;
120
+ actionData?: any;
120
121
  onClick: () => void;
121
122
  className?: string;
122
123
  style?: React.CSSProperties;
@@ -146,6 +147,7 @@ export interface DataControlProps {
146
147
  export type CreateDataProps = (controlProps: DataControlProps) => DataRendererProps;
147
148
  export interface ControlRenderOptions extends FormContextOptions {
148
149
  useDataHook?: (c: ControlDefinition) => CreateDataProps;
150
+ actionOnClick?: (actionId: string, actionData: any) => () => void;
149
151
  useEvalExpressionHook?: UseEvalExpressionHook;
150
152
  clearHidden?: boolean;
151
153
  schemaInterface?: SchemaInterface;
@@ -184,7 +186,9 @@ export interface RenderControlProps {
184
186
  displayControl?: Control<string | undefined>;
185
187
  style?: React.CSSProperties;
186
188
  allowedOptions?: Control<any[] | undefined>;
189
+ actionDataControl?: Control<any | undefined | null>;
187
190
  useChildVisibility: ChildVisibilityFunc;
191
+ actionOnClick?: (actionId: string, actionData: any) => () => void;
188
192
  }
189
193
  export declare function renderControlLayout(props: RenderControlProps): ControlLayoutProps;
190
194
  export declare function appendMarkup(k: keyof Omit<RenderedLayout, "errorControl" | "style" | "className">, markup: ReactNode): (layout: RenderedLayout) => void;
package/lib/hooks.d.ts CHANGED
@@ -18,4 +18,5 @@ export declare function useEvalDynamicHook(definition: ControlDefinition, type:
18
18
  export declare function matchesType(context: ControlDataContext, types?: string[] | null): boolean | undefined;
19
19
  export declare function hideDisplayOnly(context: ControlDataContext, field: SchemaField, definition: ControlDefinition, schemaInterface: SchemaInterface): boolean | undefined;
20
20
  export declare function useJsonataExpression(jExpr: string, dataContext: DataContext, bindings?: () => Record<string, any>): Control<any>;
21
+ export declare function useEvalActionHook(useExpr: UseEvalExpressionHook, definition: ControlDefinition): EvalExpressionHook<string | null>;
21
22
  export declare function useEvalLabelText(useExpr: UseEvalExpressionHook, definition: ControlDefinition): EvalExpressionHook<string | null>;
package/lib/index.js CHANGED
@@ -41,6 +41,7 @@ exports.DynamicPropertyType = void 0;
41
41
  DynamicPropertyType["LayoutStyle"] = "LayoutStyle";
42
42
  DynamicPropertyType["AllowedOptions"] = "AllowedOptions";
43
43
  DynamicPropertyType["Label"] = "Label";
44
+ DynamicPropertyType["ActionData"] = "ActionData";
44
45
  })(exports.DynamicPropertyType || (exports.DynamicPropertyType = {}));
45
46
  exports.ExpressionType = void 0;
46
47
  (function (ExpressionType) {
@@ -313,49 +314,100 @@ function defaultControlForField(sf) {
313
314
  }
314
315
  throw "Unknown schema field";
315
316
  }
316
- function findReferencedControl(field, control) {
317
- if (isDataControl(control) && field === control.field) return control;
318
- if (isGroupControl(control)) {
319
- var _control$children;
320
- if (control.compoundField) return field === control.compoundField ? control : undefined;
321
- return findReferencedControlInArray(field, (_control$children = control.children) != null ? _control$children : []);
317
+ function findControlsForCompound(compound, definition) {
318
+ if (isDataControlDefinition(definition) && compound.field === definition.field) {
319
+ return [definition];
322
320
  }
323
- return undefined;
321
+ if (isGroupControlsDefinition(definition)) {
322
+ var _definition$children$, _definition$children;
323
+ if (definition.compoundField === compound.field) return [definition];
324
+ return (_definition$children$ = (_definition$children = definition.children) == null ? void 0 : _definition$children.flatMap(function (d) {
325
+ return findControlsForCompound(compound, d);
326
+ })) != null ? _definition$children$ : [];
327
+ }
328
+ return [];
324
329
  }
325
- function findReferencedControlInArray(field, controls) {
326
- for (var _iterator = _createForOfIteratorHelperLoose(controls), _step; !(_step = _iterator()).done;) {
327
- var c = _step.value;
328
- var ref = findReferencedControl(field, c);
329
- if (ref) return ref;
330
+ function findCompoundGroups(fields, controls) {
331
+ return Object.fromEntries(fields.filter(isCompoundField).map(function (cf) {
332
+ var groups = controls.flatMap(function (x) {
333
+ return findControlsForCompound(cf, x);
334
+ });
335
+ return [cf.field, {
336
+ groups: groups.concat(findNonDataGroups(groups.flatMap(function (x) {
337
+ var _x$children;
338
+ return (_x$children = x.children) != null ? _x$children : [];
339
+ }))),
340
+ children: findCompoundGroups(cf.children, groups.flatMap(function (x) {
341
+ var _x$children2;
342
+ return (_x$children2 = x.children) != null ? _x$children2 : [];
343
+ }))
344
+ }];
345
+ }));
346
+ }
347
+ function existsInGroups(field, lookup) {
348
+ var itself = lookup.groups.find(function (c) {
349
+ var _c$children;
350
+ return (_c$children = c.children) == null ? void 0 : _c$children.find(function (x) {
351
+ return isDataControlDefinition(x) && x.field === field.field || isGroupControlsDefinition(x) && x.compoundField === field.field;
352
+ });
353
+ });
354
+ if (!itself) return [[field, lookup]];
355
+ if (isCompoundField(field)) {
356
+ var childLookup = lookup.children[field.field];
357
+ if (!childLookup) return [[field, lookup]];
358
+ return field.children.flatMap(function (c) {
359
+ return existsInGroups(c, childLookup);
360
+ });
330
361
  }
331
- return undefined;
362
+ return [];
363
+ }
364
+ function findNonDataGroups(controls) {
365
+ return controls.flatMap(function (control) {
366
+ var _control$children2;
367
+ return isGroupControlsDefinition(control) && !control.compoundField ? [control].concat(findNonDataGroups((_control$children2 = control.children) != null ? _control$children2 : [])) : [];
368
+ });
369
+ }
370
+ function cloneChildren(c) {
371
+ var _c$children2;
372
+ if (c.children) return _extends({}, c, {
373
+ children: (_c$children2 = c.children) == null ? void 0 : _c$children2.map(cloneChildren)
374
+ });
375
+ return c;
332
376
  }
333
377
  function addMissingControls(fields, controls) {
334
- var changes = fields.flatMap(function (x) {
335
- if (fieldHasTag(x, "_NoControl")) return [];
336
- var existing = findReferencedControlInArray(x.field, controls);
337
- if (!existing || isCompoundField(x)) return {
338
- field: x,
339
- existing: existing
340
- };
341
- return [];
378
+ controls = controls.map(cloneChildren);
379
+ var rootMapping = findCompoundGroups(fields, controls);
380
+ var rootGroups = findNonDataGroups([{
381
+ type: exports.ControlDefinitionType.Group,
382
+ children: controls
383
+ }]);
384
+ var rootLookup = {
385
+ children: rootMapping,
386
+ groups: rootGroups
387
+ };
388
+ var missingFields = fields.filter(function (x) {
389
+ return !fieldHasTag(x, "_NoControl");
390
+ }).flatMap(function (x) {
391
+ return existsInGroups(x, rootLookup);
342
392
  });
343
- var changedCompounds = controls.map(function (x) {
344
- var _cf$children;
345
- var ex = changes.find(function (c) {
346
- return c.existing === x;
347
- });
348
- if (!ex) return x;
349
- var cf = x;
350
- return _extends({}, cf, {
351
- children: addMissingControls(ex.field.children, (_cf$children = cf.children) != null ? _cf$children : [])
393
+ missingFields.forEach(function (_ref) {
394
+ var _f$tags, _insertGroup;
395
+ var f = _ref[0],
396
+ lookup = _ref[1];
397
+ var groupToAdd = (_f$tags = f.tags) == null ? void 0 : _f$tags.find(function (x) {
398
+ return x.startsWith("_ControlGroup:");
352
399
  });
400
+ var insertGroup = undefined;
401
+ if (groupToAdd) {
402
+ var groupName = groupToAdd.substring(14);
403
+ insertGroup = lookup.groups.find(function (x) {
404
+ return x.title === groupName;
405
+ });
406
+ }
407
+ if (!insertGroup) insertGroup = lookup.groups[0];
408
+ (_insertGroup = insertGroup) == null || (_insertGroup = _insertGroup.children) == null || _insertGroup.push(defaultControlForField(f));
353
409
  });
354
- return changedCompounds.concat(changes.filter(function (x) {
355
- return !x.existing;
356
- }).map(function (x) {
357
- return defaultControlForField(x.field);
358
- }));
410
+ return controls;
359
411
  }
360
412
  function useUpdatedRef(a) {
361
413
  var r = React.useRef(a);
@@ -472,8 +524,8 @@ function cleanDataForSchema(v, fields) {
472
524
  return out;
473
525
  }
474
526
  function getAllReferencedClasses(c, collectExtra) {
475
- var _c$children, _collectExtra;
476
- var childClasses = (_c$children = c.children) == null ? void 0 : _c$children.flatMap(function (x) {
527
+ var _c$children3, _collectExtra;
528
+ var childClasses = (_c$children3 = c.children) == null ? void 0 : _c$children3.flatMap(function (x) {
477
529
  return getAllReferencedClasses(x, collectExtra);
478
530
  });
479
531
  var tc = clsx__default["default"]([c.styleClass, c.layoutClass, c.labelClass].concat((_collectExtra = collectExtra == null ? void 0 : collectExtra(c)) != null ? _collectExtra : []).map(getOverrideClass));
@@ -543,8 +595,8 @@ function makeHook(runHook, state, deps) {
543
595
  }
544
596
  function useDynamicHooks(hooks) {
545
597
  var hookEntries = Object.entries(hooks);
546
- var deps = hookEntries.map(function (_ref) {
547
- var x = _ref[1];
598
+ var deps = hookEntries.map(function (_ref2) {
599
+ var x = _ref2[1];
548
600
  return toDepString(x.deps);
549
601
  }).join(",");
550
602
  var ref = React.useRef({});
@@ -553,9 +605,9 @@ function useDynamicHooks(hooks) {
553
605
  return s[x[0]] = x[1].state;
554
606
  });
555
607
  return React.useCallback(function (p) {
556
- return Object.fromEntries(hookEntries.map(function (_ref2) {
557
- var f = _ref2[0],
558
- hg = _ref2[1];
608
+ return Object.fromEntries(hookEntries.map(function (_ref3) {
609
+ var f = _ref3[0],
610
+ hg = _ref3[1];
559
611
  return [f, hg.runHook(p, ref.current[f])];
560
612
  }));
561
613
  }, [deps]);
@@ -1056,6 +1108,12 @@ function useJsonataExpression(jExpr, dataContext, bindings) {
1056
1108
  }, [compiledExpr]);
1057
1109
  return control;
1058
1110
  }
1111
+ function useEvalActionHook(useExpr, definition) {
1112
+ var dynamicValue = useEvalDynamicHook(definition, exports.DynamicPropertyType.ActionData, useExpr);
1113
+ return makeDynamicPropertyHook(dynamicValue, function () {
1114
+ return core.useControl(null);
1115
+ }, undefined);
1116
+ }
1059
1117
  function useEvalLabelText(useExpr, definition) {
1060
1118
  var dynamicValue = useEvalDynamicHook(definition, exports.DynamicPropertyType.Label, useExpr);
1061
1119
  return makeDynamicPropertyHook(dynamicValue, function () {
@@ -1225,6 +1283,7 @@ function useControlRenderer(definition, fields, renderer, options) {
1225
1283
  disabledControl: useEvalDisabledHook(useExpr, definition),
1226
1284
  allowedOptions: useEvalAllowedOptionsHook(useExpr, definition),
1227
1285
  labelText: useEvalLabelText(useExpr, definition),
1286
+ actionData: useEvalActionHook(useExpr, definition),
1228
1287
  customStyle: useEvalStyleHook(useExpr, exports.DynamicPropertyType.Style, definition),
1229
1288
  layoutStyle: useEvalStyleHook(useExpr, exports.DynamicPropertyType.LayoutStyle, definition),
1230
1289
  displayControl: useEvalDisplayHook(useExpr, definition)
@@ -1265,7 +1324,8 @@ function useControlRenderer(definition, fields, renderer, options) {
1265
1324
  labelText = _dynamicHooks.labelText,
1266
1325
  customStyle = _dynamicHooks.customStyle,
1267
1326
  allowedOptions = _dynamicHooks.allowedOptions,
1268
- defaultValueControl = _dynamicHooks.defaultValueControl;
1327
+ defaultValueControl = _dynamicHooks.defaultValueControl,
1328
+ actionData = _dynamicHooks.actionData;
1269
1329
  var visible = visibleControl.current.value;
1270
1330
  var visibility = core.useControl(function () {
1271
1331
  return visible != null ? {
@@ -1359,6 +1419,8 @@ function useControlRenderer(definition, fields, renderer, options) {
1359
1419
  displayControl: displayControl,
1360
1420
  style: customStyle.value,
1361
1421
  allowedOptions: allowedOptions,
1422
+ actionDataControl: actionData,
1423
+ actionOnClick: _options.actionOnClick,
1362
1424
  useChildVisibility: function useChildVisibility(childDef, context) {
1363
1425
  var schemaField = lookupSchemaField(childDef, (context != null ? context : controlDataContext).fields);
1364
1426
  return useEvalVisibilityHook(useExpr, childDef, schemaField);
@@ -1542,12 +1604,14 @@ function renderControlLayout(props) {
1542
1604
  };
1543
1605
  }
1544
1606
  if (isActionControlsDefinition(c)) {
1545
- var _ref5, _labelText$value2;
1607
+ var _props$actionDataCont, _props$actionDataCont2, _ref5, _labelText$value2, _props$actionOnClick;
1608
+ var actionData = (_props$actionDataCont = (_props$actionDataCont2 = props.actionDataControl) == null ? void 0 : _props$actionDataCont2.value) != null ? _props$actionDataCont : c.actionData;
1546
1609
  return {
1547
1610
  children: renderer.renderAction({
1548
1611
  actionText: (_ref5 = (_labelText$value2 = labelText == null ? void 0 : labelText.value) != null ? _labelText$value2 : c.title) != null ? _ref5 : c.actionId,
1549
1612
  actionId: c.actionId,
1550
- onClick: function onClick() {},
1613
+ actionData: actionData,
1614
+ onClick: (_props$actionOnClick = props.actionOnClick == null ? void 0 : props.actionOnClick(c.actionId, actionData)) != null ? _props$actionOnClick : function () {},
1551
1615
  className: cc(c.styleClass),
1552
1616
  style: style
1553
1617
  })
@@ -2504,7 +2568,7 @@ function createFormRenderer(customRenderers, defaultRenderers) {
2504
2568
  var options = hasOptions(props);
2505
2569
  var renderer = (_dataRegistrations$fi = dataRegistrations.find(function (x) {
2506
2570
  var _x$collection, _field$collection, _x$options;
2507
- return ((_x$collection = x.collection) != null ? _x$collection : false) === (props.elementIndex == null && ((_field$collection = field.collection) != null ? _field$collection : false)) && ((_x$options = x.options) != null ? _x$options : false) === options && (x.schemaType && isOneOf(x.schemaType, field.type) || x.renderType && isOneOf(x.renderType, renderType) || x.match && x.match(props));
2571
+ return ((_x$collection = x.collection) != null ? _x$collection : false) === (props.elementIndex == null && ((_field$collection = field.collection) != null ? _field$collection : false)) && ((_x$options = x.options) != null ? _x$options : false) === options && (x.schemaType && renderType == exports.DataRenderType.Standard && isOneOf(x.schemaType, field.type) || x.renderType && isOneOf(x.renderType, renderType) || x.match && x.match(props));
2508
2572
  })) != null ? _dataRegistrations$fi : defaultRenderers.data;
2509
2573
  var result = renderer.render(props, formRenderers);
2510
2574
  if (typeof result === "function") return result;
@@ -2670,12 +2734,16 @@ exports.dynamicReadonly = dynamicReadonly;
2670
2734
  exports.dynamicVisibility = dynamicVisibility;
2671
2735
  exports.elementValueForField = elementValueForField;
2672
2736
  exports.emptyGroupDefinition = emptyGroupDefinition;
2737
+ exports.existsInGroups = existsInGroups;
2673
2738
  exports.fieldDisplayName = fieldDisplayName;
2674
2739
  exports.fieldEqExpr = fieldEqExpr;
2675
2740
  exports.fieldHasTag = fieldHasTag;
2676
2741
  exports.findChildDefinition = findChildDefinition;
2677
2742
  exports.findCompoundField = findCompoundField;
2743
+ exports.findCompoundGroups = findCompoundGroups;
2744
+ exports.findControlsForCompound = findControlsForCompound;
2678
2745
  exports.findField = findField;
2746
+ exports.findNonDataGroups = findNonDataGroups;
2679
2747
  exports.findScalarField = findScalarField;
2680
2748
  exports.getAllReferencedClasses = getAllReferencedClasses;
2681
2749
  exports.getControlData = getControlData;
@@ -2726,6 +2794,7 @@ exports.toDepString = toDepString;
2726
2794
  exports.useControlDefinitionForSchema = useControlDefinitionForSchema;
2727
2795
  exports.useControlRenderer = useControlRenderer;
2728
2796
  exports.useDynamicHooks = useDynamicHooks;
2797
+ exports.useEvalActionHook = useEvalActionHook;
2729
2798
  exports.useEvalAllowedOptionsHook = useEvalAllowedOptionsHook;
2730
2799
  exports.useEvalDefaultValueHook = useEvalDefaultValueHook;
2731
2800
  exports.useEvalDisabledHook = useEvalDisabledHook;