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

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.
package/README.md CHANGED
@@ -95,9 +95,78 @@ A `SchemaField` is a JSON object which describes the definition of a field insid
95
95
 
96
96
  Each `SchemaField` can also be marked as a `collection` which means that it will be mapped to a JSON array of the defined `FieldType`.
97
97
 
98
- TODO - buildSchema, FieldOptions, extending FieldType
98
+ ### Defining fields
99
+
100
+ While you can define a `SchemaField` as plain JSON, e.g.
101
+ ```json
102
+ [
103
+ {
104
+ "type": "String",
105
+ "field": "firstName",
106
+ "displayName": "First Name"
107
+ },
108
+ {
109
+ "type": "String",
110
+ "field": "lastName",
111
+ "displayName": "Last Name",
112
+ "required": true
113
+ },
114
+ {
115
+ "type": "Int",
116
+ "field": "yearOfBirth",
117
+ "displayName": "Year of birth",
118
+ "defaultValue": 1980
119
+ }
120
+ ]
121
+ ```
122
+
123
+ However if you have existing types which you would like to define `SchemaField`s for the library contains a function called `buildSchema` a type safe way of generating fields for a type:
124
+
125
+ ```tsx
126
+ interface SimpleForm {
127
+ firstName: string;
128
+ lastName: string;
129
+ yearOfBirth: number;
130
+ }
131
+
132
+ const simpleSchema = buildSchema<SimpleForm>({
133
+ firstName: stringField("First Name"),
134
+ lastName: stringField("Last Name", { required: true }),
135
+ yearOfBirth: intField("Year of birth", { defaultValue: 1980 }),
136
+ });
137
+ ```
138
+
139
+ ### Field options
140
+
141
+ Often a field only has a set of allowed values, e.g. a enum. `SchemaField` allows this to be modeled by
142
+ providing an array of `FieldOption`:
143
+
144
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/types.ts&lines=37-41) -->
145
+ <!-- The below code snippet is automatically added from ./src/types.ts -->
146
+ ```ts
147
+ export interface FieldOption {
148
+ name: string;
149
+ value: any;
150
+ }
151
+ ```
152
+ <!-- AUTO-GENERATED-CONTENT:END -->
153
+
154
+ For example you could only allow certain last names:
155
+
156
+ ```ts
157
+ stringField('Last Name', {
158
+ required: true,
159
+ options:[
160
+ { name: "Smith", value: "smith" },
161
+ { name: "Jones", value: "jones" }
162
+ ]
163
+ });
164
+ ```
165
+
166
+ <img src="../../images/schemas-option.png">
99
167
 
100
168
  ### `ControlDefinition`
169
+
101
170
  A `ControlDefinition` is a JSON object which describes what should be rendered in a UI. Each `ControlDefinition` can be one of 4 distinct types:
102
171
 
103
172
  * `DataControlDefinition` - Points to a `SchemaField` in order to render a control for editing of data.
@@ -0,0 +1,7 @@
1
+ import { DataControlDefinition, DisplayControlDefinition, DynamicProperty, EntityExpression, FieldValueExpression, JsonataExpression } from "./types";
2
+ export declare function dataControl(field: string, title?: string | null, options?: Partial<DataControlDefinition>): DataControlDefinition;
3
+ export declare function textDisplayControl(text: string, options?: Partial<DisplayControlDefinition>): DisplayControlDefinition;
4
+ export declare function htmlDisplayControl(html: string, options?: Partial<DisplayControlDefinition>): DisplayControlDefinition;
5
+ export declare function visibility(expr: EntityExpression): DynamicProperty;
6
+ export declare function fieldEqExpr(field: string, value: any): FieldValueExpression;
7
+ export declare function jsonataExpr(expression: string): JsonataExpression;
@@ -3,7 +3,7 @@ import { Key, ReactElement, ReactNode } from "react";
3
3
  import { Control } from "@react-typed-forms/core";
4
4
  export interface SchemaHooks {
5
5
  useExpression(expr: EntityExpression, formState: FormEditState): Control<any | undefined>;
6
- useValidators(formState: FormEditState, isVisible: boolean, control: Control<any>, required: boolean, validations?: SchemaValidator[] | null): void;
6
+ useValidators(formState: FormEditState, isVisible: boolean | undefined, control: Control<any>, required: boolean, validations?: SchemaValidator[] | null): void;
7
7
  }
