@react-typed-forms/schemas 1.0.0-dev.18 → 1.0.0-dev.19

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.
@@ -1,3 +1,3 @@
1
1
  {
2
- "nonCachedDurationMs": 3673.904774999246
2
+ "nonCachedDurationMs": 3702.910748000024
3
3
  }
@@ -100,27 +100,13 @@ export interface ActionRendererProps {
100
100
  visible: Visibility;
101
101
  onClick: () => void;
102
102
  }
103
- export declare function isScalarField(sf: SchemaField): sf is SchemaField;
104
- export declare function isCompoundField(sf: SchemaField): sf is CompoundField;
105
103
  export type AnySchemaFields = SchemaField | (Omit<CompoundField, "children"> & {
106
104
  children: AnySchemaFields[];
107
105
  });
108
- export declare function applyDefaultValues(v: {
109
- [k: string]: any;
110
- } | undefined, fields: SchemaField[]): any;
111
- export declare function applyDefaultForField(v: any, field: SchemaField, parent: SchemaField[], notElement?: boolean): any;
112
- export declare function defaultValueForFields(fields: SchemaField[]): any;
113
- export declare function defaultValueForField(sf: SchemaField): any;
114
- export declare function elementValueForField(sf: SchemaField): any;
115
- export declare function findScalarField(fields: SchemaField[], field: string): SchemaField | undefined;
116
- export declare function findCompoundField(fields: SchemaField[], field: string): CompoundField | undefined;
117
- export declare function findField(fields: SchemaField[], field: string): SchemaField | undefined;
118
106
  export declare function controlTitle(title: string | undefined | null, field: SchemaField): string;
119
107
  export declare function renderControl<S extends ControlDefinition>(definition: S, formState: FormEditState, hooks: FormEditHooks, key: Key): ReactElement;
120
108
  export declare function controlForField(field: string, formState: FormEditState): Control<any>;
121
109
  export declare function fieldForControl(c: ControlDefinition): string | null | undefined;
122
- export declare function isDataControl(c: ControlDefinition): c is DataControlDefinition;
123
- export declare function isGroupControl(c: ControlDefinition): c is GroupedControlsDefinition;
124
110
  export declare const AlwaysVisible: Visibility;
125
111
  export declare function createAction(label: string, onClick: () => void, actionId?: string): ActionRendererProps;
126
112
  export declare function visitControlData<S extends ControlDefinition, A>(definition: S, { fields, data }: FormEditState, cb: (definition: DataControlDefinition, control: Control<any>) => A | undefined): A | undefined;
package/lib/hooks.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ControlDefinition, DataControlDefinition, FieldOption, SchemaField } from "./types";
1
+ import { ControlDefinition, DataControlDefinition, FieldOption, GroupedControlsDefinition, SchemaField } from "./types";
2
2
  import { DataRendererProps, FormEditHooks, FormEditState, SchemaHooks, Visibility } from "./controlRender";
3
3
  import { Control } from "@react-typed-forms/core";
4
4
  export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, hooks: SchemaHooks): any;
@@ -8,3 +8,4 @@ export declare function getOptionsForScalarField(field: SchemaField): FieldOptio
8
8
  export declare function createDefaultSchemaHooks(): SchemaHooks;
9
9
  export declare const defaultFormEditHooks: FormEditHooks;
10
10
  export declare function createFormEditHooks(schemaHooks: SchemaHooks): FormEditHooks;
11
+ export declare function useControlsWithDefaults(definition: GroupedControlsDefinition, sf: SchemaField[]): GroupedControlsDefinition;
package/lib/index.js CHANGED
@@ -276,54 +276,6 @@ function defaultCompoundField(field, displayName, collection) {
276
276
  };
277
277
  }
278
278
 
