@react-typed-forms/schemas 7.2.0 → 7.3.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/src/util.ts CHANGED
@@ -19,10 +19,9 @@ import {
19
19
  import { MutableRefObject, useRef } from "react";
20
20
  import { Control } from "@react-typed-forms/core";
21
21
  import clsx from "clsx";
22
+ import { DataContext, JsonPath } from "./controlRender";
22
23
 
23
- export interface ControlDataContext {
24
- groupControl: Control<any>;
25
- root: Control<any>;
24
+ export interface ControlDataContext extends DataContext {
26
25
  fields: SchemaField[];
27
26
  schemaInterface: SchemaInterface;
28
27
  }
@@ -252,7 +251,7 @@ export function getTypeField(
252
251
  ): Control<string> | undefined {
253
252
  const typeSchemaField = context.fields.find((x) => x.isTypeField);
254
253
  return typeSchemaField
255
- ? context.groupControl.fields?.[typeSchemaField.field]
254
+ ? lookupChildControl(context, typeSchemaField.field)
256
255
  : undefined;
257
256
  }
258
257
 
@@ -308,11 +307,14 @@ export function visitControlData<A>(
308
307
  if (!fieldData)
309
308
  return !fieldName ? visitControlDataArray(children, ctx, cb) : undefined;
310
309
 
311
- const control = ctx.groupControl.fields[fieldData.field];
310
+ const thisPath = [...ctx.path, fieldData.field];
311
+ const control = ctx.data.lookupControl(thisPath);
312
+ if (!control) throw "No control for field";
312
313
  const result = def ? cb(def, fieldData, control, false) : undefined;
313
314
  if (result !== undefined) return result;
314
315
  if (fieldData.collection) {
315
- for (const c of control.elements ?? []) {
316
+ let cIndex = 0;
317
+ for (const c of control!.elements ?? []) {
316
318
  const elemResult = def ? cb(def, fieldData, c, true) : undefined;
317
319
  if (elemResult !== undefined) return elemResult;
318
320
  if (isCompoundField(fieldData)) {
@@ -321,17 +323,26 @@ export function visitControlData<A>(
321
323
  {
322
324
  ...ctx,
323
325
  fields: fieldData.children,
324
- groupControl: c,
326
+ path: [...thisPath, cIndex],
325
327
  },
326
328
  cb,
327
329
  );
328
330
  if (cfResult !== undefined) return cfResult;
329
331
  }
332
+ cIndex++;
330
333
  }
331
334
  }
332
335
  }
333
336
  }
334
337
 
338
+ export function lookupChildControl(
339
+ data: DataContext,
340
+ child: JsonPath,
341
+ ): Control<any> | undefined {
342
+ const childPath = [...data.path, child];
343
+ return data.data.lookupControl(childPath);
344
+ }
345
+
335
346
  export function cleanDataForSchema(
336
347
  v: { [k: string]: any } | undefined,
337
348
  fields: SchemaField[],
@@ -384,3 +395,16 @@ export function getAllReferencedClasses(c: ControlDefinition): string[] {
384
395
  if (childClasses) return [tc, ...childClasses];
385
396
  return [tc];
386
397
  }
398
+
399
+ export function jsonPathString(jsonPath: JsonPath[]) {
400
+ let out = "";
401
+ jsonPath.forEach((v, i) => {
402
+ if (typeof v === "number") {
403
+ out += "[" + v + "]";
404
+ } else {
405
+ if (i > 0) out += ".";
406
+ out += v;
407
+ }
408
+ });
409
+ return out;
410
+ }
package/src/validators.ts CHANGED
@@ -70,11 +70,7 @@ function useJsonataValidator(
70
70
  hidden: boolean,
71
71
  i: number,
72
72
  ) {
73
- const errorMsg = useJsonataExpression(
74
- expr.expression,
75
- context.groupControl,
76
- context.root,
77
- );
73
+ const errorMsg = useJsonataExpression(expr.expression, context);
78
74
  useControlEffect(
79
75
  () => [hidden, errorMsg.value],
80
76
  ([hidden, msg]) => control.setError("jsonata" + i, !hidden ? msg : null),