8
8
  export interface FormEditHooks {
9
9
  useDataProperties(formState: FormEditState, definition: DataControlDefinition, field: SchemaField): DataRendererProps;
@@ -79,7 +79,7 @@ export interface FormRenderer {
79
79
  renderAdornment: (props: AdornmentProps) => AdornmentRenderer;
80
80
  }
81
81
  export interface Visibility {
82
- value: boolean;
82
+ value: boolean | undefined;
83
83
  canChange: boolean;
84
84
  }
85
85
  export interface LabelRendererProps {
package/lib/hooks.d.ts CHANGED
@@ -8,4 +8,5 @@ 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 const emptyGroupDefinition: GroupedControlsDefinition;
11
12
  export declare function useControlDefinitionForSchema(sf: SchemaField[], definition?: GroupedControlsDefinition): GroupedControlsDefinition;
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./types";
2
2
  export * from "./schemaBuilder";
3
+ export * from "./controlBuilder";
3
4
  export * from "./controlRender";
4
5
  export * from "./hooks";
5
6
  export * from "./util";
package/lib/index.js CHANGED
@@ -9,64 +9,6 @@ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
9
9
  var jsonata__default = /*#__PURE__*/_interopDefaultLegacy(jsonata);
10
10
  var clsx__default = /*#__PURE__*/_interopDefaultLegacy(clsx);
11
11
 
12
- function _extends() {
13
- _extends = Object.assign ? Object.assign.bind() : function (target) {
14
- for (var i = 1; i < arguments.length; i++) {
15
- var source = arguments[i];
16
- for (var key in source) {
17
- if (Object.prototype.hasOwnProperty.call(source, key)) {
18
- target[key] = source[key];
19
- }
20
- }
21
- }
22
- return target;
23
- };
24
- return _extends.apply(this, arguments);
25
- }
26
- function _objectWithoutPropertiesLoose(source, excluded) {
27
- if (source == null) return {};
28
- var target = {};
29
- var sourceKeys = Object.keys(source);
30
- var key, i;
31
- for (i = 0; i < sourceKeys.length; i++) {
32
- key = sourceKeys[i];
33
- if (excluded.indexOf(key) >= 0) continue;
34
- target[key] = source[key];
35
- }
36
- return target;
37
- }
38
- function _unsupportedIterableToArray(o, minLen) {
39
- if (!o) return;
40
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
41
- var n = Object.prototype.toString.call(o).slice(8, -1);
42
- if (n === "Object" && o.constructor) n = o.constructor.name;
43
- if (n === "Map" || n === "Set") return Array.from(o);
44
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
45
- }
46
- function _arrayLikeToArray(arr, len) {
47
- if (len == null || len > arr.length) len = arr.length;
48
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
49
- return arr2;
50
- }
51
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
52
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
53
- if (it) return (it = it.call(o)).next.bind(it);
54
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
55
- if (it) o = it;
56
- var i = 0;
57
- return function () {
58
- if (i >= o.length) return {
59
- done: true
60
- };
61
- return {
62
- done: false,
63
- value: o[i++]
64
- };
65
- };
66
- }
67
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
68
- }
69
-
70
12
  exports.FieldType = void 0;
