question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Other InputProps are not applied to InputProps.inputComponent

See original GitHub issue
  • This is a v1.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

        <TextField
          className={classes.formControl}
          label="react-number-format"
          value={numberformat}
          onChange={this.handleChange("numberformat")}
          id="formatted-numberformat-input"
          InputProps={{
            inputComponent: NumberFormatCustom,
            testProp: "yo"
          }}
        />

The testProp should be passed along with other input properties when instantiating NumberFormatCustom: <NumberFormatCustom testProp="yo" ... />.

Current Behavior

The testProp is not passed.

Steps to Reproduce

Code to reproduce the issue: https://codesandbox.io/s/5z8xqoozzn

/* eslint-disable react/prefer-stateless-function */

import React from "react";
import NumberFormat from "react-number-format";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";

const styles = theme => ({
  container: {
    display: "flex",
    flexWrap: "wrap"
  },
  formControl: {
    margin: theme.spacing.unit
  }
});

function NumberFormatCustom(props) {
  const { inputRef, onChange, testProp, ...other } = props;
  // THIS SHOULD BE "YO" but is undefined instead
  console.log("testProp", testProp);
  return (
    <NumberFormat
      {...other}
      ref={inputRef}
      onValueChange={values => {
        onChange({
          target: {
            value: values.value
          }
        });
      }}
      thousandSeparator
      prefix="$"
    />
  );
}

NumberFormatCustom.propTypes = {
  inputRef: PropTypes.func.isRequired,
  onChange: PropTypes.func.isRequired
};

class FormattedInputs extends React.Component {
  state = {
    textmask: "(1  )    -    ",
    numberformat: "1320"
  };

  handleChange = name => event => {
    this.setState({
      [name]: event.target.value
    });
  };

  render() {
    const { classes } = this.props;
    const { textmask, numberformat } = this.state;

    return (
      <div className={classes.container}>
        <TextField
          className={classes.formControl}
          label="react-number-format"
          value={numberformat}
          onChange={this.handleChange("numberformat")}
          id="formatted-numberformat-input"
          InputProps={{
            inputComponent: NumberFormatCustom,
            testProp: "yo"
          }}
        />
      </div>
    );
  }
}

FormattedInputs.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(FormattedInputs);

Context

There seems to be some discussion about TextField having too complex an API and targetting only 80% of the use cases, e.g. #9326. IMHO, the behavior looks like a bug (or I’m doing something wrong, because after reading the code the InputProps seem to be passed correctly.

Your Environment

Tech Version
Material-UI v1.4.2
React 16.4.1
browser N/A

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
charlaxcommented, Sep 23, 2021

My bad, that’s indeed the case, the proper way to do this is:

inputComponent: (props) => <NumberFormatCustom testProp="yo" {...props} />,
8reactions
fuadelgadicommented, Sep 23, 2021

Please try this ,it worked for me

     <TextField
       InputProps={{
          inputComponent: NumberFormatCustom,
          inputProps: { testProp: "yo"},   
        }}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Other InputProps are not applied to InputProps.inputComponent
This is a v1.x issue. I have searched the issues of this repository and believe that this is not a duplicate. Expected Behavior....
Read more >
React - Material UI - TextField controlled input with custom ...
The problem is that you are defining inline the component type for the inputComponent prop. This means that with each re-render it will...
Read more >
Input Components - React-admin - Marmelab
An Input component displays an input, or a dropdown list, a list of radio buttons, etc. Such components allow to update a record...
Read more >
Select API - Material UI - MUI
Name Type Default autoWidth bool false children node classes object
Read more >
<input>: The Input (Form Input) element - HTML
The HTML element is used to create interactive controls for ... If this attribute is not specified, the default type adopted is text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found