javascript - Material-ui Textfield null value for zero - Stack Overflow

I have a Material-Ui TextField handled with Formik.Input value (string) is converted to number on Inpu

I have a Material-Ui TextField handled with Formik. Input value (string) is converted to number on Input Change.

My problem is that when the value number zero passes, it's considered as a false value and renders an empty string. I want it to get 'number zero' showing in TextField.

If I removes TextField value condition ( value || ' ' ), It will give me a warning message below.

Warning: `value` prop on `input` should not be null. Consider using an empty string to clear the ponent or `undefined` for uncontrolled ponents.

How can I work around with it ?

input.js

const Input = ({
  classes,
  icon,
  styleName,
  field: { name, value, onBlur },
  form: { errors, touched, setFieldValue },
  ...props
}) => {
  const errorMessage = getIn(errors, name);
  const isTouched = getIn(touched, name);

  const change = (e, name, shouldValidate) => {
    e.persist();
    const inputValue = e.target.value;
    let value;

      if (inputValue !== '') {
        value = isNaN(inputValue) ? inputValue : parseInt(inputValue, 10);
      } else {
        value = null;
      }

    return setFieldValue(name, value, shouldValidate);
  };

  return (
    <TextField
      name={name}
      value={value || ''}
      onChange={e => change(e, name, true)}
      onBlur={onBlur}
      {...props}
      className={classes[styleName]}
      helperText={isTouched && errorMessage}
      error={isTouched && Boolean(errorMessage)}
      InputProps={{
        startAdornment: (
          <InputAdornment position="start">
            <Icon
              name={icon}
              width="30"
              height="30"
              viewBox="0 0 30 30"
              fill="none"
            />
          </InputAdornment>
        ),
      }}
    />
  );
};

I have a Material-Ui TextField handled with Formik. Input value (string) is converted to number on Input Change.

My problem is that when the value number zero passes, it's considered as a false value and renders an empty string. I want it to get 'number zero' showing in TextField.

If I removes TextField value condition ( value || ' ' ), It will give me a warning message below.

Warning: `value` prop on `input` should not be null. Consider using an empty string to clear the ponent or `undefined` for uncontrolled ponents.

How can I work around with it ?

input.js

const Input = ({
  classes,
  icon,
  styleName,
  field: { name, value, onBlur },
  form: { errors, touched, setFieldValue },
  ...props
}) => {
  const errorMessage = getIn(errors, name);
  const isTouched = getIn(touched, name);

  const change = (e, name, shouldValidate) => {
    e.persist();
    const inputValue = e.target.value;
    let value;

      if (inputValue !== '') {
        value = isNaN(inputValue) ? inputValue : parseInt(inputValue, 10);
      } else {
        value = null;
      }

    return setFieldValue(name, value, shouldValidate);
  };

  return (
    <TextField
      name={name}
      value={value || ''}
      onChange={e => change(e, name, true)}
      onBlur={onBlur}
      {...props}
      className={classes[styleName]}
      helperText={isTouched && errorMessage}
      error={isTouched && Boolean(errorMessage)}
      InputProps={{
        startAdornment: (
          <InputAdornment position="start">
            <Icon
              name={icon}
              width="30"
              height="30"
              viewBox="0 0 30 30"
              fill="none"
            />
          </InputAdornment>
        ),
      }}
    />
  );
};
Share Improve this question asked Nov 16, 2019 at 4:58 JiaahJiaah 8442 gold badges14 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I experienced a situation like this in some of our projects.

This isn't specific to Material-UI but to react.

To work around this, just set the initial value to an empty string ''.

So far we're fine with setting the value to an empty string since it's the default event.target.value of input type number when it's empty.

See: https://codesandbox.io/s/affectionate-stonebraker-cgct3

The suggested solution did't work for me. Number 0 is falsy. so it renders an empty string. I resolved it with this approach.

const input = value === 0 || value ? value : '';
  return (
    <TextField
      name={name}
      value={input}
      ...
    />
  );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743782706a4506094.html

相关推荐

  • javascript - Material-ui Textfield null value for zero - Stack Overflow

    I have a Material-Ui TextField handled with Formik.Input value (string) is converted to number on Inpu

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信