import React, { useState } from 'react'; import { PlusIcon, X } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useNavigate } from 'react-router-dom'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog'; import { Form, FormField, FormItem, FormLabel, FormControl } from './ui/form'; import { Input } from './ui/input'; import { Button } from './ui/button'; import { useForm } from 'react-hook-form'; import { ToggleGroup, ToggleGroupItem } from './ui/toggle-group'; interface ExpenseFormValues { title: string; amount: string; category: string; } const EXPENSE_CATEGORIES = ['식비', '생활비', '교통비']; const AddTransactionButton = () => { const [showExpenseDialog, setShowExpenseDialog] = useState(false); const navigate = useNavigate(); const form = useForm({ defaultValues: { title: '', amount: '', category: '식비', } }); // 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) => { // Remove commas before processing the amount const numericAmount = data.amount.replace(/,/g, ''); const processedData = { ...data, amount: numericAmount }; console.log('Expense data:', processedData); setShowExpenseDialog(false); // 여기에서 실제 데이터 처리 로직을 구현할 수 있습니다 }; return ( <>
지출 추가
( 제목 )} /> ( 금액 )} /> ( 카테고리 { if (value) field.onChange(value); }} > {EXPENSE_CATEGORIES.map((category) => ( {category} ))} )} />
); }; export default AddTransactionButton;