279
- function fieldHasTag(field, tag) {
280
- var _field$tags;
281
- return Boolean((_field$tags = field.tags) == null ? void 0 : _field$tags.includes(tag));
282
- }
283
- function fieldDisplayName(field) {
284
- var _field$displayName;
285
- return (_field$displayName = field.displayName) != null ? _field$displayName : field.field;
286
- }
287
- function hasOptions(o) {
288
- var _o$options$length, _o$options;
289
- return ((_o$options$length = (_o$options = o.options) == null ? void 0 : _o$options.length) != null ? _o$options$length : 0) > 0;
290
- }
291
-
292
- var _FormRendererComponentsContext = null;
293
- function FormRendererComponentsContext() {
294
- if (!_FormRendererComponentsContext) {
295
- _FormRendererComponentsContext = React.createContext(undefined);
296
- }
297
- return _FormRendererComponentsContext;
298
- }
299
- function FormRendererProvider(_ref) {
300
- var _effect = core.useComponentTracking();
301
- try {
302
- var value = _ref.value,
303
- children = _ref.children;
304
- var _FormRendererComponen = FormRendererComponentsContext(),
305
- Provider = _FormRendererComponen.Provider;
306
- return /*#__PURE__*/React__default["default"].createElement(Provider, {
307
- value: value,
308
- children: children
309
- });
310
- } finally {
311
- _effect();
312
- }
313
- }
314
- function useFormRendererComponents() {
315
- var c = React.useContext(FormRendererComponentsContext());
316
- if (!c) {
317
- throw "Need to use FormRendererComponentContext.Provider";
318
- }
319
- return c;
320
- }
321
- function isScalarField(sf) {
322
- return !isCompoundField(sf);
323
- }
324
- function isCompoundField(sf) {
325
- return sf.type === exports.FieldType.Compound;
326
- }
327
279
  function applyDefaultValues(v, fields) {
328
280
  if (!v) return defaultValueForFields(fields);
329
281
  var applyValue = fields.filter(function (x) {
@@ -377,6 +329,129 @@ function findField(fields, field) {
377
329
  return x.field === field;
378
330
  });
379
331
  }
332
+ function isScalarField(sf) {
333
+ return !isCompoundField(sf);
334
+ }
335
+ function isCompoundField(sf) {
336
+ return sf.type === exports.FieldType.Compound;
337
+ }
338
+ function isDataControl(c) {
339
+ return c.type === exports.ControlDefinitionType.Data;
340
+ }
341
+ function isGroupControl(c) {
342
+ return c.type === exports.ControlDefinitionType.Group;
343
+ }
344
+ function fieldHasTag(field, tag) {
345
+ var _field$tags;
346
+ return Boolean((_field$tags = field.tags) == null ? void 0 : _field$tags.includes(tag));
347
+ }
348
+ function fieldDisplayName(field) {
349
+ var _field$displayName;
350
+ return (_field$displayName = field.displayName) != null ? _field$displayName : field.field;
351
+ }
352
+ function hasOptions(o) {
353
+ var _o$options$length, _o$options;
354
+ return ((_o$options$length = (_o$options = o.options) == null ? void 0 : _o$options.length) != null ? _o$options$length : 0) > 0;
355
+ }
356
+ function defaultControlForField(sf) {
357
+ if (isCompoundField(sf)) {
358
+ return {
359
+ type: exports.ControlDefinitionType.Group,
360
+ title: sf.displayName,
361
+ compoundField: sf.field,
362
+ groupOptions: {
363
+ type: exports.GroupRenderType.Grid,
364
+ hideTitle: false
365
+ },
366
+ children: sf.children.map(defaultControlForField)
367
+ };
368
+ } else if (isScalarField(sf)) {
369
+ var _sf$tags;
370
+ var htmlEditor = (_sf$tags = sf.tags) == null ? void 0 : _sf$tags.includes("_HtmlEditor");
371
+ return {
372
+ type: exports.ControlDefinitionType.Data,
373
+ title: sf.displayName,
374
+ field: sf.field,
375
+ required: sf.required,
376
+ renderOptions: {
377
+ type: htmlEditor ? exports.DataRenderType.HtmlEditor : exports.DataRenderType.Standard
378
+ }
379
+ };
380
+ }
381
+ throw "Unknown schema field";
382
+ }
383
+ function findReferencedControl(field, control) {
384
+ if (isDataControl(control) && field === control.field) return control;
385
+ if (isGroupControl(control)) {
386
+ if (control.compoundField) return field === control.compoundField ? control : undefined;
387
+ return findReferencedControlInArray(field, control.children);
388
+ }
389
+ return undefined;
390
+ }
391
+ function findReferencedControlInArray(field, controls) {
392
+ for (var _iterator = _createForOfIteratorHelperLoose(controls), _step; !(_step = _iterator()).done;) {
393
+ var c = _step.value;
394
+ var ref = findReferencedControl(field, c);
395
+ if (ref) return ref;
396
+ }
397
+ return undefined;
398
+ }
399
+ function addMissingControls(fields, controls) {
400
+ var changes = fields.flatMap(function (x) {
401
+ if (fieldHasTag(x, "_NoControl")) return [];
402
+ var existing = findReferencedControlInArray(x.field, controls);
403
+ if (!existing || isCompoundField(x)) return {
404
+ field: x,
405
+ existing: existing
406
+ };
407
+ return [];
408
+ });
409
+ var changedCompounds = controls.map(function (x) {
410
+ var ex = changes.find(function (c) {
411
+ return c.existing === x;
412
+ });
413
+ if (!ex) return x;
414
+ var cf = x;
415
+ return _extends({}, cf, {
416
+ children: addMissingControls(ex.field.children, cf.children)
417
+ });
418
+ });
419
+ return changedCompounds.concat(changes.filter(function (x) {
420
+ return !x.existing;
421
+ }).map(function (x) {
422
+ return defaultControlForField(x.field);
423
+ }));
424
+ }
425
+
426
+ var _FormRendererComponentsContext = null;
427
+ function FormRendererComponentsContext() {
428
+ if (!_FormRendererComponentsContext) {
429
+ _FormRendererComponentsContext = React.createContext(undefined);
430
+ }
431
+ return _FormRendererComponentsContext;
432
+ }
433
+ function FormRendererProvider(_ref) {
434
+ var _effect = core.useComponentTracking();
435
+ try {
436
+ var value = _ref.value,
437
+ children = _ref.children;
438
+ var _FormRendererComponen = FormRendererComponentsContext(),
439
+ Provider = _FormRendererComponen.Provider;
440
+ return /*#__PURE__*/React__default["default"].createElement(Provider, {
441
+ value: value,
442
+ children: children
443
+ });
444
+ } finally {
445
+ _effect();
446
+ }
447
+ }
448
+ function useFormRendererComponents() {
449
+ var c = React.useContext(FormRendererComponentsContext());
450
+ if (!c) {
451
+ throw "Need to use FormRendererComponentContext.Provider";
452
+ }
453
+ return c;
454
+ }
380
455
  function controlTitle(title, field) {
381
456
  return title ? title : fieldDisplayName(field);
382
457
  }
@@ -492,12 +567,6 @@ function controlForField(field, formState) {
492
567
  function fieldForControl(c) {
493
568
  return isDataControl(c) ? c.field : isGroupControl(c) ? c.compoundField : undefined;
494
569
  }
495
- function isDataControl(c) {
496
- return c.type === exports.ControlDefinitionType.Data;
497
- }
498
- function isGroupControl(c) {
499
- return c.type === exports.ControlDefinitionType.Group;
500
- }
501
570
  var AlwaysVisible = {
502
571
  value: true,
503
572
  canChange: false
@@ -615,8 +684,8 @@ function getDefaultScalarControlProperties(definition, field, visible, defaultVa
615
684
  };
616
685
  }
617
686
  function getOptionsForScalarField(field) {
618
- var _field$options, _field$restrictions, _opts$length;
619
- var opts = (_field$options = field.options) != null ? _field$options : (_field$restrictions = field.restrictions) == null ? void 0 : _field$restrictions.options;
687
+ var _opts$length;
688
+ var opts = field.options;
620
689
  if ((_opts$length = opts == null ? void 0 : opts.length) != null ? _opts$length : 0 > 0) {
621
690
  return opts;
622
691
  }
@@ -835,6 +904,13 @@ function defaultArrayRendererProps(control, field, definition, readonly, renderE
835
904
  }
836
905
  };
837
906
  }
907
+ function useControlsWithDefaults(definition, sf) {
908
+ return React.useMemo(function () {
909
+ return definition.children.length ? definition : _extends({}, definition, {
910
+ children: addMissingControls(sf, [])
911
+ });
912
+ }, [sf, definition]);
913
+ }
838
914
 
839
915
  var _excluded = ["control", "convert"],
840
916
  _excluded2 = ["errorText", "value", "onChange"],
@@ -1462,6 +1538,7 @@ exports.DefaultBoolOptions = DefaultBoolOptions;
1462
1538
  exports.DefaultLabelRenderer = DefaultLabelRenderer;
1463
1539
  exports.FormRendererProvider = FormRendererProvider;
1464
1540
  exports.SelectDataRenderer = SelectDataRenderer;
1541
+ exports.addMissingControls = addMissingControls;
1465
1542
  exports.applyDefaultForField = applyDefaultForField;
1466
1543
  exports.applyDefaultValues = applyDefaultValues;
1467
1544
  exports.boolField = boolField;
@@ -1491,6 +1568,7 @@ exports.createSelectConversion = createSelectConversion;
1491
1568
  exports.createSelectRenderer = createSelectRenderer;
1492
1569
  exports.dataControl = dataControl;
1493
1570
  exports.defaultCompoundField = defaultCompoundField;
1571
+ exports.defaultControlForField = defaultControlForField;
1494
1572
  exports.defaultFormEditHooks = defaultFormEditHooks;
1495
1573
  exports.defaultScalarField = defaultScalarField;
1496
1574
  exports.defaultTailwindTheme = defaultTailwindTheme;
@@ -1522,6 +1600,7 @@ exports.makeScalarField = makeScalarField;
1522
1600
  exports.renderControl = renderControl;
1523
1601
  exports.stringField = stringField;
1524
1602
  exports.stringOptionsField = stringOptionsField;
1603
+ exports.useControlsWithDefaults = useControlsWithDefaults;
1525
1604
  exports.useDefaultValue = useDefaultValue;
1526
1605
  exports.useFormRendererComponents = useFormRendererComponents;
1527
1606
  exports.useIsControlVisible = useIsControlVisible;