"use client"; import React 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"; interface AdminSectionProps { section: SectionConfig; values: Record; errors: Record; disabled?: boolean; onChange: (fieldId: string, value: any) => void; onBlur?: (fieldId: string) => void; className?: string; } export function AdminSection({ section, values, errors, disabled = false, onChange, onBlur, className }: 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) => { const value = getFieldValue(field); const error = errors[field.id]; const fieldDisabled = disabled || field.disabled; return (
{field.description && (

{field.description}

)}
{field.type === "switch" && ( onChange(field.id, newValue)} onBlur={() => onBlur?.(field.id)} /> )}
{field.type !== "switch" && ( onChange(field.id, newValue)} onBlur={() => onBlur?.(field.id)} /> )}
); }; // 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}
); }