@react-typed-forms/schemas 1.0.0-dev.24 → 1.0.0-dev.26

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,7 +1,9 @@
1
- import { DataControlDefinition, DisplayControlDefinition, DynamicProperty, EntityExpression, FieldValueExpression, JsonataExpression } from "./types";
1
+ import { ControlDefinition, DataControlDefinition, DisplayControlDefinition, DynamicProperty, EntityExpression, FieldValueExpression, GroupedControlsDefinition, JsonataExpression } from "./types";
2
2
  export declare function dataControl(field: string, title?: string | null, options?: Partial<DataControlDefinition>): DataControlDefinition;
3
3
  export declare function textDisplayControl(text: string, options?: Partial<DisplayControlDefinition>): DisplayControlDefinition;
4
4
  export declare function htmlDisplayControl(html: string, options?: Partial<DisplayControlDefinition>): DisplayControlDefinition;
5
+ export declare function dynamicDefaultValue(expr: EntityExpression): DynamicProperty;
5
6
  export declare function visibility(expr: EntityExpression): DynamicProperty;
6
7
  export declare function fieldEqExpr(field: string, value: any): FieldValueExpression;
7
8
  export declare function jsonataExpr(expression: string): JsonataExpression;
9
+ export declare function compoundControl(compoundField: string, title: string | undefined, children: ControlDefinition[], options?: Partial<GroupedControlsDefinition>): GroupedControlsDefinition;
package/lib/hooks.d.ts CHANGED
@@ -1,7 +1,7 @@
1
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
- export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, hooks: SchemaHooks): any;
4
+ export declare function useDefaultValue(definition: ControlDefinition, field: SchemaField, formState: FormEditState, hooks: SchemaHooks): any;
5
5
  export declare function useIsControlVisible(definition: ControlDefinition, formState: FormEditState, hooks: SchemaHooks): Visibility;
6
6
  export declare function getDefaultScalarControlProperties(definition: DataControlDefinition, field: SchemaField, visible: Visibility, defaultValue: any, control: Control<any>, formState: FormEditState): DataRendererProps;
7
7
  export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined | null;
package/lib/index.js CHANGED
@@ -282,6 +282,12 @@ function htmlDisplayControl(html, options) {
282
282
  }
283
283
  }, options);
284
284
  }
