@react-typed-forms/schemas 7.3.2 → 8.0.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.
@@ -14,3 +14,9 @@ export declare function compoundControl(field: string, title: string | undefined
14
14
  export declare function createAction(actionId: string, onClick: () => void, actionText?: string): ActionRendererProps;
15
15
  export declare const emptyGroupDefinition: GroupedControlsDefinition;
16
16
  export declare function useControlDefinitionForSchema(sf: SchemaField[], definition?: GroupedControlsDefinition): GroupedControlsDefinition;
17
+ export interface CustomRenderOptions {
18
+ value: string;
19
+ name: string;
20
+ fields: SchemaField[];
21
+ }
22
+ export declare function addCustomDataRenderOptions(controlFields: SchemaField[], customRenderOptions: CustomRenderOptions[]): SchemaField[];
@@ -86,14 +86,13 @@ export interface DisplayRendererProps {
86
86
  style?: React.CSSProperties;
87
87
  }
88
88
  export interface GroupRendererProps {
89
+ children: ControlDefinition[];
89
90
  renderOptions: GroupRenderOptions;
90
- childCount: number;
91
- renderChild: (child: number) => ReactNode;
91
+ renderChild: ChildRenderer;
92
92
  className?: string;
93
93
  style?: React.CSSProperties;
94
94
  }
95
95
  export interface DataRendererProps {
96
- definition: DataControlDefinition;
97
96
  renderOptions: RenderOptions;
98
97
  field: SchemaField;
99
98
  id: string;
@@ -105,7 +104,7 @@ export interface DataRendererProps {
105
104
  className?: string;
106
105
  style?: React.CSSProperties;
107
106
  dataContext: ControlDataContext;
108
- childCount: number;
107
+ children: ControlDefinition[];
109
108
  renderChild: ChildRenderer;
110
109
  toArrayProps?: () => ArrayRendererProps;
111
110
  }
@@ -132,7 +131,6 @@ export interface DataControlProps {
132
131
  control: Control<any>;
133
132
  options: FormContextOptions;
134
133
  style: React.CSSProperties | undefined;
135
- childCount: number;
136
134
  renderChild: ChildRenderer;
137
135
  allowedOptions?: Control<any[] | undefined>;
138
136
  elementRenderer?: (elemIndex: number) => ReactNode;
@@ -152,13 +150,20 @@ export interface ControlRenderOptions extends FormContextOptions {
152
150
  export declare function useControlRenderer(definition: ControlDefinition, fields: SchemaField[], renderer: FormRenderer, options?: ControlRenderOptions): FC<ControlRenderProps>;
153
151
  export declare function lookupSchemaField(c: ControlDefinition, fields: SchemaField[]): SchemaField | undefined;
154
152
  export declare function getControlData(schemaField: SchemaField | undefined, parentContext: ControlDataContext): [Control<any> | undefined, Control<any> | undefined, ControlDataContext];
153
+ export declare function ControlRenderer({ definition, fields, renderer, options, control, parentPath, }: {
154
+ definition: ControlDefinition;
155
+ fields: SchemaField[];
156
+ renderer: FormRenderer;
157
+ options?: ControlRenderOptions;
158
+ control: Control<any>;
159
+ parentPath?: JsonPath[];
160
+ }): React.JSX.Element;
155
161
  export declare function defaultDataProps({ definition, field, control, options, elementRenderer, style, allowedOptions, ...props }: DataControlProps): DataRendererProps;
156
162
  export declare function defaultArrayProps(arrayControl: Control<any[] | undefined | null>, field: SchemaField, required: boolean, style: CSSProperties | undefined, className: string | undefined, renderElement: (elemIndex: number) => ReactNode): ArrayRendererProps;
157
- export type ChildRenderer = (k: Key, childIndex: number, props: ControlRenderProps) => ReactNode;
163
+ export type ChildRenderer = (k: Key, child: number | number[], parentPath?: JsonPath[]) => ReactNode;
158
164
  export interface RenderControlProps {
159
165
  definition: ControlDefinition;
160
166
  renderer: FormRenderer;
161
- childCount: number;
162
167
  renderChild: ChildRenderer;
163
168
  createDataProps: CreateDataProps;
164
169
  formOptions: FormContextOptions;
@@ -169,7 +174,7 @@ export interface RenderControlProps {
169
174
  style?: React.CSSProperties;
170
175
  allowedOptions?: Control<any[] | undefined>;
171
176
  }
172
- export declare function renderControlLayout({ definition: c, renderer, childCount, renderChild: childRenderer, control: childControl, schemaField, dataContext, formOptions: dataOptions, createDataProps: dataProps, displayControl, style, allowedOptions, }: RenderControlProps): ControlLayoutProps;
177
+ export declare function renderControlLayout({ definition: c, renderer, renderChild: childRenderer, control: childControl, schemaField, dataContext, formOptions: dataOptions, createDataProps: dataProps, displayControl, style, allowedOptions, }: RenderControlProps): ControlLayoutProps;
173
178
  export declare function appendMarkup(k: keyof Omit<RenderedLayout, "errorControl" | "style" | "className">, markup: ReactNode): (layout: RenderedLayout) => void;
174
179
  export declare function wrapMarkup(k: keyof Omit<RenderedLayout, "errorControl" | "style" | "className">, wrap: (ex: ReactNode) => ReactNode): (layout: RenderedLayout) => void;
175
180
  export declare function layoutKeyForPlacement(pos: AdornmentPlacement): keyof Omit<RenderedLayout, "errorControl" | "style" | "className">;
package/lib/index.js CHANGED
@@ -268,6 +268,27 @@ function defaultCompoundField(field, displayName, collection) {
268
268
  children: []
269
269
  };
270
270
  }
271
+ function mergeField(field, mergeInto) {
272
+ var existing = mergeInto.find(function (x) {
273
+ return x.field === field.field;
274
+ });
275
+ if (existing) {
276
+ return mergeInto.map(function (x) {
277
+ return x !== existing ? x : _extends({}, x, {
278
+ onlyForTypes: mergeTypes(x.onlyForTypes, field.onlyForTypes)
279
+ });
280
+ });
281
+ }
282
+ return [].concat(mergeInto, [field]);
283
+ function mergeTypes(f, s) {
284
+ if (!f) return s;
285
+ if (!s) return f;
286
+ var extras = s.filter(function (x) {
287
+ return !f.includes(x);
288
+ });
289
+ return extras.length ? [].concat(f, extras) : f;
290
+ }
291
+ }
271
292
 
272
293
  function applyDefaultValues(v, fields) {
273
294
  if (!v) return defaultValueForFields(fields);
@@ -557,6 +578,16 @@ function jsonPathString(jsonPath) {
557
578
  });
558
579
  return out;
559
580
  }
581
+ function findChildDefinition(parent, childPath) {
582
+ if (Array.isArray(childPath)) {
583
+ var base = parent;
584
+ childPath.forEach(function (x) {
585
+ return base = base.children[x];
586
+ });
587
+ return base;
588
+ }
589
+ return parent.children[childPath];
590
+ }
560
591
 
561
592
  function dataControl(field, title, options) {
562
593
  return _extends({
@@ -668,6 +699,42 @@ function useControlDefinitionForSchema(sf, definition) {
668
699
  });
669
700
  }, [sf, definition]);
670
701
  }
702
+ function addCustomDataRenderOptions(controlFields, customRenderOptions) {
703
+ return controlFields.map(function (x) {
704
+ return x.field === "renderOptions" && isCompoundField(x) ? addRenderOptions(x) : x;
705
+ });
706
+ function addRenderOptions(roField) {
707
+ var children = roField.children;
708
+ var withTypes = children.map(function (x) {
709
+ return x.field === "type" ? addRenderOptionType(x) : x;
710
+ });
711
+ return _extends({}, roField, {
712
+ children: customRenderOptions.reduce(function (renderOptionFields, ro) {
713
+ return ro.fields.map(function (x) {
714
+ return _extends({}, x, {
715
+ onlyForTypes: [ro.value]
716
+ });
717
+ }).reduce(function (af, x) {
718
+ return mergeField(x, af);
719
+ }, renderOptionFields);
720
+ }, withTypes)
721
+ });
722
+ }
723
+ function addRenderOptionType(typeField) {
724
+ var _typeField$options;
725
+ var options = (_typeField$options = typeField.options) != null ? _typeField$options : [];
726
+ return _extends({}, typeField, {
727
+ options: [].concat(options, customRenderOptions.map(function (_ref) {
728
+ var name = _ref.name,
729
+ value = _ref.value;
730
+ return {
731
+ name: name,
732
+ value: value
733
+ };
734
+ }))
735
+ });
736
+ }
737
+ }
671
738
 
672
739
  function useCalculatedControl(calculate) {
673
740
  var c = core.useControl(calculate);
@@ -847,14 +914,15 @@ function hideDisplayOnly(context, field, definition, schemaInterface) {
847
914
  }
848
915
  function useJsonataExpression(jExpr, dataContext, bindings) {
849
916
  var pathString = jsonPathString(dataContext.path);
917
+ var fullExpr = pathString ? pathString + ".(" + jExpr + ")" : jExpr;
850
918
  var compiledExpr = React.useMemo(function () {
851
919
  try {
852
- return jsonata__default["default"](pathString ? pathString + ".(" + jExpr + ")" : jExpr);
920
+ return jsonata__default["default"](fullExpr);
853
921
  } catch (e) {
854
922
  console.error(e);
855
923
  return jsonata__default["default"]("null");
856
924
  }
857
- }, [jExpr, pathString]);
925
+ }, [fullExpr]);
858
926
  var control = core.useControl();
859
927
  var listenerRef = React.useRef();
860
928
  var _useRefState = core.useRefState(function () {
@@ -1021,7 +1089,7 @@ function useControlRenderer(definition, fields, renderer, options) {
1021
1089
  parentPath = _ref$parentPath === void 0 ? [] : _ref$parentPath;
1022
1090
  var stopTracking = core.useComponentTracking();
1023
1091
  try {
1024
- var _c$children$map, _c$children, _definition$adornment, _definition$adornment2;
1092
+ var _definition$adornment, _definition$adornment2;
1025
1093
  var _r$current = r.current,
1026
1094
  c = _r$current.definition,
1027
1095
  _options = _r$current.options,
@@ -1092,9 +1160,7 @@ function useControlRenderer(definition, fields, renderer, options) {
1092
1160
  };
1093
1161
  }).value;
1094
1162
  useValidation(control != null ? control : core.newControl(null), !!myOptions.hidden, parentDataContext);
1095
- var childRenderers = (_c$children$map = (_c$children = c.children) == null ? void 0 : _c$children.map(function (cd) {
1096
- return useControlRenderer(cd, controlDataContext.fields, renderer, _extends({}, _options, myOptions));
1097
- })) != null ? _c$children$map : [];
1163
+ var childOptions = _extends({}, _options, myOptions);
1098
1164
  React.useEffect(function () {
1099
1165
  if (control && typeof myOptions.disabled === "boolean") control.disabled = myOptions.disabled;
1100
1166
  }, [control, myOptions.disabled]);
@@ -1107,12 +1173,16 @@ function useControlRenderer(definition, fields, renderer, options) {
1107
1173
  var labelAndChildren = renderControlLayout({
1108
1174
  definition: c,
1109
1175
  renderer: renderer,
1110
- childCount: childRenderers.length,
1111
- renderChild: function renderChild(k, i, props) {
1112
- var RenderChild = childRenderers[i];
1113
- return /*#__PURE__*/React__default["default"].createElement(RenderChild, _extends({
1114
- key: k
1115
- }, props));
1176
+ renderChild: function renderChild(k, child, path) {
1177
+ return /*#__PURE__*/React__default["default"].createElement(ControlRenderer, {
1178
+ key: k,
1179
+ control: controlDataContext.data,
1180
+ fields: controlDataContext.fields,
1181
+ definition: findChildDefinition(c, child),
1182
+ parentPath: path ? [].concat(controlDataContext.path, path) : controlDataContext.path,
1183
+ renderer: renderer,
1184
+ options: childOptions
1185
+ });
1116
1186
  },
1117
1187
  createDataProps: dataProps,
1118
1188
  formOptions: myOptions,
@@ -1154,41 +1224,52 @@ function getControlData(schemaField, parentContext) {
1154
1224
  fields: isCompoundField(schemaField) ? schemaField.children : parentContext.fields
1155
1225
  }) : parentContext];
1156
1226
  }
1157
- function groupProps(renderOptions, childCount, _renderChild, data, className, style) {
1158
- if (renderOptions === void 0) {
1159
- renderOptions = {
1160
- type: "Standard"
1161
- };
1227
+ function ControlRenderer(_ref3) {
1228
+ var _effect = core.useComponentTracking();
1229
+ try {
1230
+ var definition = _ref3.definition,
1231
+ fields = _ref3.fields,
1232
+ renderer = _ref3.renderer,
1233
+ options = _ref3.options,
1234
+ control = _ref3.control,
1235
+ parentPath = _ref3.parentPath;
1236
+ var Render = useControlRenderer(definition, fields, renderer, options);
1237
+ return /*#__PURE__*/React__default["default"].createElement(Render, {
1238
+ control: control,
1239
+ parentPath: parentPath
1240
+ });
1241
+ } finally {
1242
+ _effect();
1162
1243
  }
1244
+ }
1245
+ function groupProps(definition, renderChild, data, className, style) {
1246
+ var _definition$children, _definition$groupOpti;
1163
1247
  return {
1164
- childCount: childCount,
1165
- renderChild: function renderChild(i) {
1166
- return _renderChild(i, i, {
1167
- control: data.data,
1168
- parentPath: data.path
1169
- });
1248
+ children: (_definition$children = definition.children) != null ? _definition$children : [],
1249
+ renderChild: renderChild,
1250
+ renderOptions: (_definition$groupOpti = definition.groupOptions) != null ? _definition$groupOpti : {
1251
+ type: "Standard"
1170
1252
  },
1171
- renderOptions: renderOptions,
1172
1253
  className: cc(className),
1173
1254
  style: style
1174
1255
  };
1175
1256
  }
1176
- function defaultDataProps(_ref3) {
1177
- var _field$options$length, _field$options, _allowedOptions$value, _definition$renderOpt;
1178
- var definition = _ref3.definition,
1179
- field = _ref3.field,
1180
- control = _ref3.control,
1181
- options = _ref3.options,
1182
- elementRenderer = _ref3.elementRenderer,
1183
- style = _ref3.style,
1184
- allowedOptions = _ref3.allowedOptions,
1185
- props = _objectWithoutPropertiesLoose(_ref3, _excluded$1);
1257
+ function defaultDataProps(_ref4) {
1258
+ var _field$options$length, _field$options, _allowedOptions$value, _definition$children2, _definition$renderOpt;
1259
+ var definition = _ref4.definition,
1260
+ field = _ref4.field,
1261
+ control = _ref4.control,
1262
+ options = _ref4.options,
1263
+ elementRenderer = _ref4.elementRenderer,
1264
+ style = _ref4.style,
1265
+ allowedOptions = _ref4.allowedOptions,
1266
+ props = _objectWithoutPropertiesLoose(_ref4, _excluded$1);
1186
1267
  var className = cc(definition.styleClass);
1187
1268
  var required = !!definition.required;
1188
1269
  var fieldOptions = ((_field$options$length = (_field$options = field.options) == null ? void 0 : _field$options.length) != null ? _field$options$length : 0) === 0 ? null : field.options;
1189
1270
  var allowed = (_allowedOptions$value = allowedOptions == null ? void 0 : allowedOptions.value) != null ? _allowedOptions$value : [];
1190
1271
  return _extends({
1191
- definition: definition,
1272
+ children: (_definition$children2 = definition.children) != null ? _definition$children2 : [],
1192
1273
  control: control,
1193
1274
  field: field,
1194
1275
  id: "c" + control.uniqueId,
@@ -1243,19 +1324,18 @@ function defaultArrayProps(arrayControl, field, required, style, className, _ren
1243
1324
  style: style
1244
1325
  };
1245
1326
  }
1246
- function renderControlLayout(_ref4) {
1247
- var c = _ref4.definition,
1248
- renderer = _ref4.renderer,
1249
- childCount = _ref4.childCount,
1250
- childRenderer = _ref4.renderChild,
1251
- childControl = _ref4.control,
1252
- schemaField = _ref4.schemaField,
1253
- dataContext = _ref4.dataContext,
1254
- dataOptions = _ref4.formOptions,
1255
- dataProps = _ref4.createDataProps,
1256
- displayControl = _ref4.displayControl,
1257
- style = _ref4.style,
1258
- allowedOptions = _ref4.allowedOptions;
1327
+ function renderControlLayout(_ref5) {
1328
+ var c = _ref5.definition,
1329
+ renderer = _ref5.renderer,
1330
+ childRenderer = _ref5.renderChild,
1331
+ childControl = _ref5.control,
1332
+ schemaField = _ref5.schemaField,
1333
+ dataContext = _ref5.dataContext,
1334
+ dataOptions = _ref5.formOptions,
1335
+ dataProps = _ref5.createDataProps,
1336
+ displayControl = _ref5.displayControl,
1337
+ style = _ref5.style,
1338
+ allowedOptions = _ref5.allowedOptions;
1259
1339
  if (isDataControlDefinition(c)) {
1260
1340
  return renderData(c);
1261
1341
  }
@@ -1269,7 +1349,7 @@ function renderControlLayout(_ref4) {
1269
1349
  }));
1270
1350
  }
1271
1351
  return {
1272
- processLayout: renderer.renderGroup(groupProps(c.groupOptions, childCount, childRenderer, dataContext, c.styleClass, style)),
1352
+ processLayout: renderer.renderGroup(groupProps(c, childRenderer, dataContext, c.styleClass, style)),
1273
1353
  label: {
1274
1354
  label: c.title,
1275
1355
  type: exports.LabelType.Group,
@@ -1317,9 +1397,10 @@ function renderControlLayout(_ref4) {
1317
1397
  control: elemIndex != null ? childControl.elements[elemIndex] : childControl,
1318
1398
  options: dataOptions,
1319
1399
  style: style,
1320
- childCount: childCount,
1321
1400
  allowedOptions: allowedOptions,
1322
- renderChild: childRenderer,
1401
+ renderChild: elemIndex != null ? function (k, d, p) {
1402
+ return childRenderer(k, d, p ? [elemIndex].concat(p) : [elemIndex]);
1403
+ } : childRenderer,
1323
1404
  elementRenderer: elemIndex == null && schemaField.collection ? function (ei) {
1324
1405
  return renderLayoutParts(renderData(c, ei), renderer).children;
1325
1406
  } : undefined
@@ -1368,13 +1449,13 @@ function wrapMarkupAt(pos, wrap) {
1368
1449
  }
1369
1450
  function renderLayoutParts(props, renderer) {
1370
1451
  var _props$processLayout;
1371
- var _ref5 = (_props$processLayout = props.processLayout == null ? void 0 : props.processLayout(props)) != null ? _props$processLayout : props,
1372
- className = _ref5.className,
1373
- children = _ref5.children,
1374
- style = _ref5.style,
1375
- errorControl = _ref5.errorControl,
1376
- label = _ref5.label,
1377
- adornments = _ref5.adornments;
1452
+ var _ref6 = (_props$processLayout = props.processLayout == null ? void 0 : props.processLayout(props)) != null ? _props$processLayout : props,
1453
+ className = _ref6.className,
1454
+ children = _ref6.children,
1455
+ style = _ref6.style,
1456
+ errorControl = _ref6.errorControl,
1457
+ label = _ref6.label,
1458
+ adornments = _ref6.adornments;
1378
1459
  var layout = {
1379
1460
  children: children,
1380
1461
  errorControl: errorControl,
@@ -1609,9 +1690,9 @@ function createDefaultGroupRenderer(options) {
1609
1690
  };
1610
1691
  }
1611
1692
  function render(props) {
1612
- var childCount = props.childCount,
1613
- renderChild = props.renderChild,
1614
- renderOptions = props.renderOptions;
1693
+ var renderChild = props.renderChild,
1694
+ renderOptions = props.renderOptions,
1695
+ children = props.children;
1615
1696
  var _ref7 = isGridRenderer(renderOptions) ? gridStyles(renderOptions) : isFlexRenderer(renderOptions) ? flexStyles(renderOptions) : {
1616
1697
  className: standardClassName
1617
1698
  },
@@ -1622,10 +1703,8 @@ function createDefaultGroupRenderer(options) {
1622
1703
  children: /*#__PURE__*/React__default["default"].createElement("div", {
1623
1704
  className: clsx__default["default"](props.className, className, gcn),
1624
1705
  style: style
1625
- }, Array.from({
1626
- length: childCount
1627
- }, function (_, x) {
1628
- return renderChild(x);
1706
+ }, children == null ? void 0 : children.map(function (c, i) {
1707
+ return renderChild(i, i);
1629
1708
  }))
1630
1709
  });
1631
1710
  };
@@ -1715,17 +1794,12 @@ function createDefaultDataRenderer(options) {
1715
1794
  return renderers.renderGroup({
1716
1795
  style: props.style,
1717
1796
  className: props.className,
1797
+ children: props.children,
1718
1798
  renderOptions: {
1719
1799
  type: "Standard",
1720
1800
  hideTitle: true
1721
1801
  },
1722
- renderChild: function renderChild(i) {
1723
- return props.renderChild(i, i, {
1724
- control: props.dataContext.data,
1725
- parentPath: props.dataContext.path
1726
- });
1727
- },
1728
- childCount: props.childCount
1802
+ renderChild: props.renderChild
1729
1803
  });
1730
1804
  }
1731
1805
  var renderOptions = props.renderOptions;
@@ -2164,6 +2238,7 @@ var defaultTailwindTheme = {
2164
2238
 
2165
2239
  exports.AppendAdornmentPriority = AppendAdornmentPriority;
2166
2240
  exports.ControlInput = ControlInput;
2241
+ exports.ControlRenderer = ControlRenderer;
2167
2242
  exports.DefaultBoolOptions = DefaultBoolOptions;
2168
2243
  exports.DefaultDisplay = DefaultDisplay;
2169
2244
  exports.DefaultDisplayOnly = DefaultDisplayOnly;
@@ -2171,6 +2246,7 @@ exports.DefaultLayout = DefaultLayout;
2171
2246
  exports.DefaultVisibility = DefaultVisibility;
2172
2247
  exports.SelectDataRenderer = SelectDataRenderer;
2173
2248
  exports.WrapAdornmentPriority = WrapAdornmentPriority;
2249
+ exports.addCustomDataRenderOptions = addCustomDataRenderOptions;
2174
2250
  exports.addMissingControls = addMissingControls;
2175
2251
  exports.appendMarkup = appendMarkup;
2176
2252
  exports.appendMarkupAt = appendMarkupAt;
@@ -2225,6 +2301,7 @@ exports.emptyGroupDefinition = emptyGroupDefinition;
2225
2301
  exports.fieldDisplayName = fieldDisplayName;
2226
2302
  exports.fieldEqExpr = fieldEqExpr;
2227
2303
  exports.fieldHasTag = fieldHasTag;
2304
+ exports.findChildDefinition = findChildDefinition;
2228
2305
  exports.findCompoundField = findCompoundField;
2229
2306
  exports.findField = findField;
2230
2307
  exports.findScalarField = findScalarField;
@@ -2259,6 +2336,7 @@ exports.makeCompoundField = makeCompoundField;
2259
2336
  exports.makeEvalExpressionHook = makeEvalExpressionHook;
2260
2337
  exports.makeScalarField = makeScalarField;
2261
2338
  exports.matchesType = matchesType;
2339
+ exports.mergeField = mergeField;
2262
2340
  exports.renderControlLayout = renderControlLayout;
2263
2341
  exports.renderLayoutParts = renderLayoutParts;
2264
2342
  exports.stringField = stringField;