turash/bugulma/frontend/components/form/DynamicFieldArray.tsx
Damir Mukimov 6347f42e20
Consolidate repositories: Remove nested frontend .git and merge into main repository
- Remove nested git repository from bugulma/frontend/.git
- Add all frontend files to main repository tracking
- Convert from separate frontend/backend repos to unified monorepo
- Preserve all frontend code and development history as tracked files
- Eliminate nested repository complexity for simpler development workflow

This creates a proper monorepo structure with frontend and backend
coexisting in the same repository for easier development and deployment.
2025-11-25 06:02:57 +01:00

64 lines
1.7 KiB
TypeScript

import React from 'react';
import { useFieldArray, Control, FieldErrors, FieldValues, Path } from 'react-hook-form';
import Button from '@/components/ui/Button.tsx';
interface DynamicFieldArrayProps<T extends FieldValues> {
control: Control<T>;
name: Path<T>;
title: string;
addText: string;
errors: FieldErrors<T>;
defaultItem: Record<string, unknown>;
children?: (index: number) => React.ReactNode;
}
const DynamicFieldArray = <T extends FieldValues>({
control,
name,
title,
addText,
errors,
defaultItem,
children,
}: DynamicFieldArrayProps<T>) => {
const { fields, append, remove } = useFieldArray({ control, name });
const arrayErrors = errors[name as string];
return (
<div>
<h3 className="text-base font-semibold mb-2">{title}</h3>
<div className="space-y-3">
{fields.map((field, index) => (
<div key={field.id} className="flex items-start gap-2">
<div className="flex-1">{children && children(index)}</div>
<Button
type="button"
variant="outline"
onClick={() => remove(index)}
className="h-10 w-10 p-0 shrink-0 mt-1.5"
>
</Button>
</div>
))}
</div>
{arrayErrors && (
<p className="text-destructive text-xs mt-1">
{(arrayErrors as { message?: string; root?: { message?: string } })?.message ||
(arrayErrors as { message?: string; root?: { message?: string } })?.root?.message}
</p>
)}
<Button
type="button"
variant="ghost"
onClick={() => append(defaultItem)}
className="text-sm mt-2"
>
{addText}
</Button>
</div>
);
};
export default DynamicFieldArray;