MCPcopy
hub / github.com/vantezzen/autoform / AutoFormField

Function AutoFormField

packages/react/src/AutoFormField.tsx:13–69  ·  view source on GitHub ↗
({ field, path })

Source from the content-addressed store, hash-verified

11 field: ParsedField;
12 path: string[];
13}> = ({ field, path }) => {
14 const { formComponents, uiComponents } = useAutoForm();
15 const {
16 register,
17 formState: { errors },
18 getValues,
19 } = useFormContext();
20
21 const fullPath = path.join(".");
22 const error = getPathInObject(errors, path)?.message as string | undefined;
23 const value = getValues(fullPath);
24
25 const FieldWrapper =
26 field.fieldConfig?.fieldWrapper || uiComponents.FieldWrapper;
27
28 let FieldComponent: React.ComponentType<AutoFormFieldProps> = () => (
29 <uiComponents.ErrorMessage
30 error={`[AutoForm Configuration Error] No component found for type "${field.type}" nor a fallback`}
31 />
32 );
33
34 if (field.type === "array") {
35 FieldComponent = ArrayField;
36 } else if (field.type === "object") {
37 FieldComponent = ObjectField;
38 } else if (field.type in formComponents) {
39 FieldComponent = formComponents[field.type as keyof typeof formComponents]!;
40 } else if ("fallback" in formComponents) {
41 FieldComponent = formComponents.fallback;
42 }
43
44 return (
45 <FieldWrapper
46 label={getLabel(field)}
47 error={error}
48 id={fullPath}
49 field={field}
50 >
51 <FieldComponent
52 label={getLabel(field)}
53 field={field}
54 value={value}
55 error={error}
56 id={fullPath}
57 key={fullPath}
58 path={path}
59 inputProps={{
60 required: field.required,
61 error: error,
62 key: `${fullPath}-input`,
63 ...field.fieldConfig?.inputProps,
64 ...register(fullPath),
65 }}
66 />
67 </FieldWrapper>
68 );
69};

Callers

nothing calls this directly

Calls 3

useAutoFormFunction · 0.90
getPathInObjectFunction · 0.90
getLabelFunction · 0.90

Tested by

no test coverage detected