简体   繁体   English

如何将 HMTL 属性设置为 Material UI InputProps?

[英]How to set HMTL attribute to Material UI InputProps?

I am using react-number-format with Material UI Textfield and I'm trying to add a max property of 100 on my field.我正在使用带有 Material UI 文本字段的 react-number-format,并且我正在尝试在我的字段中添加 100 的 max 属性。 eg numbers above 100 are not allowed.例如,不允许超过 100 的数字。 How can I do this using HTML attribute: min?如何使用 HTML 属性执行此操作:min?

import InputAdornment from "@material-ui/core/InputAdornment";
import TextField from "@material-ui/core/TextField";
import NumberFormat from "react-number-format";


interface IProps {
  endAdornment?:React.FC;
  error?: string;
  fixedDecimalScale?: boolean;
  fullWidth: boolean;
  label: string;
  numberLimit: number;
  onChange: () => void;
  placeholder: string;
  thousandSeparator: boolean;
  touched?: boolean;
  value: number;
  variant: string;
  inputComponent?: React.FC;
}



  return (
    <NumberFormat
      InputProps={
        props.inputComponent
          ? {
              endAdornment: (
                <InputAdornment position="start">
                  {props.inputComponent}
                </InputAdornment>
              ),
            }
          : null
      }
      inputProps={{ max: 100 }}
      customInput={TextField}
      decimalScale={0}
      error={Boolean(props.error) && props.touched ? true : false}
      fixedDecimalScale={props.fixedDecimalScale}
      fullWidth={props.fullWidth}
      helperText={
        Boolean(props.error) && props.touched ? props.error : undefined
      }
      id={id}
      label={props.label}
      margin="normal"
      onChange={props.onChange}
      placeholder={props.placeholder}
      thousandSeparator={props.thousandSeparator}
      value={props.value}
      variant={props.variant}
    />
  );
};

export default NumberField;

I've tried to add it inside InputProps as well but I can't seem to figure out the right format.我也尝试将它添加到 InputProps 中,但我似乎无法找出正确的格式。 I know NumberFormat comes with prop isAllowed but I'd like to try and set it with an HTML attribute.我知道 NumberFormat 带有道具 isAllowed 但我想尝试使用 HTML 属性对其进行设置。

I've tried to add it inside InputProps我试图将它添加到 InputProps

The correct props to pass attributes to the native input element is i nputProps , not I nputProps , notice the lowercase 'i'.将属性传递给本机input元素的正确道具是i nputProps ,而不是I nputProps ,请注意小写的“i”。

<TextField {{ type: "number", min: 0, max: 10 }} />

Result:结果:

<input type="number" max="10" min="0">

编辑 67112768/how-to-set-hmtl-attribute-to-material-ui-inputprops

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM