reactjs - 在 react-final-form 中更新输入模糊的值
问题描述
我正在react-number-format
使用react-final-form
. 我的组件如下所示:
const CurrencyInput = props => {
return (
<NumberFormat
thousandSeparator=" "
decimalScale="2"
isNumericString={true}
fixedDecimalScale={true}
allowNegative={false}
autoComplete="off"
onBlur={props.input.onBlur}
onFocus={props.input.onFocus}
onChange={value => props.input.onChange(value)}
/>
);
};
react-number-format
有allowLeadingZeros
prop,它在输入模糊时去除前导零。如何react-final-form
相应地更新值?如果我在数字前输入零,则输入本身的值会在模糊时得到纠正,但存储的值react-final-form
保持为零。
这是我的代码框。
解决方案
react-number-format
格式化字符串后不调用 onChange。
但是react-number-format
有一个 proponValueChange
在格式化字符串后被触发:
示例: https ://codesandbox.io/s/react-final-form-wreact-number-format-bmeg9
除此之外,我建议将值存储为状态中的数字而不是字符串。