Files
sam-react-prod/src/components/items/DynamicItemForm/DynamicFormRenderer.tsx

75 lines
1.9 KiB
TypeScript
Raw Normal View History

/**
* DynamicFormRenderer Component
*
*
* -
* - /
*/
'use client';
import { DynamicSection } from './DynamicSection';
import { useConditionalFields } from './hooks/useConditionalFields';
import type { DynamicFormRendererProps, DynamicSection as DynamicSectionType } from './types';
export function DynamicFormRenderer({
sections,
conditionalSections,
conditionalFields,
values,
errors,
onChange,
onBlur,
disabled,
}: DynamicFormRendererProps) {
// 조건부 표시 로직
const { isSectionVisible, isFieldVisible } = useConditionalFields({
sections,
conditionalSections,
conditionalFields,
values,
});
// 섹션 순서대로 정렬
const sortedSections = [...sections].sort((a, b) => a.order_no - b.order_no);
// 표시할 섹션만 필터링
const visibleSections = sortedSections.filter((section) =>
isSectionVisible(section.id)
);
// 각 섹션의 표시할 필드만 필터링
const sectionsWithVisibleFields: DynamicSectionType[] = visibleSections.map((section) => ({
...section,
fields: section.fields.filter((field) =>
isFieldVisible(section.id, field.id)
),
}));
if (sectionsWithVisibleFields.length === 0) {
return (
<div className="p-8 text-center text-gray-500 border-2 border-dashed rounded-lg">
<p> .</p>
<p className="text-sm mt-1"> .</p>
</div>
);
}
return (
<div className="space-y-4">
{sectionsWithVisibleFields.map((section) => (
<DynamicSection
key={section.id}
section={section}
values={values}
errors={errors}
onChange={onChange}
onBlur={onBlur}
disabled={disabled}
/>
))}
</div>
);
}
export default DynamicFormRenderer;