/** * 드롭다운(Select) 필드 컴포넌트 * 기존 ItemForm과 100% 동일한 디자인 */ 'use client'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import type { DynamicFieldRendererProps } from '../types'; // 옵션을 {label, value} 형태로 정규화 function normalizeOptions(rawOptions: unknown): Array<{ label: string; value: string }> { if (!rawOptions) return []; // 문자열인 경우: 콤마로 분리 if (typeof rawOptions === 'string') { return rawOptions.split(',').map(o => { const trimmed = o.trim(); return { label: trimmed, value: trimmed }; }); } // 배열인 경우 if (Array.isArray(rawOptions)) { return rawOptions.map(item => { // 이미 {label, value} 형태 if (typeof item === 'object' && item !== null && 'value' in item) { return { label: String(item.label || item.value), value: String(item.value), }; } // 문자열 배열 const str = String(item); return { label: str, value: str }; }); } return []; } export function DropdownField({ field, value, onChange, error, disabled, unitOptions, }: DynamicFieldRendererProps) { const fieldKey = field.field_key || `field_${field.id}`; // is_active 필드인지 확인 const isActiveField = fieldKey === 'is_active' || fieldKey.endsWith('_is_active'); // 옵션을 먼저 정규화 (is_active 값 변환에 필요) const rawOptions = normalizeOptions(field.options); // is_active 필드일 때 boolean 값을 옵션에 맞게 변환 let stringValue = ''; if (value !== null && value !== undefined) { if (isActiveField && rawOptions.length >= 2) { // boolean/숫자 값을 첫번째(활성) 또는 두번째(비활성) 옵션 값으로 매핑 const isActive = value === true || value === 'true' || value === 1 || value === '1' || value === '활성'; stringValue = isActive ? rawOptions[0].value : rawOptions[1].value; } else { stringValue = String(value); } } // field_key 또는 field_name이 '단위'/'unit' 관련이면 unitOptions 사용 const isUnitField = fieldKey.toLowerCase().includes('unit') || fieldKey.includes('단위') || field.field_name.includes('단위') || field.field_name.toLowerCase().includes('unit'); // 옵션 목록 결정 let options: Array<{ label: string; value: string }> = []; if (isUnitField && unitOptions && unitOptions.length > 0) { options = unitOptions.map((u) => ({ label: `${u.label} (${u.value})`, value: u.value, })); } else { // rawOptions는 이미 위에서 정규화됨 options = rawOptions; } // 옵션이 없으면 드롭다운을 disabled로 표시 const hasOptions = options.length > 0; return (
{error && (

{error}

)} {!error && field.description && (

* {field.description}

)}
); }