fix(budget): seed edit fields with two decimals and allow signed amounts for refunds (#2175, #2176)

This commit is contained in:
Maurice
2026-09-01 15:07:26 +02:00
committed by Maurice
parent 49b0ce28d5
commit d1aa573167
18 changed files with 717 additions and 78 deletions
@@ -91,6 +91,30 @@ describe('NumericInput', () => {
expect(input.value).toBe('36.72')
})
it('FE-COMP-NUMINPUT-010: signed-decimal mode keeps a leading minus and the comma (#2176)', () => {
render(<Harness initial="" mode="signed-decimal" />)
const input = screen.getByLabelText('field') as HTMLInputElement
// A refund typed on a European keypad: sign and comma both survive.
fireEvent.change(input, { target: { value: '-12,50' } })
expect(input.value).toBe('-12,50')
fireEvent.change(input, { target: { value: '-100' } })
expect(input.value).toBe('-100')
// A minus anywhere but the front is not a sign.
fireEvent.change(input, { target: { value: '12-50' } })
expect(input.value).toBe('1250')
fireEvent.change(input, { target: { value: '-1x2.5eur' } })
expect(input.value).toBe('-12.5')
})
it('FE-COMP-NUMINPUT-011: signed-decimal mode uses the decimal keypad', () => {
render(<Harness initial="" mode="signed-decimal" />)
expect((screen.getByLabelText('field') as HTMLInputElement).inputMode).toBe('decimal')
})
it('FE-COMP-NUMINPUT-009: typing inside the one-frame window is not swallowed by the deferred select', async () => {
// The deferred select() exists for WebKit, but it must not fire *after* the user has
// started typing: it would select the half-typed value and the next character would
@@ -1,6 +1,6 @@
import { useRef, type InputHTMLAttributes, type Ref } from 'react'
export type NumericMode = 'integer' | 'decimal' | 'signed'
export type NumericMode = 'integer' | 'decimal' | 'signed' | 'signed-decimal'
const SANITIZERS: Record<NumericMode, (raw: string) => string> = {
// Digits only — quantities, weights, day counts.
@@ -14,6 +14,14 @@ const SANITIZERS: Record<NumericMode, (raw: string) => string> = {
const digits = raw.replace(/[^0-9.]/g, '')
return negative ? `-${digits}` : digits
},
// 'decimal' plus a leading '-' — money fields that accept a refund (#2176).
// Distinct from 'signed' on purpose: that one strips the comma, which a
// European keypad needs for amounts.
'signed-decimal': raw => {
const negative = raw.trimStart().startsWith('-')
const digits = raw.replace(/[^0-9.,]/g, '')
return negative ? `-${digits}` : digits
},
}
type Props = Omit<InputHTMLAttributes<HTMLInputElement>, 'type' | 'onChange' | 'value'> & {