Simulador: um valor guardado como «25.000» antes da correcção pede confirmação
tests / ci (push) Waiting to run

Os rascunhos gravados com o campo numérico antigo podem conter «25.000»,
que o cálculo antigo lia como 25 e que agora se leria como 25 000. O campo
deixa de dizer «Lido como 25 000» sobre um valor que continua guardado de
outra forma: mostra o valor guardado e pede ao investidor que escolha
25 000 ou 25. Nada é alterado sem essa escolha.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
Claude
2026-12-04 23:19:39 +02:00
committed by nala
parent e95cdde5d9
commit 09fc8ec0dd
2 changed files with 73 additions and 2 deletions
+45 -2
View File
@@ -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 (
<>
<input
@@ -65,7 +90,10 @@ export function DecimalInput({
onBlur?.(event);
}}
aria-describedby={
[rest['aria-describedby'], reinterpreted ? hintId : null]
[
rest['aria-describedby'],
reinterpreted || legacyChoices.length ? hintId : null,
]
.filter(Boolean)
.join(' ') || undefined
}
@@ -79,6 +107,21 @@ export function DecimalInput({
onValueChange(emitted.current);
}}
/>
{legacyChoices.length > 0 && (
<span id={hintId} className="mt-1 block text-xs text-amber-900">
Valor guardado «{value}». Confirme o que pretendia:{' '}
{legacyChoices.map((choice) => (
<button
key={choice}
type="button"
className="mr-1 rounded border border-amber-700 px-2 py-0.5"
onClick={() => choose(choice)}
>
{presentDecimal(choice)}
</button>
))}
</span>
)}
{reinterpreted && (
<span id={hintId} className="mt-1 block text-xs text-slate-600">
Lido como {presentDecimal(reinterpreted)}
+28
View File
@@ -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',
);
});