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:
126
src/components/items/DynamicItemForm/fields/CurrencyField.tsx
Normal file
126
src/components/items/DynamicItemForm/fields/CurrencyField.tsx
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* 통화 금액 입력 필드
|
||||
* 천단위 콤마 포맷, 통화 기호 prefix 지원
|
||||
*
|
||||
* properties: { currency, precision, showSymbol, allowNegative }
|
||||
* 저장값: number (포맷 없이)
|
||||
*/
|
||||
|
||||
'use client';
|
||||
|
||||
import { useState, useCallback } from 'react';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import type { DynamicFieldRendererProps, CurrencyConfig } from '../types';
|
||||
|
||||
const CURRENCY_SYMBOLS: Record<string, string> = {
|
||||
KRW: '\u20A9',
|
||||
USD: '$',
|
||||
EUR: '\u20AC',
|
||||
JPY: '\u00A5',
|
||||
CNY: '\u00A5',
|
||||
GBP: '\u00A3',
|
||||
};
|
||||
|
||||
function formatCurrency(num: number, precision: number): string {
|
||||
const fixed = num.toFixed(precision);
|
||||
const [intPart, decPart] = fixed.split('.');
|
||||
const formatted = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
return decPart !== undefined ? `${formatted}.${decPart}` : formatted;
|
||||
}
|
||||
|
||||
function parseCurrency(str: string): number {
|
||||
const cleaned = str.replace(/[^0-9.\-]/g, '');
|
||||
const num = parseFloat(cleaned);
|
||||
return isNaN(num) ? 0 : num;
|
||||
}
|
||||
|
||||
export function CurrencyField({
|
||||
field,
|
||||
value,
|
||||
onChange,
|
||||
error,
|
||||
disabled,
|
||||
}: DynamicFieldRendererProps) {
|
||||
const fieldKey = field.field_key || `field_${field.id}`;
|
||||
const config = (field.properties || {}) as CurrencyConfig;
|
||||
const currency = config.currency || 'KRW';
|
||||
const precision = config.precision ?? 0;
|
||||
const showSymbol = config.showSymbol !== false;
|
||||
const allowNegative = config.allowNegative === true;
|
||||
|
||||
const symbol = CURRENCY_SYMBOLS[currency] || currency;
|
||||
|
||||
const numericValue = value !== null && value !== undefined ? Number(value) : null;
|
||||
|
||||
const [isFocused, setIsFocused] = useState(false);
|
||||
const [inputValue, setInputValue] = useState(
|
||||
numericValue !== null ? String(numericValue) : ''
|
||||
);
|
||||
|
||||
const handleFocus = useCallback(() => {
|
||||
setIsFocused(true);
|
||||
setInputValue(numericValue !== null ? String(numericValue) : '');
|
||||
}, [numericValue]);
|
||||
|
||||
const handleBlur = useCallback(() => {
|
||||
setIsFocused(false);
|
||||
if (inputValue === '' || inputValue === '-') {
|
||||
onChange(null);
|
||||
return;
|
||||
}
|
||||
const parsed = parseCurrency(inputValue);
|
||||
const final = allowNegative ? parsed : Math.abs(parsed);
|
||||
onChange(final);
|
||||
}, [inputValue, onChange, allowNegative]);
|
||||
|
||||
const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const raw = e.target.value;
|
||||
// 숫자, 점, 마이너스만 허용
|
||||
const pattern = allowNegative ? /[^0-9.\-]/g : /[^0-9.]/g;
|
||||
const cleaned = raw.replace(pattern, '');
|
||||
setInputValue(cleaned);
|
||||
}, [allowNegative]);
|
||||
|
||||
const displayValue = isFocused
|
||||
? inputValue
|
||||
: numericValue !== null
|
||||
? formatCurrency(numericValue, precision)
|
||||
: '';
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Label htmlFor={fieldKey}>
|
||||
{field.field_name}
|
||||
{field.is_required && <span className="text-red-500"> *</span>}
|
||||
</Label>
|
||||
<div className="relative">
|
||||
{showSymbol && (
|
||||
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-sm text-muted-foreground pointer-events-none">
|
||||
{symbol}
|
||||
</span>
|
||||
)}
|
||||
<Input
|
||||
id={fieldKey}
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
placeholder={field.placeholder || '0'}
|
||||
value={displayValue}
|
||||
onChange={handleChange}
|
||||
onFocus={handleFocus}
|
||||
onBlur={handleBlur}
|
||||
disabled={disabled}
|
||||
className={`${showSymbol ? 'pl-8' : ''} text-right ${error ? 'border-red-500' : ''}`}
|
||||
/>
|
||||
</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