@react-typed-forms/schemas 3.0.0-dev.98 → 4.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.
- package/.babelrc +4 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/operation/update-readme/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +581 -7
- package/README.md +292 -0
- package/lib/controlBuilder.d.ts +14 -0
- package/lib/controlRender.d.ts +96 -80
- package/lib/hooks.d.ts +9 -9
- package/lib/index.d.ts +5 -1
- package/lib/index.js +1835 -19
- package/lib/index.js.map +1 -0
- package/lib/renderers.d.ts +171 -0
- package/lib/schemaBuilder.d.ts +53 -70
- package/lib/tailwind.d.ts +2 -0
- package/lib/types.d.ts +108 -43
- package/lib/util.d.ts +35 -0
- package/lib/validators.d.ts +4 -0
- package/package.json +15 -8
- package/src/controlBuilder.ts +121 -0
- package/src/controlRender.tsx +533 -437
- package/src/hooks.tsx +153 -0
- package/src/index.ts +5 -1
- package/src/renderers.tsx +846 -0
- package/src/schemaBuilder.ts +45 -66
- package/src/tailwind.tsx +25 -0
- package/src/types.ts +164 -48
- package/src/util.ts +360 -0
- package/src/validators.ts +116 -0
- package/tsconfig.json +4 -3
- package/.rush/temp/package-deps_build.json +0 -13
- package/lib/controlRender.js +0 -230
- package/lib/hooks.js +0 -93
- package/lib/schemaBuilder.js +0 -82
- package/lib/types.js +0 -73
- package/schemas.build.log +0 -2
- package/src/hooks.ts +0 -167
package/src/hooks.tsx
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ControlDefinition,
|
|
3
|
+
DynamicPropertyType,
|
|
4
|
+
EntityExpression,
|
|
5
|
+
ExpressionType,
|
|
6
|
+
FieldValueExpression,
|
|
7
|
+
isDataControlDefinition,
|
|
8
|
+
JsonataExpression,
|
|
9
|
+
SchemaField,
|
|
10
|
+
} from "./types";
|
|
11
|
+
import { useCallback, useMemo } from "react";
|
|
12
|
+
import {
|
|
13
|
+
Control,
|
|
14
|
+
useComputed,
|
|
15
|
+
useControl,
|
|
16
|
+
useControlEffect,
|
|
17
|
+
} from "@react-typed-forms/core";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
ControlGroupContext,
|
|
21
|
+
defaultValueForField,
|
|
22
|
+
findField,
|
|
23
|
+
getTypeField,
|
|
24
|
+
useUpdatedRef,
|
|
25
|
+
} from "./util";
|
|
26
|
+
import jsonata from "jsonata";
|
|
27
|
+
|
|
28
|
+
export function useEvalVisibilityHook(
|
|
29
|
+
definition: ControlDefinition,
|
|
30
|
+
schemaField?: SchemaField,
|
|
31
|
+
): EvalExpressionHook<boolean> {
|
|
32
|
+
const dynamicVisibility = useEvalDynamicHook(
|
|
33
|
+
definition,
|
|
34
|
+
DynamicPropertyType.Visible,
|
|
35
|
+
);
|
|
36
|
+
const r = useUpdatedRef(schemaField);
|
|
37
|
+
return useCallback(
|
|
38
|
+
(ctx) => {
|
|
39
|
+
const schemaField = r.current;
|
|
40
|
+
return (
|
|
41
|
+
dynamicVisibility?.(ctx) ??
|
|
42
|
+
useComputed(() => matchesType(ctx, schemaField?.onlyForTypes))
|
|
43
|
+
);
|
|
44
|
+
},
|
|
45
|
+
[dynamicVisibility, r],
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function useEvalDefaultValueHook(
|
|
50
|
+
definition: ControlDefinition,
|
|
51
|
+
schemaField?: SchemaField,
|
|
52
|
+
): EvalExpressionHook {
|
|
53
|
+
const dynamicValue = useEvalDynamicHook(
|
|
54
|
+
definition,
|
|
55
|
+
DynamicPropertyType.DefaultValue,
|
|
56
|
+
);
|
|
57
|
+
const r = useUpdatedRef({ definition, schemaField });
|
|
58
|
+
return useCallback(
|
|
59
|
+
(ctx) => {
|
|
60
|
+
const { definition, schemaField } = r.current;
|
|
61
|
+
return dynamicValue?.(ctx) ?? useComputed(calcDefault);
|
|
62
|
+
function calcDefault() {
|
|
63
|
+
const [required, dcv] = isDataControlDefinition(definition)
|
|
64
|
+
? [definition.required, definition.defaultValue]
|
|
65
|
+
: [false, undefined];
|
|
66
|
+
return (
|
|
67
|
+
dcv ??
|
|
68
|
+
(schemaField
|
|
69
|
+
? defaultValueForField(schemaField, required)
|
|
70
|
+
: undefined)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
[dynamicValue, r],
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type EvalExpressionHook<A = any> = (
|
|
79
|
+
groupContext: ControlGroupContext,
|
|
80
|
+
) => Control<A | undefined>;
|
|
81
|
+
|
|
82
|
+
function useFieldValueExpression(
|
|
83
|
+
fvExpr: FieldValueExpression,
|
|
84
|
+
fields: SchemaField[],
|
|
85
|
+
data: Control<any>,
|
|
86
|
+
) {
|
|
87
|
+
const refField = findField(fields, fvExpr.field);
|
|
88
|
+
const otherField = refField ? data.fields[refField.field] : undefined;
|
|
89
|
+
return useComputed(() => {
|
|
90
|
+
const fv = otherField?.value;
|
|
91
|
+
return Array.isArray(fv) ? fv.includes(fvExpr.value) : fv === fvExpr.value;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function useEvalExpressionHook(
|
|
95
|
+
expr: EntityExpression | undefined,
|
|
96
|
+
): EvalExpressionHook | undefined {
|
|
97
|
+
const r = useUpdatedRef(expr);
|
|
98
|
+
const cb = useCallback(
|
|
99
|
+
({ groupControl, fields }: ControlGroupContext) => {
|
|
100
|
+
const expr = r.current!;
|
|
101
|
+
switch (expr.type) {
|
|
102
|
+
case ExpressionType.Jsonata:
|
|
103
|
+
return useJsonataExpression(
|
|
104
|
+
(expr as JsonataExpression).expression,
|
|
105
|
+
groupControl,
|
|
106
|
+
);
|
|
107
|
+
case ExpressionType.FieldValue:
|
|
108
|
+
return useFieldValueExpression(
|
|
109
|
+
expr as FieldValueExpression,
|
|
110
|
+
fields,
|
|
111
|
+
groupControl,
|
|
112
|
+
);
|
|
113
|
+
default:
|
|
114
|
+
return useControl(undefined);
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
[expr?.type, r],
|
|
118
|
+
);
|
|
119
|
+
return expr ? cb : undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function useEvalDynamicHook(
|
|
123
|
+
definition: ControlDefinition,
|
|
124
|
+
type: DynamicPropertyType,
|
|
125
|
+
): EvalExpressionHook | undefined {
|
|
126
|
+
const expression = definition.dynamic?.find((x) => x.type === type);
|
|
127
|
+
return useEvalExpressionHook(expression?.expr);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function matchesType(
|
|
131
|
+
context: ControlGroupContext,
|
|
132
|
+
types?: string[] | null,
|
|
133
|
+
) {
|
|
134
|
+
if (types == null || types.length === 0) return true;
|
|
135
|
+
const typeField = getTypeField(context);
|
|
136
|
+
return typeField && types.includes(typeField.value);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function useJsonataExpression(
|
|
140
|
+
jExpr: string,
|
|
141
|
+
data: Control<any>,
|
|
142
|
+
): Control<any> {
|
|
143
|
+
const compiledExpr = useMemo(() => jsonata(jExpr), [jExpr]);
|
|
144
|
+
const control = useControl();
|
|
145
|
+
useControlEffect(
|
|
146
|
+
() => data.value,
|
|
147
|
+
async (v) => {
|
|
148
|
+
control.value = await compiledExpr.evaluate(v);
|
|
149
|
+
},
|
|
150
|
+
true,
|
|
151
|
+
);
|
|
152
|
+
return control;
|
|
153
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export * from "./types";
|
|
2
2
|
export * from "./schemaBuilder";
|
|
3
|
+
export * from "./controlBuilder";
|
|
3
4
|
export * from "./controlRender";
|
|
4
|
-
export * from "./
|
|
5
|
+
export * from "./util";
|
|
6
|
+
export * from "./renderers";
|
|
7
|
+
export * from "./tailwind";
|
|
8
|
+
export * from "./validators";
|