import { ReactNode } from 'react'; import { Modal } from '../ui/modal'; import Button from '../ui/button/Button'; import SelectDropdown from '../form/SelectDropdown'; import Label from '../form/Label'; export interface FormField { key: string; label: string; type: 'text' | 'number' | 'email' | 'password' | 'select' | 'textarea'; placeholder?: string; options?: Array<{ value: string; label: string }>; value: any; onChange: (value: any) => void; required?: boolean; min?: number; max?: number; rows?: number; className?: string; } interface FormModalProps { isOpen: boolean; onClose: () => void; onSubmit: () => void; title: string; fields?: FormField[]; submitLabel?: string; cancelLabel?: string; isLoading?: boolean; className?: string; customFooter?: ReactNode; customBody?: ReactNode; // Custom body content that replaces fields } export default function FormModal({ isOpen, onClose, onSubmit, title, fields = [], submitLabel = 'Create', cancelLabel = 'Cancel', isLoading = false, className = 'max-w-2xl', customFooter, customBody, }: FormModalProps) { return (

{title}

{ e.preventDefault(); onSubmit(); }} className="space-y-4" > {customBody ? ( customBody ) : ( <> {fields.find(f => f.key === 'keyword') && (
f.key === 'keyword')!.value || ''} onChange={(e) => fields.find(f => f.key === 'keyword')!.onChange(e.target.value)} placeholder={fields.find(f => f.key === 'keyword')!.placeholder} required={fields.find(f => f.key === 'keyword')!.required} />
)} {(fields.find(f => f.key === 'volume') || fields.find(f => f.key === 'difficulty')) && (
{fields.find(f => f.key === 'volume') && (
f.key === 'volume')!.value || ''} onChange={(e) => { const value = e.target.value === '' ? '' : parseInt(e.target.value) || 0; fields.find(f => f.key === 'volume')!.onChange(value); }} placeholder={fields.find(f => f.key === 'volume')!.placeholder} required={fields.find(f => f.key === 'volume')!.required} />
)} {fields.find(f => f.key === 'difficulty') && (() => { const difficultyField = fields.find(f => f.key === 'difficulty')!; return (
{difficultyField.type === 'select' ? ( difficultyField.onChange(value)} className="w-full" /> ) : ( { const value = e.target.value === '' ? '' : parseInt(e.target.value) || 0; difficultyField.onChange(value); }} placeholder={difficultyField.placeholder} required={difficultyField.required} min={difficultyField.min} max={difficultyField.max} /> )}
); })()}
)} {fields.filter(f => f.key !== 'keyword' && f.key !== 'volume' && f.key !== 'difficulty').map((field) => { if (field.type === 'select') { return (
field.onChange(value)} className="w-full" />
); } if (field.type === 'textarea') { return (