mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
fix(budget): match the add-expense input decimal separator to the list (#1645)
This commit is contained in:
committed by
GitHub
parent
b8728078c4
commit
88cb7f22dd
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user