reactjs - 退格键不适用于具有 NumberFormat 的 Material ui TextField

标签 reactjs material-ui textfield

我正在尝试使用react-number-format (NumberFormat) 来格式化数值 在 Material ui TextField 中。这是例子 我可以输入新的数字字符,但无法删除任何字符 使用退格键对字符进行编号。 我也无法使用箭头或删除键。 我可以删除任何数字字符的唯一方法是 用鼠标选择这些字符并按 空格键。

虽然不是我的,但以下代码沙箱复制了我得到的内容: https://codesandbox.io/s/material-demo-iny12?file=/demo.tsx

function NumberFormatCustom(props) {
    const { inputRef, onChange, ...other } = props;

    return (
        <NumberFormat
            {...other}
            customInput={TextField}
            getInputRef={inputRef}
            onValueChange={values => {

                onChange({
                    target: {
                        value: values.value,
                    },
                });
            }}
        />
    );
}

<TextField
    margin="dense"
    fullWidth={true}
    value={value}
    onChange={handleNumberChanged}
    variant="outlined"
    autoComplete="off"
    type="number"
    InputProps={{
        inputComponent: NumberFormatCustom,
    }}

最佳答案

实际上有一个控制台警告告诉您问题所在:

Warning: Failed prop type: Invalid prop type of value number supplied to NumberFormat, expected one of ["text","tel","password"]. in NumberFormat (at demo.tsx:14)

如果您从 TextField 中删除无效的 type="number",那么它似乎工作正常。

https://codesandbox.io/s/material-demo-ovx7i

关于reactjs - 退格键不适用于具有 NumberFormat 的 Material ui TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61374680/

相关文章:

ios - xcode 8.2 中显示 'RCTLogLevel' 错误的重新定义

reactjs - 使用 Material-UI 的数组中的多个悬停弹出窗口仅打开一个弹出窗口

javascript - 从父组件调用子组件函数时,React Material-UI 抽屉不工作

swift - UITextField 总是返回 nil

swift - 键盘文本字段是否取决于文本字段约束?

java - 如何使用 KeyEvent 只允许 JTextField 中的数字?

javascript - 如何在 React Native 中添加按钮?

reactjs - 在react-bootstrap中使用bsClass和className有什么区别?

reactjs - 如何正确设置对 React 组件的引用?

reactjs - 如何更改 Material UI 选择边框和标签