所以我使用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没有太多经验,所以如果你能用基本的术语回答,那就太棒了。谢谢
编辑:设法做到了!添加占位符属性和值={未定义}
尝试为NumberFormat
设置hintText
道具,同时给出初始值undefined
。
<NumberFormat
hintText="Some placeholder"
value={this.state.card}
customInput={TextField}
format="#### #### #### ####"
/>