import { useState, useRef, useId } from 'react' import { useChatStore } from '@/features/chat/stores/useChatStore' // 단위(원/%) 오버레이 + 전송 버튼이 붙은 값 입력 (Percent / Price 공용) function InputWithUnit({ value, onChange, onSubmit, placeholder, unit, error, inputRef, ariaLabel, }: { value: string onChange: (e: React.ChangeEvent) => void onSubmit: () => void placeholder: string unit: string error: string inputRef: React.RefObject ariaLabel: string }) { const errorId = useId() return (
e.key === 'Enter' && onSubmit()} className="h-[50px] flex-1 bg-white border-none rounded-[999px] outline-none ring-1 ring-primary headline-3 text-negative placeholder:text-neutral-60 pl-[32px]" placeholder={placeholder} aria-label={ariaLabel} aria-invalid={!!error} aria-describedby={error ? errorId : undefined} />
{value} {value && {unit}}
{error && ( )}
) } export function Percent() { const [percent, setPercent] = useState('') const [error, setError] = useState('') const inputRef = useRef(null) const sendMessage = useChatStore((s) => s.sendMessage) const handleChange = (e: React.ChangeEvent) => { const value = e.target.value if (value === '') { setPercent('') setError('') return } if (!/^\d*\.?\d*$/.test(value)) return if (value.includes('.')) { const parts = value.split('.') if (parts[1] && parts[1].length > 1) return } else if (value.length > 2) { return } if (parseFloat(value) > 80) return setPercent(value) setError('') } const handleSubmit = () => { if (!percent || percent === '.') { setError('숫자를 입력해주세요') return } sendMessage(`${parseFloat(percent).toString()}%`, 'percent') setError('') } return ( ) } export function Price({ priceErrorMessage }: { priceErrorMessage?: string }) { const [inputValue, setInputValue] = useState('') const [error, setError] = useState('') const inputRef = useRef(null) const sendMessage = useChatStore((s) => s.sendMessage) const setPriceErrorMessage = useChatStore((s) => s.setPriceErrorMessage) const displayError = priceErrorMessage || error const handleChange = (e: React.ChangeEvent) => { const value = e.target.value if (priceErrorMessage) setPriceErrorMessage('') if (value === '') { setInputValue('') setError('') return } const numberOnly = value.replace(/,/g, '') if (!/^\d*$/.test(numberOnly)) return if (parseInt(numberOnly) > 999999999999) return setInputValue(numberOnly.replace(/\B(?=(\d{3})+(?!\d))/g, ',')) setError('') } const handleSubmit = () => { if (!inputValue) { if (priceErrorMessage) setPriceErrorMessage('') setError('가격을 입력해주세요') return } sendMessage(`${inputValue}원`, 'price') setError('') } return ( ) }