import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { CHART_TIME_FORMAT_PRESETS, formatUnixWithChartPattern, isPresetChartTimeFormat, normalizeChartTimeFormat, } from '../utils/chartTimeFormat'; const CUSTOM_VALUE = '__custom__'; interface ChartTimeFormatPickerProps { value: string; onChange: (format: string) => void; } const ChartTimeFormatPicker: React.FC = ({ value, onChange }) => { const normalized = normalizeChartTimeFormat(value); const isPreset = isPresetChartTimeFormat(normalized); const [selectValue, setSelectValue] = useState( isPreset ? normalized : CUSTOM_VALUE, ); const [customText, setCustomText] = useState( isPreset ? '' : normalized, ); useEffect(() => { const n = normalizeChartTimeFormat(value); if (isPresetChartTimeFormat(n)) { setSelectValue(n); setCustomText(''); } else { setSelectValue(CUSTOM_VALUE); setCustomText(n); } }, [value]); const preview = useMemo(() => { const fmt = selectValue === CUSTOM_VALUE ? normalizeChartTimeFormat(customText) : selectValue; return formatUnixWithChartPattern(Math.floor(Date.now() / 1000), fmt); }, [selectValue, customText]); const commitCustom = useCallback(() => { const next = normalizeChartTimeFormat(customText); onChange(next); }, [customText, onChange]); return (
{selectValue === CUSTOM_VALUE && ( setCustomText(e.target.value)} onBlur={commitCustom} onKeyDown={e => { if (e.key === 'Enter') commitCustom(); }} /> )} 미리보기: {preview} 토큰: yyyy, yy, MM, dd, HH, mm, ss
); }; export default ChartTimeFormatPicker;