@react-typed-forms/schemas 7.3.1 → 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,
@@ -1143,51 +1213,63 @@ function lookupSchemaField(c, fields) {
1143
1213
  return fieldName ? findField(fields, fieldName) : undefined;
1144
1214
  }
1145
1215
  function getControlData(schemaField, parentContext) {
1216
+ var _parentControl$fields;
1146
1217
  var data = parentContext.data,
1147
1218
  path = parentContext.path;
1148
1219
  var parentControl = data.lookupControl(path);
1149
1220
  var childPath = schemaField ? [].concat(path, [schemaField.field]) : path;
1150
- var childControl = schemaField && parentControl ? parentControl.fields[schemaField.field] : undefined;
1221
+ var childControl = schemaField && parentControl ? (_parentControl$fields = parentControl.fields) == null ? void 0 : _parentControl$fields[schemaField.field] : undefined;
1151
1222
  return [parentControl, childControl, schemaField ? _extends({}, parentContext, {
1152
1223
  path: childPath,
1153
1224
  fields: isCompoundField(schemaField) ? schemaField.children : parentContext.fields
1154
1225
  }) : parentContext];
1155
1226
  }
1156
- function groupProps(renderOptions, childCount, _renderChild, data, className, style) {
1157
- if (renderOptions === void 0) {
1158
- renderOptions = {
1159
- type: "Standard"
1160
- };
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();
1161
1243
  }
1244
+ }
1245
+ function groupProps(definition, renderChild, data, className, style) {
1246
+ var _definition$children, _definition$groupOpti;
1162
1247
  return {
1163
- childCount: childCount,
1164
- renderChild: function renderChild(i) {
1165
- return _renderChild(i, i, {
1166
- control: data.data,
1167
- parentPath: data.path
1168
- });
1248
+ children: (_definition$children = definition.children) != null ? _definition$children : [],
1249
+ renderChild: renderChild,
1250
+ renderOptions: (_definition$groupOpti = definition.groupOptions) != null ? _definition$groupOpti : {
1251
+ type: "Standard"
1169
1252
  },
1170
- renderOptions: renderOptions,
1171
1253
  className: cc(className),
1172
1254
  style: style
1173
1255
  };
1174
1256
  }
1175
- function defaultDataProps(_ref3) {
1176
- var _field$options$length, _field$options, _allowedOptions$value, _definition$renderOpt;
1177
- var definition = _ref3.definition,
1178
- field = _ref3.field,
1179
- control = _ref3.control,
1180
- options = _ref3.options,
1181
- elementRenderer = _ref3.elementRenderer,
1182
- style = _ref3.style,
1183
- allowedOptions = _ref3.allowedOptions,
1184
- 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);
1185
1267
  var className = cc(definition.styleClass);
1186
1268
  var required = !!definition.required;
1187
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;
1188
1270
  var allowed = (_allowedOptions$value = allowedOptions == null ? void 0 : allowedOptions.value) != null ? _allowedOptions$value : [];
1189
1271
  return _extends({
1190
- definition: definition,
1272
+ children: (_definition$children2 = definition.children) != null ? _definition$children2 : [],
1191
1273
  control: control,
1192
1274
  field: field,
1193
1275
  id: "c" + control.uniqueId,
@@ -1242,19 +1324,18 @@ function defaultArrayProps(arrayControl, field, required, style, className, _ren
1242
1324
  style: style
1243
1325
  };
1244
1326
  }
1245
- function renderControlLayout(_ref4) {
1246
- var c = _ref4.definition,
1247
- renderer = _ref4.renderer,
1248
- childCount = _ref4.childCount,
1249
- childRenderer = _ref4.renderChild,
1250
- childControl = _ref4.control,
1251
- schemaField = _ref4.schemaField,
1252
- dataContext = _ref4.dataContext,
1253
- dataOptions = _ref4.formOptions,
1254
- dataProps = _ref4.createDataProps,
1255
- displayControl = _ref4.displayControl,
1256
- style = _ref4.style,
1257
- 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;
1258
1339
  if (isDataControlDefinition(c)) {
1259
1340
  return renderData(c);
1260
1341
  }
@@ -1268,7 +1349,7 @@ function renderControlLayout(_ref4) {
1268
1349
  }));
1269
1350
  }
