diff --git a/src/components/AddTransactionButton.tsx b/src/components/AddTransactionButton.tsx index 3b6cb4f..6a88454 100644 --- a/src/components/AddTransactionButton.tsx +++ b/src/components/AddTransactionButton.tsx @@ -30,8 +30,27 @@ const AddTransactionButton = () => { } }); + // Format number with commas + const formatWithCommas = (value: string): string => { + // Remove commas first to avoid duplicates when typing + const numericValue = value.replace(/[^0-9]/g, ''); + return numericValue.replace(/\B(?=(\d{3})+(?!\d))/g, ','); + }; + + const handleAmountChange = (e: React.ChangeEvent) => { + const formattedValue = formatWithCommas(e.target.value); + form.setValue('amount', formattedValue); + }; + const onSubmit = (data: ExpenseFormValues) => { - console.log('Expense data:', data); + // Remove commas before processing the amount + const numericAmount = data.amount.replace(/,/g, ''); + const processedData = { + ...data, + amount: numericAmount + }; + + console.log('Expense data:', processedData); setShowExpenseDialog(false); // 여기에서 실제 데이터 처리 로직을 구현할 수 있습니다 }; @@ -77,9 +96,9 @@ const AddTransactionButton = () => { 금액 diff --git a/src/components/BudgetInputCard.tsx b/src/components/BudgetInputCard.tsx index e2c36ea..da4d84b 100644 --- a/src/components/BudgetInputCard.tsx +++ b/src/components/BudgetInputCard.tsx @@ -27,21 +27,18 @@ const BudgetInputCard: React.FC = ({ }); const [isOpen, setIsOpen] = useState(false); - // Format for display without commas - const formatForInput = (amount: number) => { - return amount.toString(); - }; - // Format with commas for display const formatWithCommas = (amount: string) => { - return amount.replace(/\B(?=(\d{3})+(?!\d))/g, ','); + // Remove commas first to handle re-formatting + const numericValue = amount.replace(/,/g, ''); + return numericValue.replace(/\B(?=(\d{3})+(?!\d))/g, ','); }; useEffect(() => { setBudgetInputs({ - daily: formatForInput(initialBudgets.daily), - weekly: formatForInput(initialBudgets.weekly), - monthly: formatForInput(initialBudgets.monthly) + daily: initialBudgets.daily.toString(), + weekly: initialBudgets.weekly.toString(), + monthly: initialBudgets.monthly.toString() }); }, [initialBudgets]); @@ -55,7 +52,7 @@ const BudgetInputCard: React.FC = ({ }; const handleSave = () => { - const amount = parseInt(budgetInputs[selectedTab], 10) || 0; + const amount = parseInt(budgetInputs[selectedTab].replace(/,/g, ''), 10) || 0; onSave(selectedTab, amount); // Close the collapsible after saving setIsOpen(false); @@ -86,7 +83,12 @@ const BudgetInputCard: React.FC = ({
- handleInputChange(e.target.value, 'daily')} placeholder="목표 금액 입력" className="neuro-pressed" /> + handleInputChange(e.target.value, 'daily')} + placeholder="목표 금액 입력" + className="neuro-pressed" + /> @@ -96,7 +98,12 @@ const BudgetInputCard: React.FC = ({
- handleInputChange(e.target.value, 'weekly')} placeholder="목표 금액 입력" className="neuro-pressed" /> + handleInputChange(e.target.value, 'weekly')} + placeholder="목표 금액 입력" + className="neuro-pressed" + /> @@ -106,7 +113,12 @@ const BudgetInputCard: React.FC = ({
- handleInputChange(e.target.value, 'monthly')} placeholder="목표 금액 입력" className="neuro-pressed" /> + handleInputChange(e.target.value, 'monthly')} + placeholder="목표 금액 입력" + className="neuro-pressed" + /> diff --git a/src/components/CategoryBudgetInputs.tsx b/src/components/CategoryBudgetInputs.tsx index 7d0ccd4..c623d2b 100644 --- a/src/components/CategoryBudgetInputs.tsx +++ b/src/components/CategoryBudgetInputs.tsx @@ -17,13 +17,25 @@ const CategoryBudgetInputs: React.FC = ({ categoryBudgets, handleCategoryInputChange }) => { + // Format number with commas for display + const formatWithCommas = (value: number): string => { + return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); + }; + + // Handle input with comma formatting + const handleInput = (e: React.ChangeEvent, category: keyof CategoryBudget) => { + // Remove all non-numeric characters before passing to parent handler + const numericValue = e.target.value.replace(/[^0-9]/g, ''); + handleCategoryInputChange(numericValue, category); + }; + return (
handleCategoryInputChange(e.target.value, '식비')} + value={formatWithCommas(categoryBudgets.식비)} + onChange={(e) => handleInput(e, '식비')} className="neuro-pressed max-w-[150px]" />
@@ -31,8 +43,8 @@ const CategoryBudgetInputs: React.FC = ({
handleCategoryInputChange(e.target.value, '생활비')} + value={formatWithCommas(categoryBudgets.생활비)} + onChange={(e) => handleInput(e, '생활비')} className="neuro-pressed max-w-[150px]" />
@@ -40,8 +52,8 @@ const CategoryBudgetInputs: React.FC = ({
handleCategoryInputChange(e.target.value, '교통비')} + value={formatWithCommas(categoryBudgets.교통비)} + onChange={(e) => handleInput(e, '교통비')} className="neuro-pressed max-w-[150px]" />