diff --git a/resources/js/components/decimal-input.tsx b/resources/js/components/decimal-input.tsx index 9c587f35..ce6e5ea1 100644 --- a/resources/js/components/decimal-input.tsx +++ b/resources/js/components/decimal-input.tsx @@ -45,8 +45,33 @@ export function DecimalInput({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [value]); const read = normalizeDecimalInput(text); + // A value saved before this field existed (for example «25.000» from the + // browser's number input) is kept as it is until the investor says what it + // meant: as written in Mozambique, or with the point as decimal separator. + const storedRead = normalizeDecimalInput(value); + const pointRead = Number(value.trim()); + const legacyChoices = + value !== '' && text === value && storedRead !== value + ? [ + ...(storedRead ? [storedRead] : []), + ...(Number.isFinite(pointRead) && + String(pointRead) !== storedRead + ? [String(pointRead)] + : []), + ] + : []; + const choose = (choice: string) => { + setText(choice); + emitted.current = choice; + onValueChange(choice); + }; const reinterpreted = - read !== null && read !== '' && read !== text.trim() ? read : null; + !legacyChoices.length && + read !== null && + read !== '' && + read !== text.trim() + ? read + : null; return ( <> + {legacyChoices.length > 0 && ( + + Valor guardado «{value}». Confirme o que pretendia:{' '} + {legacyChoices.map((choice) => ( + + ))} + + )} {reinterpreted && ( Lido como {presentDecimal(reinterpreted)} diff --git a/tests/frontend/decimal-input.test.mjs b/tests/frontend/decimal-input.test.mjs index 41f0e5ab..08fd2bb3 100644 --- a/tests/frontend/decimal-input.test.mjs +++ b/tests/frontend/decimal-input.test.mjs @@ -150,3 +150,31 @@ test('an integer field emptied while retyping shows again the value still in for f.input().props.onBlur({}); assert.equal(f.input().props.value, '24'); }); + +void test('a value saved as «25.000» before this field is kept until the investor confirms 25 000 or 25', () => { + const emitted = []; + const f = field('25.000', emitted); + assert.equal(f.input().props.value, '25.000'); + const note = f.hint(); + const html = renderToStaticMarkup(note); + assert.match(html, /Valor guardado «25.000»/); + assert.deepEqual(emitted, [], 'nothing changes on its own'); + const buttons = [note.props.children] + .flat(Infinity) + .flatMap((child) => (Array.isArray(child) ? child : [child])); + const choices = buttons.filter((node) => node?.type === 'button'); + assert.deepEqual( + choices.map((node) => node.props.children), + ['25 000', '25'], + ); + choices[1].props.onClick(); + assert.equal(emitted.at(-1), '25'); + assert.equal(f.input().props.value, '25'); + assert.equal(f.hint(), undefined); + const canonical = field('1500.50', []); + assert.equal( + canonical.hint(), + undefined, + 'a value already in order asks nothing', + ); +});