285
+ function dynamicDefaultValue(expr) {
286
+ return {
287
+ type: exports.DynamicPropertyType.DefaultValue,
288
+ expr: expr
289
+ };
290
+ }
285
291
  function visibility(expr) {
286
292
  return {
287
293
  type: exports.DynamicPropertyType.Visible,
@@ -301,6 +307,18 @@ function jsonataExpr(expression) {
301
307
  expression: expression
302
308
  };
303
309
  }
310
+ function compoundControl(compoundField, title, children, options) {
311
+ return _extends({
312
+ type: exports.ControlDefinitionType.Group,
313
+ compoundField: compoundField,
314
+ children: children,
315
+ title: title,
316
+ groupOptions: {
317
+ type: "Standard",
318
+ hideTitle: !title
319
+ }
320
+ }, options);
321
+ }
304
322
 
305
323
  function applyDefaultValues(v, fields) {
306
324
  if (!v) return defaultValueForFields(fields);
@@ -607,17 +625,17 @@ function visitControlData(definition, _ref6, cb) {
607
625
  }
608
626
 
609
627
  function useDefaultValue(definition, field, formState, hooks) {
610
- var _definition$dynamic;
628
+ var _definition$dynamic, _ref;
611
629
  var valueExpression = (_definition$dynamic = definition.dynamic) == null ? void 0 : _definition$dynamic.find(function (x) {
612
630
  return x.type === exports.DynamicPropertyType.DefaultValue;
613
631
  });
614
632
  if (valueExpression) {
615
633
  return hooks.useExpression(valueExpression.expr, formState).value;
616
634
  }
617
- return field.defaultValue;
635
+ return (_ref = isDataControlDefinition(definition) ? definition.defaultValue : undefined) != null ? _ref : field.defaultValue;
618
636
  }
619
637
  function useIsControlVisible(definition, formState, hooks) {
620
- var _definition$dynamic2, _ref2, _onlyForTypes$length;
638
+ var _definition$dynamic2, _schemaField$onlyForT;
621
639
  var visibleExpression = (_definition$dynamic2 = definition.dynamic) == null ? void 0 : _definition$dynamic2.find(function (x) {
622
640
  return x.type === exports.DynamicPropertyType.Visible;
623
641
  });
@@ -629,24 +647,19 @@ function useIsControlVisible(definition, formState, hooks) {
629
647
  };
630
648
  }
631
649
  var schemaFields = formState.fields;
632
- var _useMemo = React.useMemo(function () {
633
- var _ref, _formState$data$field;
634
- var typeField = schemaFields.find(function (x) {
635
- return isScalarField(x) && x.isTypeField;
636
- });
637
- var typeControl = (_ref = typeField && ((_formState$data$field = formState.data.fields) == null ? void 0 : _formState$data$field[typeField.field])) != null ? _ref : core.newControl(undefined);
638
- var compoundField = isGroupControl(definition) && definition.compoundField ? formState.data.fields[definition.compoundField] : undefined;
639
- return {
640
- typeControl: typeControl,
641
- compoundField: compoundField
642
- };
643
- }, [schemaFields, formState.data]),
644
- typeControl = _useMemo.typeControl,
645
- compoundField = _useMemo.compoundField;
650
+ var typeControl = React.useMemo(function () {
651
+ var _ref2, _formState$data$field;
652
+ var typeField = schemaFields.find(function (x) {
653
+ return isScalarField(x) && x.isTypeField;
654
+ });
655
+ return (_ref2 = typeField && ((_formState$data$field = formState.data.fields) == null ? void 0 : _formState$data$field[typeField.field])) != null ? _ref2 : core.newControl(undefined);
656
+ }, [schemaFields, formState.data]);
646
657
  var fieldName = fieldForControl(definition);
647
- var onlyForTypes = (_ref2 = fieldName ? findField(schemaFields, fieldName) : undefined) == null ? void 0 : _ref2.onlyForTypes;
648
- var canChange = Boolean(compoundField || ((_onlyForTypes$length = onlyForTypes == null ? void 0 : onlyForTypes.length) != null ? _onlyForTypes$length : 0) > 0);
649
- var value = (!compoundField || compoundField.value != null) && (!onlyForTypes || onlyForTypes.length === 0 || Boolean(typeControl.value && onlyForTypes.includes(typeControl.value)));
658
+ var schemaField = fieldName ? findField(schemaFields, fieldName) : undefined;
659
+ var isSingleCompoundField = schemaField && schemaField.type === exports.FieldType.Compound && !schemaField.collection;
660
+ var onlyForTypes = (_schemaField$onlyForT = schemaField == null ? void 0 : schemaField.onlyForTypes) != null ? _schemaField$onlyForT : [];
661
+ var canChange = Boolean(isSingleCompoundField || onlyForTypes.length);
662
+ var value = (!isSingleCompoundField || formState.data.fields[fieldName].value != null) && (!onlyForTypes.length || Boolean(typeControl.value && onlyForTypes.includes(typeControl.value)));
650
663
  return {
651
664
  value: value,
652
665
  canChange: canChange
@@ -801,6 +814,15 @@ function createFormEditHooks(schemaHooks) {
801
814
  var _definition$groupOpti;
802
815
  var visible = useIsControlVisible(definition, fs, schemaHooks);
803
816
  var field = definition.compoundField ? findCompoundField(fs.fields, definition.compoundField) : undefined;
817
+ var defaultValue = field && useDefaultValue(definition, field, fs, schemaHooks);
818
+ var dataControl = field && fs.data.fields[field.field];
819
+ var isVisible = visible.value && !fs.invisible;
820
+ React.useEffect(function () {
821
+ if (!dataControl) return;
822
+ if (isVisible === false) dataControl.value = null;else if (dataControl.current.value == null) {
823
+ dataControl.value = defaultValue;
824
+ }
825
+ }, [dataControl, isVisible, defaultValue]);
804
826
  var newFs = _extends({}, fs, {
805
827
  fields: field ? field.children : fs.fields,
806
828
  invisible: visible.value === false || fs.invisible
@@ -1547,6 +1569,7 @@ exports.applyDefaultForField = applyDefaultForField;
1547
1569
  exports.applyDefaultValues = applyDefaultValues;
1548
1570
  exports.boolField = boolField;
1549
1571
  exports.buildSchema = buildSchema;
1572
+ exports.compoundControl = compoundControl;
1550
1573
  exports.compoundField = compoundField;
1551
1574
  exports.controlForField = controlForField;
1552
1575
  exports.controlTitle = controlTitle;
@@ -1579,6 +1602,7 @@ exports.defaultScalarField = defaultScalarField;
1579
1602
  exports.defaultTailwindTheme = defaultTailwindTheme;
1580
1603
  exports.defaultValueForField = defaultValueForField;
1581
1604
  exports.defaultValueForFields = defaultValueForFields;
1605
+ exports.dynamicDefaultValue = dynamicDefaultValue;
1582
1606
  exports.elementValueForField = elementValueForField;
1583
1607
  exports.emptyGroupDefinition = emptyGroupDefinition;
1584
1608
  exports.fieldDisplayName = fieldDisplayName;