1270
1351
  return {
1271
- processLayout: renderer.renderGroup(groupProps(c.groupOptions, childCount, childRenderer, dataContext, c.styleClass, style)),
1352
+ processLayout: renderer.renderGroup(groupProps(c, childRenderer, dataContext, c.styleClass, style)),
1272
1353
  label: {
1273
1354
  label: c.title,
1274
1355
  type: exports.LabelType.Group,
@@ -1316,9 +1397,10 @@ function renderControlLayout(_ref4) {
1316
1397
  control: elemIndex != null ? childControl.elements[elemIndex] : childControl,
1317
1398
  options: dataOptions,
1318
1399
  style: style,
1319
- childCount: childCount,
1320
1400
  allowedOptions: allowedOptions,
1321
- renderChild: childRenderer,
1401
+ renderChild: elemIndex != null ? function (k, d, p) {
1402
+ return childRenderer(k, d, p ? [elemIndex].concat(p) : [elemIndex]);
1403
+ } : childRenderer,
1322
1404
  elementRenderer: elemIndex == null && schemaField.collection ? function (ei) {
1323
1405
  return renderLayoutParts(renderData(c, ei), renderer).children;
1324
1406
  } : undefined
@@ -1367,13 +1449,13 @@ function wrapMarkupAt(pos, wrap) {
1367
1449
  }
1368
1450
  function renderLayoutParts(props, renderer) {
1369
1451
  var _props$processLayout;
1370
- var _ref5 = (_props$processLayout = props.processLayout == null ? void 0 : props.processLayout(props)) != null ? _props$processLayout : props,
1371
- className = _ref5.className,
1372
- children = _ref5.children,
1373
- style = _ref5.style,
1374
- errorControl = _ref5.errorControl,
1375
- label = _ref5.label,
1376
- 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;
1377
1459
  var layout = {
1378
1460
  children: children,
1379
1461
  errorControl: errorControl,
@@ -1608,9 +1690,9 @@ function createDefaultGroupRenderer(options) {
1608
1690
  };
1609
1691
  }
1610
1692
  function render(props) {
1611
- var childCount = props.childCount,
1612
- renderChild = props.renderChild,
1613
- renderOptions = props.renderOptions;
1693
+ var renderChild = props.renderChild,
1694
+ renderOptions = props.renderOptions,
1695
+ children = props.children;
1614
1696
  var _ref7 = isGridRenderer(renderOptions) ? gridStyles(renderOptions) : isFlexRenderer(renderOptions) ? flexStyles(renderOptions) : {
1615
1697
  className: standardClassName
1616
1698
  },
@@ -1621,10 +1703,8 @@ function createDefaultGroupRenderer(options) {
1621
1703
  children: /*#__PURE__*/React__default["default"].createElement("div", {
1622
1704
  className: clsx__default["default"](props.className, className, gcn),
1623
1705
  style: style
1624
- }, Array.from({
1625
- length: childCount
1626
- }, function (_, x) {
1627
- return renderChild(x);
1706
+ }, children == null ? void 0 : children.map(function (c, i) {
1707
+ return renderChild(i, i);
1628
1708
  }))
1629
1709
  });
1630
1710
  };
@@ -1714,17 +1794,12 @@ function createDefaultDataRenderer(options) {
1714
1794
  return renderers.renderGroup({
1715
1795
  style: props.style,
1716
1796
  className: props.className,
1797
+ children: props.children,
1717
1798
  renderOptions: {
1718
1799
  type: "Standard",
1719
1800
  hideTitle: true
1720
1801
  },
1721
- renderChild: function renderChild(i) {
1722
- return props.renderChild(i, i, {
1723
- control: props.dataContext.data,
1724
- parentPath: props.dataContext.path
1725
- });
1726
- },
1727
- childCount: props.childCount
1802
+ renderChild: props.renderChild
1728
1803
  });
1729
1804
  }
1730
1805
  var renderOptions = props.renderOptions;
@@ -2163,6 +2238,7 @@ var defaultTailwindTheme = {
2163
2238
 
2164
2239
  exports.AppendAdornmentPriority = AppendAdornmentPriority;
2165
2240
  exports.ControlInput = ControlInput;
2241
+ exports.ControlRenderer = ControlRenderer;
2166
2242
  exports.DefaultBoolOptions = DefaultBoolOptions;
2167
2243
  exports.DefaultDisplay = DefaultDisplay;
2168
2244
  exports.DefaultDisplayOnly = DefaultDisplayOnly;
@@ -2170,6 +2246,7 @@ exports.DefaultLayout = DefaultLayout;
2170
2246
  exports.DefaultVisibility = DefaultVisibility;
2171
2247
  exports.SelectDataRenderer = SelectDataRenderer;
2172
2248
  exports.WrapAdornmentPriority = WrapAdornmentPriority;
2249
+ exports.addCustomDataRenderOptions = addCustomDataRenderOptions;
2173
2250
  exports.addMissingControls = addMissingControls;
2174
2251
  exports.appendMarkup = appendMarkup;
2175
2252
  exports.appendMarkupAt = appendMarkupAt;
@@ -2224,6 +2301,7 @@ exports.emptyGroupDefinition = emptyGroupDefinition;
2224
2301
  exports.fieldDisplayName = fieldDisplayName;
2225
2302
  exports.fieldEqExpr = fieldEqExpr;
2226
2303
  exports.fieldHasTag = fieldHasTag;
2304
+ exports.findChildDefinition = findChildDefinition;
2227
2305
  exports.findCompoundField = findCompoundField;
2228
2306
  exports.findField = findField;
2229
2307
  exports.findScalarField = findScalarField;
@@ -2258,6 +2336,7 @@ exports.makeCompoundField = makeCompoundField;
2258
2336
  exports.makeEvalExpressionHook = makeEvalExpressionHook;
2259
2337
  exports.makeScalarField = makeScalarField;
2260
2338
  exports.matchesType = matchesType;
2339
+ exports.mergeField = mergeField;
2261
2340
  exports.renderControlLayout = renderControlLayout;
2262
2341
  exports.renderLayoutParts = renderLayoutParts;
2263
2342
  exports.stringField = stringField;