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

Function ArrayField

packages/react/src/ArrayField.tsx:10–48  ·  view source on GitHub ↗
({ field, path })

Source from the content-addressed store, hash-verified

8 field: ParsedField;
9 path: string[];
10}> = ({ field, path }) => {
11 const { uiComponents } = useAutoForm();
12 const { control } = useFormContext();
13 const { fields, append, remove } = useFieldArray({
14 control,
15 name: path.join("."),
16 });
17
18 const subFieldType = field.schema?.[0]?.type;
19 let defaultValue: any;
20 if (subFieldType === "object") {
21 defaultValue = {};
22 } else if (subFieldType === "array") {
23 defaultValue = [];
24 } else {
25 defaultValue = null;
26 }
27
28 return (
29 <uiComponents.ArrayWrapper
30 label={getLabel(field)}
31 field={field}
32 onAddItem={() => append(defaultValue)}
33 >
34 {fields.map((item, index) => (
35 <uiComponents.ArrayElementWrapper
36 key={item.id}
37 onRemove={() => remove(index)}
38 index={index}
39 >
40 <AutoFormField
41 field={field.schema![0]!}
42 path={[...path, index.toString()]}
43 />
44 </uiComponents.ArrayElementWrapper>
45 ))}
46 </uiComponents.ArrayWrapper>
47 );
48};

Callers

nothing calls this directly

Calls 2

useAutoFormFunction · 0.90
getLabelFunction · 0.90

Tested by

no test coverage detected