Simulador: os campos numéricos aceitam «25.000», «1 500» e «1500,50»
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
O campo numérico do navegador segue a língua do navegador: «25.000» era lido como 25 e a vírgula decimal podia ser recusada, pelo que o investidor via o valor mudar ou não conseguia escrever. Os campos de montantes do simulador, do plano de negócio e do orçamento inicial passam a usar um campo de texto decimal que lê os números como se escrevem em Moçambique, mostra «Lido como …» quando reinterpreta e deixa o texto inválido chegar à validação, que o explica. Os campos inteiros (horizonte, prazo de entrega, vida útil, mês económico) deixam de saltar para 1 quando se apaga para reescrever; se o valor escrito não for aceite, ao sair do campo volta o valor em vigor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import {
|
||||
useEffect,
|
||||
useId,
|
||||
useRef,
|
||||
useState,
|
||||
type InputHTMLAttributes,
|
||||
} from 'react';
|
||||
import { normalizeDecimalInput, presentDecimal } from '@/lib/decimal-input';
|
||||
|
||||
type Props = Omit<
|
||||
InputHTMLAttributes<HTMLInputElement>,
|
||||
'type' | 'value' | 'onChange' | 'min' | 'max' | 'step'
|
||||
> & {
|
||||
value: string | number | null | undefined;
|
||||
/** Receives the stored format ("1500.50"), or the raw text when it is not a number, so validation can explain it. */
|
||||
onValueChange: (value: string) => void;
|
||||
min?: string | number;
|
||||
max?: string | number;
|
||||
step?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Number field that reads «25.000», «1 500» and «1500,50» as written in
|
||||
* Mozambique, instead of the browser's number input, which follows the
|
||||
* browser locale (25.000 → 25; a comma may be refused).
|
||||
*/
|
||||
export function DecimalInput({
|
||||
value: rawValue,
|
||||
onValueChange,
|
||||
min: _min,
|
||||
max: _max,
|
||||
step: _step,
|
||||
onBlur,
|
||||
...rest
|
||||
}: Props) {
|
||||
const value =
|
||||
rawValue === null || rawValue === undefined ? '' : String(rawValue);
|
||||
const hintId = useId();
|
||||
const [text, setText] = useState(value);
|
||||
const emitted = useRef(value);
|
||||
useEffect(() => {
|
||||
// A value set elsewhere (draft recovery, another field) replaces the text.
|
||||
if (normalizeDecimalInput(text) !== value) setText(value);
|
||||
emitted.current = value;
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [value]);
|
||||
const read = normalizeDecimalInput(text);
|
||||
const reinterpreted =
|
||||
read !== null && read !== '' && read !== text.trim() ? read : null;
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
{...rest}
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
autoComplete="off"
|
||||
value={text}
|
||||
onBlur={(event) => {
|
||||
// A field the plan did not accept (an emptied horizon, a
|
||||
// fraction of a month) shows again the value still in force.
|
||||
if (emitted.current !== value) {
|
||||
setText(value);
|
||||
emitted.current = value;
|
||||
}
|
||||
onBlur?.(event);
|
||||
}}
|
||||
aria-describedby={
|
||||
[rest['aria-describedby'], reinterpreted ? hintId : null]
|
||||
.filter(Boolean)
|
||||
.join(' ') || undefined
|
||||
}
|
||||
onChange={(event) => {
|
||||
setText(event.target.value);
|
||||
const normalized = normalizeDecimalInput(
|
||||
event.target.value,
|
||||
);
|
||||
emitted.current =
|
||||
normalized === null ? event.target.value : normalized;
|
||||
onValueChange(emitted.current);
|
||||
}}
|
||||
/>
|
||||
{reinterpreted && (
|
||||
<span id={hintId} className="mt-1 block text-xs text-slate-600">
|
||||
Lido como {presentDecimal(reinterpreted)}
|
||||
</span>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user