@react-typed-forms/schemas 8.2.0 → 9.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/lib/controlBuilder.d.ts +9 -2
- package/lib/controlRender.d.ts +12 -11
- package/lib/hooks.d.ts +8 -8
- package/lib/index.js +323 -215
- package/lib/index.js.map +1 -1
- package/lib/renderers.d.ts +1 -0
- package/lib/schemaBuilder.d.ts +5 -2
- package/lib/types.d.ts +9 -1
- package/lib/util.d.ts +12 -0
- package/package.json +2 -2
- package/.babelrc +0 -4
- package/.rush/temp/operation/build/state.json +0 -3
- package/.rush/temp/operation/update-readme/state.json +0 -3
- package/.rush/temp/package-deps_build.json +0 -13
- package/.rush/temp/shrinkwrap-deps.json +0 -612
- package/src/controlBuilder.ts +0 -175
- package/src/controlRender.tsx +0 -790
- package/src/hooks.tsx +0 -373
- package/src/index.ts +0 -10
- package/src/internal.ts +0 -48
- package/src/renderers.tsx +0 -996
- package/src/schemaBuilder.ts +0 -171
- package/src/schemaInterface.ts +0 -31
- package/src/tailwind.tsx +0 -29
- package/src/types.ts +0 -423
- package/src/util.ts +0 -453
- package/src/validators.ts +0 -116
package/src/hooks.tsx
DELETED
|
@@ -1,373 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ControlDefinition,
|
|
3
|
-
DataExpression,
|
|
4
|
-
DataMatchExpression,
|
|
5
|
-
DynamicPropertyType,
|
|
6
|
-
EntityExpression,
|
|
7
|
-
ExpressionType,
|
|
8
|
-
isDataControlDefinition,
|
|
9
|
-
JsonataExpression,
|
|
10
|
-
SchemaField,
|
|
11
|
-
SchemaInterface,
|
|
12
|
-
} from "./types";
|
|
13
|
-
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
14
|
-
import {
|
|
15
|
-
addAfterChangesCallback,
|
|
16
|
-
collectChanges,
|
|
17
|
-
Control,
|
|
18
|
-
makeChangeTracker,
|
|
19
|
-
useComputed,
|
|
20
|
-
useControl,
|
|
21
|
-
useRefState,
|
|
22
|
-
} from "@react-typed-forms/core";
|
|
23
|
-
|
|
24
|
-
import {
|
|
25
|
-
ControlDataContext,
|
|
26
|
-
defaultValueForField,
|
|
27
|
-
findField,
|
|
28
|
-
getDisplayOnlyOptions,
|
|
29
|
-
getTypeField,
|
|
30
|
-
isControlReadonly,
|
|
31
|
-
jsonPathString,
|
|
32
|
-
lookupChildControl,
|
|
33
|
-
useUpdatedRef,
|
|
34
|
-
} from "./util";
|
|
35
|
-
import jsonata from "jsonata";
|
|
36
|
-
import { trackedStructure, useCalculatedControl } from "./internal";
|
|
37
|
-
import { DataContext } from "./controlRender";
|
|
38
|
-
|
|
39
|
-
export type UseEvalExpressionHook = (
|
|
40
|
-
expr: EntityExpression | undefined,
|
|
41
|
-
) => EvalExpressionHook | undefined;
|
|
42
|
-
|
|
43
|
-
export function useEvalVisibilityHook(
|
|
44
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
45
|
-
definition: ControlDefinition,
|
|
46
|
-
schemaField?: SchemaField,
|
|
47
|
-
): EvalExpressionHook<boolean> {
|
|
48
|
-
const dynamicVisibility = useEvalDynamicHook(
|
|
49
|
-
definition,
|
|
50
|
-
DynamicPropertyType.Visible,
|
|
51
|
-
useEvalExpressionHook,
|
|
52
|
-
);
|
|
53
|
-
const r = useUpdatedRef({ schemaField, definition });
|
|
54
|
-
return useCallback(
|
|
55
|
-
(ctx) => {
|
|
56
|
-
const { schemaField, definition } = r.current;
|
|
57
|
-
return (
|
|
58
|
-
dynamicVisibility?.(ctx) ??
|
|
59
|
-
useComputed(
|
|
60
|
-
() =>
|
|
61
|
-
matchesType(ctx, schemaField?.onlyForTypes) &&
|
|
62
|
-
(!schemaField ||
|
|
63
|
-
!hideDisplayOnly(
|
|
64
|
-
ctx,
|
|
65
|
-
schemaField,
|
|
66
|
-
definition,
|
|
67
|
-
ctx.schemaInterface,
|
|
68
|
-
)),
|
|
69
|
-
)
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
[dynamicVisibility, r],
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function useEvalReadonlyHook(
|
|
77
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
78
|
-
definition: ControlDefinition,
|
|
79
|
-
): EvalExpressionHook<boolean> {
|
|
80
|
-
const dynamicReadonly = useEvalDynamicHook(
|
|
81
|
-
definition,
|
|
82
|
-
DynamicPropertyType.Readonly,
|
|
83
|
-
useEvalExpressionHook,
|
|
84
|
-
);
|
|
85
|
-
const r = useUpdatedRef(definition);
|
|
86
|
-
return useCallback(
|
|
87
|
-
(ctx) => {
|
|
88
|
-
if (dynamicReadonly) return dynamicReadonly(ctx);
|
|
89
|
-
return useCalculatedControl(() => isControlReadonly(r.current));
|
|
90
|
-
},
|
|
91
|
-
[dynamicReadonly, r],
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function useEvalStyleHook(
|
|
96
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
97
|
-
property: DynamicPropertyType,
|
|
98
|
-
definition: ControlDefinition,
|
|
99
|
-
): EvalExpressionHook<React.CSSProperties> {
|
|
100
|
-
const dynamicStyle = useEvalDynamicHook(
|
|
101
|
-
definition,
|
|
102
|
-
property,
|
|
103
|
-
useEvalExpressionHook,
|
|
104
|
-
);
|
|
105
|
-
return useCallback(
|
|
106
|
-
(ctx) => {
|
|
107
|
-
if (dynamicStyle) return dynamicStyle(ctx);
|
|
108
|
-
return useControl(undefined);
|
|
109
|
-
},
|
|
110
|
-
[dynamicStyle],
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function useEvalAllowedOptionsHook(
|
|
115
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
116
|
-
definition: ControlDefinition,
|
|
117
|
-
): EvalExpressionHook<any[]> {
|
|
118
|
-
const dynamicAllowed = useEvalDynamicHook(
|
|
119
|
-
definition,
|
|
120
|
-
DynamicPropertyType.AllowedOptions,
|
|
121
|
-
useEvalExpressionHook,
|
|
122
|
-
);
|
|
123
|
-
return useCallback(
|
|
124
|
-
(ctx) => {
|
|
125
|
-
if (dynamicAllowed) return dynamicAllowed(ctx);
|
|
126
|
-
return useControl([]);
|
|
127
|
-
},
|
|
128
|
-
[dynamicAllowed],
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function useEvalDisabledHook(
|
|
133
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
134
|
-
definition: ControlDefinition,
|
|
135
|
-
): EvalExpressionHook<boolean> {
|
|
136
|
-
const dynamicDisabled = useEvalDynamicHook(
|
|
137
|
-
definition,
|
|
138
|
-
DynamicPropertyType.Disabled,
|
|
139
|
-
useEvalExpressionHook,
|
|
140
|
-
);
|
|
141
|
-
return useCallback(
|
|
142
|
-
(ctx) => {
|
|
143
|
-
if (dynamicDisabled) return dynamicDisabled(ctx);
|
|
144
|
-
return useControl(false);
|
|
145
|
-
},
|
|
146
|
-
[dynamicDisabled],
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function useEvalDisplayHook(
|
|
151
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
152
|
-
definition: ControlDefinition,
|
|
153
|
-
): (
|
|
154
|
-
groupContext: ControlDataContext,
|
|
155
|
-
) => Control<string | undefined> | undefined {
|
|
156
|
-
const dynamicDisplay = useEvalDynamicHook(
|
|
157
|
-
definition,
|
|
158
|
-
DynamicPropertyType.Display,
|
|
159
|
-
useEvalExpressionHook,
|
|
160
|
-
);
|
|
161
|
-
return useCallback((ctx) => dynamicDisplay?.(ctx), [dynamicDisplay]);
|
|
162
|
-
}
|
|
163
|
-
export function useEvalDefaultValueHook(
|
|
164
|
-
useEvalExpressionHook: UseEvalExpressionHook,
|
|
165
|
-
definition: ControlDefinition,
|
|
166
|
-
schemaField?: SchemaField,
|
|
167
|
-
): EvalExpressionHook {
|
|
168
|
-
const dynamicValue = useEvalDynamicHook(
|
|
169
|
-
definition,
|
|
170
|
-
DynamicPropertyType.DefaultValue,
|
|
171
|
-
useEvalExpressionHook,
|
|
172
|
-
);
|
|
173
|
-
const r = useUpdatedRef({ definition, schemaField });
|
|
174
|
-
return useCallback(
|
|
175
|
-
(ctx) => {
|
|
176
|
-
const { definition, schemaField } = r.current;
|
|
177
|
-
return dynamicValue?.(ctx) ?? useComputed(calcDefault);
|
|
178
|
-
function calcDefault() {
|
|
179
|
-
const [required, dcv] = isDataControlDefinition(definition)
|
|
180
|
-
? [definition.required, definition.defaultValue]
|
|
181
|
-
: [false, undefined];
|
|
182
|
-
return (
|
|
183
|
-
dcv ??
|
|
184
|
-
(schemaField
|
|
185
|
-
? defaultValueForField(schemaField, required)
|
|
186
|
-
: undefined)
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
[dynamicValue, r],
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export type EvalExpressionHook<A = any> = (
|
|
195
|
-
groupContext: ControlDataContext,
|
|
196
|
-
) => Control<A | undefined>;
|
|
197
|
-
|
|
198
|
-
function useDataExpression(
|
|
199
|
-
fvExpr: DataExpression,
|
|
200
|
-
fields: SchemaField[],
|
|
201
|
-
data: DataContext,
|
|
202
|
-
) {
|
|
203
|
-
const refField = findField(fields, fvExpr.field);
|
|
204
|
-
const otherField = refField
|
|
205
|
-
? lookupChildControl(data, refField.field)
|
|
206
|
-
: undefined;
|
|
207
|
-
return useCalculatedControl(() => otherField?.value);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function useDataMatchExpression(
|
|
211
|
-
fvExpr: DataMatchExpression,
|
|
212
|
-
fields: SchemaField[],
|
|
213
|
-
data: DataContext,
|
|
214
|
-
) {
|
|
215
|
-
const refField = findField(fields, fvExpr.field);
|
|
216
|
-
const otherField = refField
|
|
217
|
-
? lookupChildControl(data, refField.field)
|
|
218
|
-
: undefined;
|
|
219
|
-
return useComputed(() => {
|
|
220
|
-
const fv = otherField?.value;
|
|
221
|
-
return Array.isArray(fv) ? fv.includes(fvExpr.value) : fv === fvExpr.value;
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export function defaultEvalHooks(
|
|
226
|
-
expr: EntityExpression,
|
|
227
|
-
context: ControlDataContext,
|
|
228
|
-
) {
|
|
229
|
-
switch (expr.type) {
|
|
230
|
-
case ExpressionType.Jsonata:
|
|
231
|
-
return useJsonataExpression(
|
|
232
|
-
(expr as JsonataExpression).expression,
|
|
233
|
-
context,
|
|
234
|
-
);
|
|
235
|
-
case ExpressionType.Data:
|
|
236
|
-
return useDataExpression(expr as DataExpression, context.fields, context);
|
|
237
|
-
case ExpressionType.DataMatch:
|
|
238
|
-
return useDataMatchExpression(
|
|
239
|
-
expr as DataMatchExpression,
|
|
240
|
-
context.fields,
|
|
241
|
-
context,
|
|
242
|
-
);
|
|
243
|
-
default:
|
|
244
|
-
return useControl(undefined);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export const defaultUseEvalExpressionHook =
|
|
249
|
-
makeEvalExpressionHook(defaultEvalHooks);
|
|
250
|
-
|
|
251
|
-
export function makeEvalExpressionHook(
|
|
252
|
-
f: (expr: EntityExpression, context: ControlDataContext) => Control<any>,
|
|
253
|
-
): (expr: EntityExpression | undefined) => EvalExpressionHook | undefined {
|
|
254
|
-
return (expr) => {
|
|
255
|
-
const r = useUpdatedRef(expr);
|
|
256
|
-
const cb = useCallback(
|
|
257
|
-
(ctx: ControlDataContext) => {
|
|
258
|
-
const expr = r.current!;
|
|
259
|
-
return f(expr, ctx);
|
|
260
|
-
},
|
|
261
|
-
[expr?.type, r],
|
|
262
|
-
);
|
|
263
|
-
return expr ? cb : undefined;
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function useEvalDynamicHook(
|
|
268
|
-
definition: ControlDefinition,
|
|
269
|
-
type: DynamicPropertyType,
|
|
270
|
-
useEvalExpressionHook: (
|
|
271
|
-
expr: EntityExpression | undefined,
|
|
272
|
-
) => EvalExpressionHook | undefined,
|
|
273
|
-
): EvalExpressionHook | undefined {
|
|
274
|
-
const expression = definition.dynamic?.find((x) => x.type === type);
|
|
275
|
-
return useEvalExpressionHook(expression?.expr);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
export function matchesType(
|
|
279
|
-
context: ControlDataContext,
|
|
280
|
-
types?: string[] | null,
|
|
281
|
-
) {
|
|
282
|
-
if (types == null || types.length === 0) return true;
|
|
283
|
-
const typeField = getTypeField(context);
|
|
284
|
-
return typeField && types.includes(typeField.value);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export function hideDisplayOnly(
|
|
288
|
-
context: ControlDataContext,
|
|
289
|
-
field: SchemaField,
|
|
290
|
-
definition: ControlDefinition,
|
|
291
|
-
schemaInterface: SchemaInterface,
|
|
292
|
-
) {
|
|
293
|
-
const displayOptions = getDisplayOnlyOptions(definition);
|
|
294
|
-
return (
|
|
295
|
-
displayOptions &&
|
|
296
|
-
!displayOptions.emptyText &&
|
|
297
|
-
schemaInterface.isEmptyValue(
|
|
298
|
-
field,
|
|
299
|
-
lookupChildControl(context, field.field)?.value,
|
|
300
|
-
)
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
export function useJsonataExpression(
|
|
305
|
-
jExpr: string,
|
|
306
|
-
dataContext: DataContext,
|
|
307
|
-
bindings?: () => Record<string, any>,
|
|
308
|
-
): Control<any> {
|
|
309
|
-
const pathString = jsonPathString(dataContext.path, (x) => `#$i[${x}]`);
|
|
310
|
-
const fullExpr = pathString ? pathString + ".(" + jExpr + ")" : jExpr;
|
|
311
|
-
const compiledExpr = useMemo(() => {
|
|
312
|
-
try {
|
|
313
|
-
return jsonata(fullExpr);
|
|
314
|
-
} catch (e) {
|
|
315
|
-
console.error(e);
|
|
316
|
-
return jsonata("null");
|
|
317
|
-
}
|
|
318
|
-
}, [fullExpr]);
|
|
319
|
-
const control = useControl();
|
|
320
|
-
const listenerRef = useRef<() => void>();
|
|
321
|
-
const [ref] = useRefState(() =>
|
|
322
|
-
makeChangeTracker(() => {
|
|
323
|
-
const l = listenerRef.current;
|
|
324
|
-
if (l) {
|
|
325
|
-
listenerRef.current = undefined;
|
|
326
|
-
addAfterChangesCallback(() => {
|
|
327
|
-
l();
|
|
328
|
-
listenerRef.current = l;
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
}),
|
|
332
|
-
);
|
|
333
|
-
useEffect(() => {
|
|
334
|
-
listenerRef.current = apply;
|
|
335
|
-
apply();
|
|
336
|
-
async function apply() {
|
|
337
|
-
const [collect, updateSubscriptions] = ref.current;
|
|
338
|
-
try {
|
|
339
|
-
const bindingData = bindings
|
|
340
|
-
? collectChanges(collect, bindings)
|
|
341
|
-
: undefined;
|
|
342
|
-
control.value = await compiledExpr.evaluate(
|
|
343
|
-
trackedStructure(dataContext.data, collect),
|
|
344
|
-
bindingData,
|
|
345
|
-
);
|
|
346
|
-
} finally {
|
|
347
|
-
updateSubscriptions();
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
return () => ref.current[1](true);
|
|
351
|
-
}, [compiledExpr]);
|
|
352
|
-
return control;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
export function useEvalLabelText(
|
|
356
|
-
useExpr: UseEvalExpressionHook,
|
|
357
|
-
definition: ControlDefinition,
|
|
358
|
-
): EvalExpressionHook<string | null> {
|
|
359
|
-
const dynamicValue = useEvalDynamicHook(
|
|
360
|
-
definition,
|
|
361
|
-
DynamicPropertyType.Label,
|
|
362
|
-
useExpr,
|
|
363
|
-
);
|
|
364
|
-
return useCallback(
|
|
365
|
-
(ctx) => {
|
|
366
|
-
if (dynamicValue) {
|
|
367
|
-
return dynamicValue(ctx);
|
|
368
|
-
}
|
|
369
|
-
return useControl(null);
|
|
370
|
-
},
|
|
371
|
-
[dynamicValue],
|
|
372
|
-
);
|
|
373
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from "./types";
|
|
2
|
-
export * from "./schemaBuilder";
|
|
3
|
-
export * from "./controlBuilder";
|
|
4
|
-
export * from "./controlRender";
|
|
5
|
-
export * from "./util";
|
|
6
|
-
export * from "./renderers";
|
|
7
|
-
export * from "./tailwind";
|
|
8
|
-
export * from "./validators";
|
|
9
|
-
export * from "./hooks";
|
|
10
|
-
export * from "./schemaInterface";
|
package/src/internal.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ChangeListenerFunc,
|
|
3
|
-
Control,
|
|
4
|
-
ControlChange,
|
|
5
|
-
useControl,
|
|
6
|
-
useControlEffect,
|
|
7
|
-
} from "@react-typed-forms/core";
|
|
8
|
-
|
|
9
|
-
export function useCalculatedControl<V>(calculate: () => V): Control<V> {
|
|
10
|
-
const c = useControl(calculate);
|
|
11
|
-
useControlEffect(calculate, (v) => (c.value = v));
|
|
12
|
-
return c;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function cc(n: string | null | undefined): string | undefined {
|
|
16
|
-
return n ? n : undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function trackedStructure<A>(
|
|
20
|
-
c: Control<A>,
|
|
21
|
-
tracker: ChangeListenerFunc<any>,
|
|
22
|
-
): A {
|
|
23
|
-
const cc = c.current;
|
|
24
|
-
const cv = cc.value;
|
|
25
|
-
if (cv == null) {
|
|
26
|
-
tracker(c, ControlChange.Structure);
|
|
27
|
-
return cv;
|
|
28
|
-
}
|
|
29
|
-
if (typeof cv !== "object") {
|
|
30
|
-
tracker(c, ControlChange.Value);
|
|
31
|
-
return cv;
|
|
32
|
-
}
|
|
33
|
-
return new Proxy(cv, {
|
|
34
|
-
get(target: object, p: string | symbol, receiver: any): any {
|
|
35
|
-
if (Array.isArray(cv)) {
|
|
36
|
-
tracker(c, ControlChange.Structure);
|
|
37
|
-
if (typeof p === "symbol" || p[0] >= "9" || p[0] < "0")
|
|
38
|
-
return Reflect.get(cv, p);
|
|
39
|
-
const nc = (cc.elements as any)[p];
|
|
40
|
-
if (typeof nc === "function") return nc;
|
|
41
|
-
if (nc == null) return null;
|
|
42
|
-
return trackedStructure(nc, tracker);
|
|
43
|
-
}
|
|
44
|
-
if (p in cv) return trackedStructure((cc.fields as any)[p], tracker);
|
|
45
|
-
return undefined;
|
|
46
|
-
},
|
|
47
|
-
}) as A;
|
|
48
|
-
}
|