"use client"; import React, { useEffect } from "react"; import { cn } from "@/lib/utils"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { SectionConfig, FieldConfig } from "@/types/admin-panel"; import { FieldRenderer } from "./field-renderer"; import { FormControl, FormField, FormItem, FormLabel } from "../ui/form"; import { useForm, UseFormReturn, } from "react-hook-form"; import { toast } from "sonner"; interface AdminSectionProps { section: SectionConfig; values?: Record; errors?: Record; disabled?: boolean; onChange: (fieldId: string, value: any) => void; onBlur?: (fieldId: string) => void; className?: string; form?: any; } export function AdminSection({ section, values, errors, disabled = false, onChange, onBlur, className, form }: AdminSectionProps) { // Filter fields based on conditional rendering const visibleFields = section.fields.filter(field => { if (field.showWhen) { return field.showWhen(values ?? {}); } return true; }); // Get field value helper const getFieldValue = (field: FieldConfig) => { return values?.[field.id] ?? field.value; }; // Render field with label and description const renderFieldWithLabel = (field: FieldConfig) => { return ( ( {field.label} onChange(field.id, newValue)} onBlur={() => onBlur?.(field.id)} form_field={formField} /> )} /> ); }; // Use custom render function if provided if (section.render) { const children = (
{visibleFields.map(renderFieldWithLabel)}
); return section.render(section, children); } // Default card layout const content = (
{visibleFields.map(renderFieldWithLabel)}
); return (
{section.icon} {section.title} {section.description && ( {section.description} )}
{content}
); }