71
13
  (function (FieldType) {
72
14
  FieldType["String"] = "String";
@@ -179,27 +121,66 @@ function visitControlDefinition(x, visitor, defaultValue) {
179
121
  return defaultValue(x);
180
122
  }
181
123
  }
182
- function dataControl(field, options) {
183
- return _extends({
184
- type: exports.ControlDefinitionType.Data,
185
- field: field
186
- }, options);
124
+ function isGridRenderer(options) {
125
+ return options.type === exports.GroupRenderType.Grid;
187
126
  }
188
- function fieldValueExpr(field, value) {
189
- return {
190
- type: exports.ExpressionType.FieldValue,
191
- field: field,
192
- value: value
127
+
128
+ function _extends() {
129
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
130
+ for (var i = 1; i < arguments.length; i++) {
131
+ var source = arguments[i];
132
+ for (var key in source) {
133
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
134
+ target[key] = source[key];
135
+ }
136
+ }
137
+ }
138
+ return target;
193
139
  };
140
+ return _extends.apply(this, arguments);
194
141
  }
195
- function visibility(expr) {
196
- return {
197
- type: exports.DynamicPropertyType.Visible,
198
- expr: expr
199
- };
142
+ function _objectWithoutPropertiesLoose(source, excluded) {
143
+ if (source == null) return {};
144
+ var target = {};
145
+ var sourceKeys = Object.keys(source);
146
+ var key, i;
147
+ for (i = 0; i < sourceKeys.length; i++) {
148
+ key = sourceKeys[i];
149
+ if (excluded.indexOf(key) >= 0) continue;
150
+ target[key] = source[key];
151
+ }
152
+ return target;
200
153
  }
201
- function isGridRenderer(options) {
202
- return options.type === exports.GroupRenderType.Grid;
154
+ function _unsupportedIterableToArray(o, minLen) {
155
+ if (!o) return;
156
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
157
+ var n = Object.prototype.toString.call(o).slice(8, -1);
158
+ if (n === "Object" && o.constructor) n = o.constructor.name;
159
+ if (n === "Map" || n === "Set") return Array.from(o);
160
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
161
+ }
162
+ function _arrayLikeToArray(arr, len) {
163
+ if (len == null || len > arr.length) len = arr.length;
164
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
165
+ return arr2;
166
+ }
167
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
168
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
169
+ if (it) return (it = it.call(o)).next.bind(it);
170
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
171
+ if (it) o = it;
172
+ var i = 0;
173
+ return function () {
174
+ if (i >= o.length) return {
175
+ done: true
176
+ };
177
+ return {
178
+ done: false,
179
+ value: o[i++]
180
+ };
181
+ };
182
+ }
183
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
203
184
  }
204
185
 
205
186
  function buildSchema(def) {
@@ -276,6 +257,51 @@ function defaultCompoundField(field, displayName, collection) {
276
257
  };
277
258
  }
278
259
 
260
+ function dataControl(field, title, options) {
261
+ return _extends({
262
+ type: exports.ControlDefinitionType.Data,
263
+ field: field,
264
+ title: title
265
+ }, options);
266
+ }
267
+ function textDisplayControl(text, options) {
268
+ return _extends({
269
+ type: exports.ControlDefinitionType.Display,
270
+ displayData: {
271
+ type: exports.DisplayDataType.Text,
272
+ text: text
273
+ }
274
+ }, options);
275
+ }
276
+ function htmlDisplayControl(html, options) {
277
+ return _extends({
278
+ type: exports.ControlDefinitionType.Display,
279
+ displayData: {
280
+ type: exports.DisplayDataType.Html,
281
+ html: html
282
+ }
283
+ }, options);
284
+ }
285
+ function visibility(expr) {
286
+ return {
287
+ type: exports.DynamicPropertyType.Visible,
288
+ expr: expr
289
+ };
290
+ }
291
+ function fieldEqExpr(field, value) {
292
+ return {
293
+ type: exports.ExpressionType.FieldValue,
294
+ field: field,
295
+ value: value
296
+ };
297
+ }
298
+ function jsonataExpr(expression) {
299
+ return {
300
+ type: exports.ExpressionType.Jsonata,
301
+ expression: expression
302
+ };
303
+ }
304
+
279
305
  function applyDefaultValues(v, fields) {
280
306
  if (!v) return defaultValueForFields(fields);
281
307
  var applyValue = fields.filter(function (x) {
@@ -598,7 +624,7 @@ function useIsControlVisible(definition, formState, hooks) {
598
624
  if (visibleExpression && visibleExpression.expr) {
599
625
  var exprValue = hooks.useExpression(visibleExpression.expr, formState).value;
600
626
  return {
601
- value: Boolean(exprValue),
627
+ value: exprValue,
602
628
  canChange: true
603
629
  };
604
630
  }
@@ -670,7 +696,7 @@ function createDefaultSchemaHooks() {
670
696
  } catch (e) {
671
697
  return Promise.reject(e);
672
698
  }
673
- });
699
+ }, true);
674
700
  return control;
675
701
  case exports.ExpressionType.FieldValue:
676
702
  var fvExpr = expr;
@@ -684,7 +710,7 @@ function createDefaultSchemaHooks() {
684
710
  }
685
711
  function useValidators(formState, isVisible, control, required, validators) {
686
712
  if (required) core.useValidator(control, function (v) {
687
- return isVisible && (v == null || v == "") ? "Please enter a value" : null;
713
+ return isVisible === true && (v == null || v === "") ? "Please enter a value" : null;
688
714
  }, "required");
689
715
  validators == null || validators.forEach(function (v, i) {
690
716
  switch (v.type) {
@@ -741,7 +767,7 @@ function createFormEditHooks(schemaHooks) {
741
767
  var defaultValue = useDefaultValue(definition, field, formState, schemaHooks);
742
768
  var scalarControl = formState.data.fields[field.field];
743
769
  React.useEffect(function () {
744
- if (!isVisible) scalarControl.value = null;else if (scalarControl.current.value == null) {
770
+ if (isVisible === false) scalarControl.value = null;else if (scalarControl.current.value == null) {
745
771
  scalarControl.value = defaultValue;
746
772
  }
747
773
  }, [isVisible, defaultValue]);
@@ -777,7 +803,7 @@ function createFormEditHooks(schemaHooks) {
777
803
  var field = definition.compoundField ? findCompoundField(fs.fields, definition.compoundField) : undefined;
778
804
  var newFs = _extends({}, fs, {
779
805
  fields: field ? field.children : fs.fields,
780
- invisible: !visible.value || fs.invisible
806
+ invisible: visible.value === false || fs.invisible
781
807
  });
782
808
  var data = field ? fs.data.fields[field.field] : fs.data;
783
809
  var groupProps = {
@@ -875,8 +901,8 @@ function useControlDefinitionForSchema(sf, definition) {
875
901
  definition = emptyGroupDefinition;
876
902
  }
877
903
  return React.useMemo(function () {
878
- return definition.children.length ? definition : _extends({}, definition, {
879
- children: addMissingControls(sf, [])
904
+ return _extends({}, definition, {
905
+ children: addMissingControls(sf, definition.children)
880
906
  });
881
907
  }, [sf, definition]);
882
908
  }
@@ -1554,16 +1580,18 @@ exports.defaultTailwindTheme = defaultTailwindTheme;
1554
1580
  exports.defaultValueForField = defaultValueForField;
1555
1581
  exports.defaultValueForFields = defaultValueForFields;
1556
1582
  exports.elementValueForField = elementValueForField;
1583
+ exports.emptyGroupDefinition = emptyGroupDefinition;
1557
1584
  exports.fieldDisplayName = fieldDisplayName;
1585
+ exports.fieldEqExpr = fieldEqExpr;
1558
1586
  exports.fieldForControl = fieldForControl;
1559
1587
  exports.fieldHasTag = fieldHasTag;
1560
- exports.fieldValueExpr = fieldValueExpr;
1561
1588
  exports.findCompoundField = findCompoundField;
1562
1589
  exports.findField = findField;
1563
1590
  exports.findScalarField = findScalarField;
1564
1591
  exports.getDefaultScalarControlProperties = getDefaultScalarControlProperties;
1565
1592
  exports.getOptionsForScalarField = getOptionsForScalarField;
1566
1593
  exports.hasOptions = hasOptions;
1594
+ exports.htmlDisplayControl = htmlDisplayControl;
1567
1595
  exports.intField = intField;
1568
1596
  exports.isActionControlsDefinition = isActionControlsDefinition;
1569
1597
  exports.isCompoundField = isCompoundField;
@@ -1574,11 +1602,13 @@ exports.isGridRenderer = isGridRenderer;
1574
1602
  exports.isGroupControl = isGroupControl;
1575
1603
  exports.isGroupControlsDefinition = isGroupControlsDefinition;
1576
1604
  exports.isScalarField = isScalarField;
1605
+ exports.jsonataExpr = jsonataExpr;
1577
1606
  exports.makeCompoundField = makeCompoundField;
1578
1607
  exports.makeScalarField = makeScalarField;
1579
1608
  exports.renderControl = renderControl;
1580
1609
  exports.stringField = stringField;
1581
1610
  exports.stringOptionsField = stringOptionsField;
1611
+ exports.textDisplayControl = textDisplayControl;
1582
1612
  exports.useControlDefinitionForSchema = useControlDefinitionForSchema;
1583
1613
  exports.useDefaultValue = useDefaultValue;
1584
1614
  exports.useIsControlVisible = useIsControlVisible;