fix(budget): match the add-expense input decimal separator to the list (#1645)

This commit is contained in:
Konstantinos Thermos
2026-07-21 21:36:47 +03:00
committed by GitHub
parent b8728078c4
commit 88cb7f22dd
5 changed files with 54 additions and 26 deletions
+15 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { splitReservationDateTime, resolveDayId, formatMoney, formatMoneySum, currencyDecimals } from './formatters'
import { splitReservationDateTime, resolveDayId, formatMoney, formatMoneySum, currencyDecimals, localizeAmountInput } from './formatters'
import { CURRENCIES, SYMBOLS, currenciesWith } from '../components/Budget/BudgetPanel.constants'
import type { Day } from '../types'
@@ -26,6 +26,20 @@ describe('resolveDayId', () => {
})
})
describe('localizeAmountInput (#1624)', () => {
it('shows the amount with the currency comma separator so the field matches the list', () => {
expect(localizeAmountInput('12.5', 'EUR')).toBe('12,5')
expect(localizeAmountInput('0.00', 'EUR')).toBe('0,00')
})
it('keeps the dot for dot-separator currencies', () => {
expect(localizeAmountInput('12.5', 'USD')).toBe('12.5')
})
it('passes an empty/nullish value through unchanged', () => {
expect(localizeAmountInput('', 'EUR')).toBe('')
expect(localizeAmountInput(null, 'EUR')).toBe('')
})
})
describe('KGS currency (#1400)', () => {
it('is selectable everywhere the shared currency list feeds', () => {
expect(CURRENCIES).toContain('KGS')
+14
View File
@@ -71,6 +71,20 @@ export function currencyLocale(currency: string): string {
return CURRENCY_LOCALE[(currency || '').toUpperCase()] || 'en-US'
}
// The decimal separator formatMoney renders a currency with (',' for EUR, '.' for
// USD) — same home-locale convention, so an amount input can match the displayed list.
export function currencyDecimalSeparator(currency: string): string {
return (0.1).toLocaleString(currencyLocale(currency)).replace(/\d/g, '') || '.'
}
// Show a dot-normalized amount string in an input using the currency's decimal
// separator, so the field reads the same way as the list. State stays dot-normalized;
// callers still normalize typed input back to a dot on change.
export function localizeAmountInput(value: string | number | null | undefined, currency: string): string {
const s = String(value ?? '')
return currencyDecimalSeparator(currency) === ',' ? s.replace('.', ',') : s
}
/**
* Locale- and currency-correct money formatting via Intl: the symbol position,
* thousands/decimal separators and decimal count all follow the user's locale