import type { FormField } from '@/lib/types'; import { Input, Textarea, Select, Field, type Option } from '@/components/ui'; export type TaxonomyOptionsMap = Record; function resolveOptions(f: FormField, taxonomies: TaxonomyOptionsMap): Option[] { if (!f.options) return []; if (Array.isArray(f.options)) return f.options.map(v => ({ value: v, label: v })); return taxonomies[f.options.taxonomy] ?? []; } export function DynamicForm({ fields, value, onChange, taxonomies }: { fields: FormField[]; value: Record; onChange: (next: Record) => void; taxonomies: TaxonomyOptionsMap; }) { if (!fields.length) return null; const set = (key: string, v: unknown) => onChange({ ...value, [key]: v }); const renderField = (f: FormField) => { const v = value[f.key]; const commonError = f.required && (v === undefined || v === null || v === '') ? '该项必填' : ''; const withUnit = (node: React.ReactNode) => f.unit ? (
{node} {f.unit}
) : node; switch (f.widget) { case 'textarea': return (