数字格式的错误行为
因此,我使用 NumberFormat 库来构建占位符,但它没有按照我想要的方式工作,而且我没有找到原因。输入字段的行为很奇怪。应该充当占位符的“$0”充当字符,因此如果用户键入“120”,则金额实际上将是“1200”。
以下是有关其工作原理的视频: https://media.giphy.com/media/ sptBQiYqQr3B54rJFu/giphy.gif
但是正确的行为是,当存在 0 并且用户输入一个值时, 0 应该被替换(你可以检查谷歌计算器,它就是这样做的)。
<NumberFormat
defaultValue={undefined}
value={payoutInputValue}
displayType="input"
thousandSeparator
decimalScale={2}
prefix={getCurrencySymbol(currency)}
allowLeadingZeros={false}
allowNegative={false}
getInputRef={numberInputRef}
onValueChange={(values) =>
setPayoutInputValue(values?.floatValue || undefined)
}
disabled={disabled || isLoading}
isAllowed={isDrawAmountAllowed(0, currency)}
required
/>
和 setPayoutInputValue 函数:
const [payoutInputValue, setPayoutInputValue] = useState<number>(
payout === 0 && isAmountParamValid ? amountParamParsed : payout
);
我对 React 没有太多经验,所以如果你能用基本术语回答,那就太棒了。谢谢!
编辑:设法让它工作!添加占位符属性和 value={undefined}
So I'm using the NumberFormat library for building a placeholder, but it doesn't work how I wanted it to work, and I don't find the reason. The behavior of the input field is quite strange. The "$0" which should behave as a placeholder behaves as a character, so if the user types "120", the amount will actually be "1200".
Here's a video of how it works: https://media.giphy.com/media/sptBQiYqQr3B54rJFu/giphy.gif
But the correct behaviour would be to, when there is a 0, and the user inputs a value, the 0 should be replaced (you can check the google calculator, it does that).
<NumberFormat
defaultValue={undefined}
value={payoutInputValue}
displayType="input"
thousandSeparator
decimalScale={2}
prefix={getCurrencySymbol(currency)}
allowLeadingZeros={false}
allowNegative={false}
getInputRef={numberInputRef}
onValueChange={(values) =>
setPayoutInputValue(values?.floatValue || undefined)
}
disabled={disabled || isLoading}
isAllowed={isDrawAmountAllowed(0, currency)}
required
/>
and the setPayoutInputValue function:
const [payoutInputValue, setPayoutInputValue] = useState<number>(
payout === 0 && isAmountParamValid ? amountParamParsed : payout
);
I have not much experience with React, so if you could answer with basic terms, that'd be awesome. Thanks!
EDIT: managed to make it work! added placeholder attribute and value={undefined}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试为
NumberFormat
设置hintText
属性,同时给定一个初始值undefined
。Try setting the
hintText
prop forNumberFormat
, also give as an initial valueundefined
.