@react-typed-forms/schemas 1.0.0-dev.2 → 1.0.0-dev.3

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,9 +1,9 @@
1
1
  {
2
2
  "files": {
3
3
  "packages/schemas/.rush/temp/shrinkwrap-deps.json": "5ce6417fae765db5c61e400f851ecafe0ce756f0",
4
- "packages/schemas/package.json": "31ae328607ccdfaa29ac8502c8e8e3495ad9c424",
5
- "packages/schemas/src/controlRender.tsx": "a9d28c66e85ff45e941ecd11e8c8032c6629b4b1",
6
- "packages/schemas/src/hooks.ts": "2356b9d918b879adb3490ad5c2b1155dec59a9e5",
4
+ "packages/schemas/package.json": "38baed2ed08306a8db8dbeb4649adb4f294fe615",
5
+ "packages/schemas/src/controlRender.tsx": "340cfc1f2fedb77b3e456c2cf8bf50f40b808c36",
6
+ "packages/schemas/src/hooks.ts": "982c0ee0c7749fc3c58f01ac8c44b8174f898861",
7
7
  "packages/schemas/src/index.ts": "b15fbb6de132cd912cf8141d5c4d2329e947546e",
8
8
  "packages/schemas/src/schemaBuilder.ts": "bfe74d3fb1cc2a4d5226051a58e025e107c32b11",
9
9
  "packages/schemas/src/types.ts": "2ff2a346d753da0eca2fe4acbe1b709f72034e5c",
@@ -8,12 +8,13 @@ export interface FormEditHooks {
8
8
  useActionProperties(formState: FormEditState, definition: ActionControlDefinition): ActionControlProperties;
9
9
  }
10
10
  export interface DataControlProperties {
11
- readonly: boolean;
11
+ control: Control<any>;
12
12
  visible: boolean;
13
- options: FieldOption[] | undefined;
13
+ readonly: boolean;
14
14
  defaultValue: any;
15
15
  required: boolean;
16
- customRender?: (props: DataRendererProps, control: Control<any>) => ReactElement;
16
+ options: FieldOption[] | undefined;
17
+ customRender?: (props: DataRendererProps) => ReactElement;
17
18
  }
18
19
  export interface GroupControlProperties {
19
20
  visible: boolean;
@@ -146,40 +146,25 @@ function renderControl(definition, formState, hooks, key, wrapChild) {
146
146
  }
147
147
  exports.renderControl = renderControl;
148
148
  function DataRenderer({ hooks, formState, controlDef, wrapElem, fieldData, }) {
149
- var _a, _b;
149
+ var _a;
150
150
  const renderer = useFormRendererComponents();
151
151
  const props = hooks.useDataProperties(formState, controlDef, fieldData);
152
- const scalarControl = (_a = formState.data.fields[fieldData.field]) !== null && _a !== void 0 ? _a : (0, core_1.newControl)(undefined);
153
- (0, core_1.useControlEffect)(() => scalarControl.value, (v) => {
154
- if (props.defaultValue && !v) {
155
- scalarControl.value = props.defaultValue;
156
- }
157
- }, true);
158
- if (!props.visible) {
159
- return react_1.default.createElement(react_1.default.Fragment, null);
160
- }
161
152
  const scalarProps = {
162
153
  formEditState: formState,
163
154
  field: fieldData,
164
155
  definition: controlDef,
165
156
  properties: props,
166
157
  };
167
- return wrapElem(((_b = props.customRender) !== null && _b !== void 0 ? _b : renderer.renderData)(scalarProps, scalarControl, false, renderer));
158
+ return wrapElem(((_a = props.customRender) !== null && _a !== void 0 ? _a : renderer.renderData)(scalarProps, props.control, false, renderer));
168
159
  }
169
160
  function ActionRenderer({ hooks, formState, wrapElem, actionDef, }) {
170
161
  const { renderAction } = useFormRendererComponents();
171
162
  const actionControlProperties = hooks.useActionProperties(formState, actionDef);
172
- if (!actionControlProperties.visible) {
173
- return react_1.default.createElement(react_1.default.Fragment, null);
174
- }
175
163
  return wrapElem(renderAction({ definition: actionDef, properties: actionControlProperties }));
176
164
  }
177
165
  function GroupRenderer({ hooks, formState, groupDef, wrapElem, }) {
178
166
  const renderers = useFormRendererComponents();
179
167
  const groupProps = hooks.useGroupProperties(formState, groupDef, hooks);
180
- if (!groupProps.visible) {
181
- return react_1.default.createElement(react_1.default.Fragment, null);
182
- }
183
168
  const compoundField = groupDef.compoundField
184
169
  ? findCompoundField(formState.fields, groupDef.compoundField)
185
170
  : undefined;
@@ -201,9 +186,6 @@ function GroupRenderer({ hooks, formState, groupDef, wrapElem, }) {
201
186
  function DisplayRenderer({ hooks, wrapElem, formState, displayDef, }) {
202
187
  const { renderDisplay } = useFormRendererComponents();
203
188
  const displayProps = hooks.useDisplayProperties(formState, displayDef);
204
- if (!displayProps.visible) {
205
- return react_1.default.createElement(react_1.default.Fragment, null);
206
- }
207
189
  return wrapElem(renderDisplay({ definition: displayDef, properties: displayProps }));
208
190
  }
209
191
  function controlForField(field, formState) {
package/lib/hooks.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { DataControlDefinition, EntityExpression, FieldOption, ControlDefinition, SchemaField } from "./types";
1
+ import { ControlDefinition, DataControlDefinition, EntityExpression, FieldOption, SchemaField } from "./types";
2
2
  import { DataControlProperties, FormEditHooks, FormEditState } from "./controlRender";
3
+ import { Control } from "@react-typed-forms/core";
3
4
  export type ExpressionHook = (expr: EntityExpression, formState: FormEditState) => any;
4
5
  export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, useExpression: ExpressionHook): any;
5
6
  export declare function useIsControlVisible(definition: ControlDefinition, formState: FormEditState, useExpression: ExpressionHook): boolean;
6
- export declare function getDefaultScalarControlProperties(control: DataControlDefinition, field: SchemaField, visible: boolean, defaultValue: any, readonly?: boolean): DataControlProperties;
7
+ export declare function getDefaultScalarControlProperties(definition: DataControlDefinition, field: SchemaField, visible: boolean, defaultValue: any, control: Control<any>, readonly?: boolean): DataControlProperties;
7
8
  export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined;
8
9
  export declare const defaultExpressionHook: ExpressionHook;
9
10
  export declare function createFormEditHooks(useExpression: ExpressionHook): FormEditHooks;
package/lib/hooks.js CHANGED
@@ -39,14 +39,15 @@ function useIsControlVisible(definition, formState, useExpression) {
39
39
  Boolean(typeControl.value && onlyForTypes.includes(typeControl.value))));
40
40
  }
41
41
  exports.useIsControlVisible = useIsControlVisible;
42
- function getDefaultScalarControlProperties(control, field, visible, defaultValue, readonly) {
42
+ function getDefaultScalarControlProperties(definition, field, visible, defaultValue, control, readonly) {
43
43
  var _a, _b;
44
44
  return {
45
45
  defaultValue,
46
46
  options: getOptionsForScalarField(field),
47
- required: (_a = control.required) !== null && _a !== void 0 ? _a : false,
47
+ required: (_a = definition.required) !== null && _a !== void 0 ? _a : false,
48
48
  visible,
49
- readonly: (_b = readonly !== null && readonly !== void 0 ? readonly : control.readonly) !== null && _b !== void 0 ? _b : false,
49
+ readonly: (_b = readonly !== null && readonly !== void 0 ? readonly : definition.readonly) !== null && _b !== void 0 ? _b : false,
50
+ control,
50
51
  };
51
52
  }
52
53
  exports.getDefaultScalarControlProperties = getDefaultScalarControlProperties;
@@ -74,7 +75,15 @@ function createFormEditHooks(useExpression) {
74
75
  useDataProperties(formState, definition, field) {
75
76
  const visible = useIsControlVisible(definition, formState, useExpression);
76
77
  const defaultValue = useDefaultValue(definition, field, formState, useExpression);
77
- return getDefaultScalarControlProperties(definition, field, visible, defaultValue, formState.readonly);
78
+ const scalarControl = formState.data.fields[field.field];
79
+ (0, react_1.useEffect)(() => {
80
+ if (!visible)
81
+ scalarControl.value = null;
82
+ else if (scalarControl.current.value == null) {
83
+ scalarControl.value = defaultValue;
84
+ }
85
+ }, [visible, defaultValue]);
86
+ return getDefaultScalarControlProperties(definition, field, visible, defaultValue, scalarControl, formState.readonly);
78
87
  },
79
88
  useDisplayProperties: (fs, definition) => {
80
89
  const visible = useIsControlVisible(definition, fs, useExpression);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-typed-forms/schemas",
3
- "version": "1.0.0-dev.2",
3
+ "version": "1.0.0-dev.3",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -36,15 +36,13 @@ export interface FormEditHooks {
36
36
  }
37
37
 
38
38
  export interface DataControlProperties {
39
- readonly: boolean;
39
+ control: Control<any>;
40
40
  visible: boolean;
41
- options: FieldOption[] | undefined;
41
+ readonly: boolean;
42
42
  defaultValue: any;
43
43
  required: boolean;
44
- customRender?: (
45
- props: DataRendererProps,
46
- control: Control<any>
47
- ) => ReactElement;
44
+ options: FieldOption[] | undefined;
45
+ customRender?: (props: DataRendererProps) => ReactElement;
48
46
  }
49
47
 
50
48
  export interface GroupControlProperties {
@@ -321,20 +319,6 @@ function DataRenderer({
321
319
  }) {
322
320
  const renderer = useFormRendererComponents();
323
321
  const props = hooks.useDataProperties(formState, controlDef, fieldData);
324
- const scalarControl =
325
- formState.data.fields[fieldData.field] ?? newControl(undefined);
326
- useControlEffect(
327
- () => scalarControl.value,
328
- (v) => {
329
- if (props.defaultValue && !v) {
330
- scalarControl.value = props.defaultValue;
331
- }
332
- },
333
- true
334
- );
335
- if (!props.visible) {
336
- return <></>;
337
- }
338
322
  const scalarProps: DataRendererProps = {
339
323
  formEditState: formState,
340
324
  field: fieldData,
@@ -344,7 +328,7 @@ function DataRenderer({
344
328
  return wrapElem(
345
329
  (props.customRender ?? renderer.renderData)(
346
330
  scalarProps,
347
- scalarControl,
331
+ props.control,
348
332
  false,
349
333
  renderer
350
334
  )
@@ -367,10 +351,6 @@ function ActionRenderer({
367
351
  formState,
368
352
  actionDef
369
353
  );
370
- if (!actionControlProperties.visible) {
371
- return <></>;
372
- }
373
-
374
354
  return wrapElem(
375
355
  renderAction({ definition: actionDef, properties: actionControlProperties })
376
356
  );
@@ -390,9 +370,6 @@ function GroupRenderer({
390
370
  const renderers = useFormRendererComponents();
391
371
 
392
372
  const groupProps = hooks.useGroupProperties(formState, groupDef, hooks);
393
- if (!groupProps.visible) {
394
- return <></>;
395
- }
396
373
  const compoundField = groupDef.compoundField
397
374
  ? findCompoundField(formState.fields, groupDef.compoundField)
398
375
  : undefined;
@@ -452,9 +429,6 @@ function DisplayRenderer({
452
429
  const { renderDisplay } = useFormRendererComponents();
453
430
 
454
431
  const displayProps = hooks.useDisplayProperties(formState, displayDef);
455
- if (!displayProps.visible) {
456
- return <></>;
457
- }
458
432
  return wrapElem(
459
433
  renderDisplay({ definition: displayDef, properties: displayProps })
460
434
  );
package/src/hooks.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  ActionControlDefinition,
3
+ ControlDefinition,
3
4
  DataControlDefinition,
4
5
  DynamicPropertyType,
5
6
  EntityExpression,
6
7
  ExpressionType,
7
8
  FieldOption,
8
9
  FieldValueExpression,
9
- ControlDefinition,
10
10
  SchemaField,
11
11
  } from "./types";
12
12
  import {
@@ -20,7 +20,7 @@ import {
20
20
  isGroupControl,
21
21
  isScalarField,
22
22
  } from "./controlRender";
23
- import { useMemo } from "react";
23
+ import { useEffect, useMemo } from "react";
24
24
  import { Control, newControl } from "@react-typed-forms/core";
25
25
 
26
26
  export type ExpressionHook = (
@@ -83,18 +83,20 @@ export function useIsControlVisible(
83
83
  );
84
84
  }
85
85
  export function getDefaultScalarControlProperties(
86
- control: DataControlDefinition,
86
+ definition: DataControlDefinition,
87
87
  field: SchemaField,
88
88
  visible: boolean,
89
89
  defaultValue: any,
90
+ control: Control<any>,
90
91
  readonly?: boolean
91
92
  ): DataControlProperties {
92
93
  return {
93
94
  defaultValue,
94
95
  options: getOptionsForScalarField(field),
95
- required: control.required ?? false,
96
+ required: definition.required ?? false,
96
97
  visible,
97
- readonly: readonly ?? control.readonly ?? false,
98
+ readonly: readonly ?? definition.readonly ?? false,
99
+ control,
98
100
  };
99
101
  }
100
102
 
@@ -137,11 +139,20 @@ export function createFormEditHooks(
137
139
  formState,
138
140
  useExpression
139
141
  );
142
+ const scalarControl = formState.data.fields[field.field];
143
+
144
+ useEffect(() => {
145
+ if (!visible) scalarControl.value = null;
146
+ else if (scalarControl.current.value == null) {
147
+ scalarControl.value = defaultValue;
148
+ }
149
+ }, [visible, defaultValue]);
140
150
  return getDefaultScalarControlProperties(
141
151
  definition,
142
152
  field,
143
153
  visible,
144
154
  defaultValue,
155
+ scalarControl,
145
156
  formState.readonly
146
157
  );
147
158
  },