数字格式的错误行为

发布于 2025-01-11 09:17:21 字数 1358 浏览 3 评论 0原文

因此,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-01-18 09:17:21

尝试为 NumberFormat 设置 hintText 属性,同时给定一个初始值 undefined

<NumberFormat
  hintText="Some placeholder"
  value={this.state.card}
  customInput={TextField}
  format="#### #### #### ####"
/>

Try setting the hintText prop for NumberFormat, also give as an initial value undefined.

<NumberFormat
  hintText="Some placeholder"
  value={this.state.card}
  customInput={TextField}
  format="#### #### #### ####"
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文