13

I am currently trying to set a custom data attribute to a TextField component as such:

class TestTextField extends React.Component {
componentDidMount() {console.log(this._input)}
  render() {
    return (
      <TextField
        label="Label 1"
        InputProps={{placeholder: 'Input 1', 'data-state': 'Data State 1'}}
        margin="normal"
        inputRef={(elem) => {this._input = elem}}
      />
    )
  }
}

However I can't get data-state to show up with the console log saying

 <textarea rows="1" class="MuiTextarea-textarea-67 MuiInput-input-56 MuiInput-inputMultiline-64" placeholder="Input 1" type="text">

Are custom attributes not supported for TextField? I am using v1.0.0-beta.6 (https://material-ui-1dab0.firebaseapp.com/api/text-field/)

3 Answers 3

20

In case others didn't notice, @pier-paolo-ramon is saying to use the inputProps prop instead of the InputProps prop. Pay close attention to the case sensitivity.

<TextField
  inputProps={{placeholder: 'Input 1', 'data-state': 'Data State 1'}}
/>
Sign up to request clarification or add additional context in comments.

1 Comment

works fine to me
12

As you can see custom props are passed to the FormControl component. What you want is probably inputProps which is then passed down to the actual <input /> (InputComponent is "input" by default)

1 Comment

I was like going crazy when i was not able to view the data attributes in markup, this post made it a piece of cake ..
0

As of MUI 6, text input attributes are set as follows:

      <TextField
        slotProps={{
          htmlInput: { 'data-index': index },
        }} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.