Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other InputProps are not applied to InputProps.inputComponent #12365

Closed
2 tasks done
charlax opened this issue Aug 1, 2018 · 7 comments
Closed
2 tasks done

Other InputProps are not applied to InputProps.inputComponent #12365

charlax opened this issue Aug 1, 2018 · 7 comments
Labels
component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@charlax
Copy link
Contributor

charlax commented Aug 1, 2018

  • 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
@charlax
Copy link
Contributor Author

charlax commented Aug 1, 2018

Actually, re-reading https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Input/Input.js#L462-L469, it does look like only inputProps (the props meant to be applied to the DOM input element) are passed.

@charlax

This comment has been minimized.

@germansokolov13

This comment has been minimized.

@IT-Suitapest

This comment has been minimized.

@mickdewald
Copy link

@IT-Suitapest can you give an example how to regain the focus? Nothing I tried worked for me.

@oliviertassinari

This comment has been minimized.

@fuadelgadi
Copy link

fuadelgadi commented Sep 15, 2021

Please try this ,it worked for me

     <TextField
       InputProps={{
          inputComponent: NumberFormatCustom,
          inputProps: { testProp: "yo"},   
        }}

@oliviertassinari oliviertassinari added component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement labels Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

6 participants