Format number inputs with commas

Formats numeric input fields to display commas for every three digits.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-15 04:08:55 +00:00
parent 136df4a5e9
commit 2071453b2b
3 changed files with 65 additions and 22 deletions

View File

@@ -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<HTMLInputElement>) => {
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 = () => {
<FormLabel></FormLabel>
<FormControl>
<Input
type="number"
placeholder="0"
{...field}
value={field.value}
onChange={handleAmountChange}
/>
</FormControl>
</FormItem>