feat(WEB): DynamicItemForm 필드 타입 확장 및 컴포넌트 레지스트리 추가
- DynamicFieldRenderer에 신규 필드 타입 추가 (Currency, File, MultiSelect, Radio, Reference, Toggle, UnitValue, Computed) - DynamicTableSection 및 TableCellRenderer 추가 - 필드 프리셋 및 설정 구조 분리 - 컴포넌트 레지스트리 개발 도구 페이지 추가 - UniversalListPage 개선 - 근태관리 코드 정리 - 즐겨찾기 기능 및 동적 필드 타입 백엔드 스펙 문서 추가 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
128
src/components/items/DynamicItemForm/fields/UnitValueField.tsx
Normal file
128
src/components/items/DynamicItemForm/fields/UnitValueField.tsx
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* 값+단위 조합 입력 필드
|
||||
* 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<string, unknown>;
|
||||
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<HTMLInputElement>) => {
|
||||
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 (
|
||||
<div>
|
||||
<Label htmlFor={fieldKey}>
|
||||
{field.field_name}
|
||||
{field.is_required && <span className="text-red-500"> *</span>}
|
||||
</Label>
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
id={fieldKey}
|
||||
type="number"
|
||||
placeholder={field.placeholder || '값 입력'}
|
||||
value={data.value !== null ? String(data.value) : ''}
|
||||
onChange={handleValueChange}
|
||||
disabled={disabled}
|
||||
step={precision !== undefined ? Math.pow(10, -precision) : 'any'}
|
||||
className={`flex-1 ${error ? 'border-red-500' : ''}`}
|
||||
/>
|
||||
{units.length > 0 ? (
|
||||
<Select
|
||||
key={`${fieldKey}-unit-${data.unit}`}
|
||||
value={data.unit}
|
||||
onValueChange={handleUnitChange}
|
||||
disabled={disabled}
|
||||
>
|
||||
<SelectTrigger className="w-24 shrink-0">
|
||||
<SelectValue placeholder="단위" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{units.map((u) => (
|
||||
<SelectItem key={u.value} value={u.value}>
|
||||
{u.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
) : (
|
||||
<span className="flex items-center text-sm text-muted-foreground px-2">
|
||||
{data.unit || '-'}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{error && (
|
||||
<p className="text-xs text-red-500 mt-1">{error}</p>
|
||||
)}
|
||||
{!error && field.description && (
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
* {field.description}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user