【问题标题】:Set min/max on TextField type="number"?在 TextField type="number" 上设置最小值/最大值?
【发布时间】:2018-05-27 15:25:38
【问题描述】:

我正在使用 Material-UI v1.0.0-beta23 以及 redux-formredux-form-material-ui。我有一个Field,它是类型号,我想在标签上设置最小值和最大值。我已经尝试了inputProps 和 min/max,但似乎都没有做我想要的。我查看了源代码,并没有看到明显的方法来做到这一点。有没有可能,如果有,怎么做?

这是我的 JSX,展示了我尝试过的东西。

<Field
  name="maxNodeSelectedCount"
  component={TextField}
  label="Max Node Selected Count"
  type="number"
  inputProps={{ min: 0, max: 10 }}
  min={11}
  max={20}
  format={formatOption}
  normalize={normalizeOption}
  {...propertyFieldDefaults}
/>

这是 DOM 的样子:

<input type="number" class="MuiInput-input-346 MuiInput-inputSingleline-349 MuiInput-inputDense-347" value="-3" name="options.maxNodeSelectedCount">

【问题讨论】:

    标签: reactjs material-ui redux-form


    【解决方案1】:

    您可以使用inputProps 将任何属性应用于本机input 元素,包括minmax

    <TextField type="number" inputProps={{ min: 4, max: 10 }} />
    

    编辑:请注意min/max 属性不会阻止用户在TextField 中输入无效值。要限制用户可以键入的内容,您可以通过添加 onclick 处理程序来验证值,如下所示:

    const min = 0;
    const max = 10;
    
    export default function BasicTextFields() {
      const [value, setValue] = useState<number>();
    
      return (
        <div>
          <TextField
            type="number"
            inputProps={{ min, max }}
            value={value}
            onChange={(e) => {
              var value = parseInt(e.target.value, 10);
    
              if (value > max) value = max;
              if (value < min) value = min;
    
              setValue(value);
            }}
          />
        </div>
      );
    }
    

    【讨论】:

    • 我可以在textField中直接写任何数字!!
    • @AitFrihaZaid 更新了我对您的观点的回答。
    【解决方案2】:

    type 放入InputProps

          <Field
            name="number"
            label="Number"
            component={TextField}
            InputProps={{
              inputProps: {
                type: 'number',
                min: 0, max: 25,
              },
            }}
          />
    

    【讨论】:

      【解决方案3】:

      这肯定行得通

      handleMobileNumber = (e) => {
       if (e.target.value.split("").length <= 10) {
        this.setState({
          mobileNumber: e.target.value,
        });
       }
      };
      
      <TextField
        label="Enter Mobile Number"
         variant="outlined"
         value={this.state.mobileNumber}
         onChange={this.handleMobileNumber}
         type="number"
      />
      

      【讨论】:

        【解决方案4】:
        <TextField
                    label="Username"
                    id="outlined-start-adornment"
                    variant="outlined"
                    inputProps={{minlength:5, maxLength: 20}}
                  />
        

        【讨论】:

        • 社区鼓励在代码中添加解释,而不是纯粹基于代码的答案(参见here)。
        • 您好,欢迎来到 SO!虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。请阅读tourHow do I write a good answer?
        【解决方案5】:

        好好利用你的inputprops

        <TextField 
            type="number"
            InputProps={{
                inputProps: { 
                    max: 100, min: 10 
                }
            }}
            label="what ever"
        />
        

        注意 inputprops 中的大小写

        感谢肯·格雷戈里

        【讨论】:

        • } 在 min : 10 后丢失
        • @Karan,对不起,我没听明白,你是什么意思?
        • 天啊,这么简单的事情太复杂了。不过,感谢您提供有效的解决方案。
        • 我很乐意提供帮助
        【解决方案6】:

        其他答案对我不起作用。

        Material UI 有一个用于 3rd 方集成的部分here

        它确实可以只写数字而不是负数。

        import NumberFormat from 'react-number-format';
        
        function NumberFormatCustom(props) {
            const { inputRef, onChange, ...other } = props;
            
             return (
                    <NumberFormat
                        {...other}
                        getInputRef={inputRef}
                        allowNegative={false}
                        onValueChange={(values) => {
                            onChange({
                                target: {
                                    name: props.name,
                                    value: values.value,
                                },
                            });
                        }}
                        isNumericString
                    />
                );
        }
            
        <TextField
            label="react-number-format"
            value={values.numberformat}
            onChange={handleChange}
            name="numberformat"
            id="formatted-numberformat-input"
            InputProps={{
                  inputComponent: NumberFormatCustom,
             }}
        />
        

        【讨论】:

          【解决方案7】:

          redux-form Field 会将 props 传递给组件:

          所有其他 props 都会传递给组件 props 生成的元素。

          TextField 有一个名为 InputProps 的属性,可用于将 props 传递给它呈现的 Input 组件。如果您使用的是redux-form-material-ui,这也是如此。它的 TextField 只是 material-ui 组件的一个包装器。

          material-ui Input 组件有一个名为 inputProps 的属性,可用于将 props 传递给它渲染的 input 元素。

          好的,那我该怎么办?

          您需要从Field,到TextField,到Input,到input元素一直传递道具。

          所以如果我们在 Field 上设置InputProps,它将被传递给 TextField,TextField 会将它传递给 Input 组件。如果我们传递的对象包含一个内部inputProps(故意小写'i'),则输入组件会将其传递给input 元素。

          热播游戏:

          基本上,在InputProps 中定义一个inputProps 对象并将其应用于Field

          <Field
              name="maxNodeSelectedCount"
              component={TextField}
              label="Max Node Selected Count"
              type="number"
              InputProps={{ inputProps: { min: 0, max: 10 } }}
              format={formatOption}
              normalize={normalizeOption}
              {...propertyFieldDefaults}
            />
          
          • 字段将 InputProps 传递给 TextField
          • TextField 将 InputProps 的内容传递给Input 组件
          • Input 将 inputProps 的内容传递给 input 元素

          有一个警告:

          current implementation of TextFieldinputProps 设置自己的值,以便将inputClassName 应用于input 元素。

          通过将自己的 inputProps 值传递给 TextField,您将覆盖 TextField 应用的版本,而 inputClassName 未设置。如果使用inputClassName,则需要将其包含在内部inputProps 对象中。

          编辑:我已提交 issuepull request 以在未来版本中解决此警告。

          【讨论】:

          • 添加这样的 props 也不起作用: props={{ InputProps: { min: 0, max: 10 } }} 现在 TextField 来自 redux-form-material-ui 而不是 material -ui,我不知道这是否重要。
          • 对不起,我已经更新了我的答案。从inputPropsInputProps 的更改是一个重大更改relased with 1.0.0-beta.23
          • 感谢 Ken 的帮助,但我仍然没有运气。那么您认为在 beta 23 中 InputProps 应该可以工作吗?我什至放了一个数据属性,但这些都没有出现在 DOM 中。 InputProps={{ min: 0, max: 10, 'data-action': 'Hello' }} 呈现为 &lt;input type="number" class="MuiInput-input-246 MuiInput-inputSingleline-249 MuiInput-inputDense-247" value="" name="options.maxNodeSelectedCount"&gt;。我是 React 新手,想知道是否有一个在线小提琴/沙箱可以导入我的库以在线显示问题?
          • @MikeSuiter 刚刚更新了答案。我错过了Inputinput 不同的事实! TextField 包装了 Input,这是另一个包装实际 input DOM 元素的 material-ui 组件。
          • @MikeSuiter 很高兴为您提供帮助。我刚刚提交了一个 PR 来解决我提到的问题。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-30
          相关资源
          最近更新 更多