okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9caf39495
main
1import { useEffect , useState } from 'react' ; 2import { parseDecimal , partialDecimal } from '../lib/decimal' ; 3 4/** A number field that lets the person type a decimal point: it keeps the text while it is typed and tells the page the number once it is one (#3736). */ 5export function DecimalInput ({ value, onChange, ariaLabel, className= 'st-input' , placeholder, disabled} :{ 6value :number | null | undefined ; 7onChange :( value :number | null ) => void ; 8ariaLabel :string ; 9className ?:string ; 10placeholder ?:string ; 11disabled ?:boolean ; 12}) { 13const [ text , setText ] = useState ( value === null || value === undefined ?'' :String ( value )); 14// A change that came from elsewhere (Restore saved, another tab) replaces the text, but never while the typed text already means this number. 15useEffect (() => { 16const shown = text === '' ?null :parseDecimal ( text ); 17if (( value ?? null ) !== shown ) setText ( value === null || value === undefined ?'' :String ( value )); 18}, [ value ]); 19return < input className = { className } aria-label = { ariaLabel } inputMode = "decimal" disabled = { disabled } placeholder = { placeholder } value = { text } 20onChange = { e => { 21const next = e . target . value ; 22if ( ! partialDecimal ( next )) return ; 23setText ( next ); 24if ( next . trim () === '' ) onChange ( null ); 25else { const n = parseDecimal ( next ); if ( n !== null ) onChange ( n ); } 26}} 27onBlur = {() => { const n = text . trim () === '' ?null :parseDecimal ( text ); setText ( n === null ?'' :String ( n )); }}/>; 28}