Format number inputs with commas
Formats numeric input fields to display commas for every three digits.
This commit is contained in:
@@ -17,13 +17,25 @@ const CategoryBudgetInputs: React.FC<CategoryBudgetInputsProps> = ({
|
||||
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<HTMLInputElement>, 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 (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<label className="text-sm text-gray-600">식비</label>
|
||||
<Input
|
||||
value={categoryBudgets.식비.toString()}
|
||||
onChange={e => handleCategoryInputChange(e.target.value, '식비')}
|
||||
value={formatWithCommas(categoryBudgets.식비)}
|
||||
onChange={(e) => handleInput(e, '식비')}
|
||||
className="neuro-pressed max-w-[150px]"
|
||||
/>
|
||||
</div>
|
||||
@@ -31,8 +43,8 @@ const CategoryBudgetInputs: React.FC<CategoryBudgetInputsProps> = ({
|
||||
<div className="flex items-center justify-between">
|
||||
<label className="text-sm text-gray-600">생활비</label>
|
||||
<Input
|
||||
value={categoryBudgets.생활비.toString()}
|
||||
onChange={e => handleCategoryInputChange(e.target.value, '생활비')}
|
||||
value={formatWithCommas(categoryBudgets.생활비)}
|
||||
onChange={(e) => handleInput(e, '생활비')}
|
||||
className="neuro-pressed max-w-[150px]"
|
||||
/>
|
||||
</div>
|
||||
@@ -40,8 +52,8 @@ const CategoryBudgetInputs: React.FC<CategoryBudgetInputsProps> = ({
|
||||
<div className="flex items-center justify-between">
|
||||
<label className="text-sm text-gray-600">교통비</label>
|
||||
<Input
|
||||
value={categoryBudgets.교통비.toString()}
|
||||
onChange={e => handleCategoryInputChange(e.target.value, '교통비')}
|
||||
value={formatWithCommas(categoryBudgets.교통비)}
|
||||
onChange={(e) => handleInput(e, '교통비')}
|
||||
className="neuro-pressed max-w-[150px]"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user