/** * 값+단위 조합 입력 필드 * Input(숫자) + Select(단위) 가로 배치 * * properties: { units, defaultUnit, precision } * 저장값: { value: number, unit: string } */ 'use client'; import { useCallback } from 'react'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import type { DynamicFieldRendererProps, UnitValueConfig } from '../types'; interface UnitValueData { value: number | null; unit: string; } function parseUnitValue(raw: unknown, defaultUnit: string): UnitValueData { if (raw && typeof raw === 'object' && !Array.isArray(raw)) { const obj = raw as Record; return { value: obj.value !== null && obj.value !== undefined ? Number(obj.value) : null, unit: typeof obj.unit === 'string' ? obj.unit : defaultUnit, }; } // 숫자만 들어온 경우 if (typeof raw === 'number') { return { value: raw, unit: defaultUnit }; } return { value: null, unit: defaultUnit }; } export function UnitValueField({ field, value, onChange, error, disabled, }: DynamicFieldRendererProps) { const fieldKey = field.field_key || `field_${field.id}`; const config = (field.properties || {}) as UnitValueConfig; const units = config.units || []; const defaultUnit = config.defaultUnit || (units.length > 0 ? units[0].value : ''); const precision = config.precision; const data = parseUnitValue(value, defaultUnit); const handleValueChange = useCallback((e: React.ChangeEvent) => { const raw = e.target.value; if (raw === '' || raw === '-') { onChange({ value: null, unit: data.unit }); return; } const num = parseFloat(raw); if (!isNaN(num)) { const final = precision !== undefined ? Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision) : num; onChange({ value: final, unit: data.unit }); } }, [data.unit, precision, onChange]); const handleUnitChange = useCallback((newUnit: string) => { onChange({ value: data.value, unit: newUnit }); }, [data.value, onChange]); return (
{units.length > 0 ? ( ) : ( {data.unit || '-'} )}
{error && (

{error}

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

* {field.description}

)}
); }