dczii
dczii

Reputation: 589

ReactJS - How can I set a value for textfield from material-ui?

I have a selectField and I want to set a value on it. Let say I type on it and when I click a button, the button will call a function that will reset the value of the textfield?

<TextField hintText="Enter Name" floatingLabelText="Client Name" autoWidth={1} ref='name'/>

Upvotes: 29

Views: 76834

Answers (6)

Machi
Machi

Reputation: 435

I would suggest to do a little wrap on the original MUI TextField.

export default function ValueTextField(props) {
  const [value, setValue] = useState(props.value);
  return (
    <TextField {...props} onChange={(event) => setValue(event.target.value)} value={value}/>
  );
}

Now you can use your own ValueTextField component now.

        <ValueTextField value={"hello world"}></ValueTextField>

Upvotes: 1

amirhe
amirhe

Reputation: 2341

Here is a short simple React Hook version of the answer

export default function MyCustomeField({
  initialValue= '', 
  placeholder= 'Enter your text...'
}) {
  const [value, setValue] = React.useState(initialValue)

  return (
    <div>
      <TextField
        placeholder={placeholder}
        value={value}
        onChange={e => setValue(e.target.value)}
      />
      <button onClick={() => setValue(initialValue)}>Reset Text</button>
    </div>
  );
}

Upvotes: 5

Kourosh
Kourosh

Reputation: 1074

I prefer this way to assign the state variables:

<TextField value={mobileNumber} 
     onChange={e => { this.setState({ mobileNumber: e.target.value }) }}
     className={classes.root}
     fullWidth={true}
     label={t('mobile number')} />

Upvotes: 0

Daniel Passos
Daniel Passos

Reputation: 1407

Instead of using ref you should use inputRef

const MyComponent = () => {
  let input;

  return (
    <form
      onSubmit={e => {
        e.preventDefault();
        console.log(input.value);
      }}>
      <TextField
        hintText="Enter Name" 
        floatingLabelText="Client Name" 
        autoWidth={1}
        inputRef={node => {
          input = node;
        }}/>
    </form>
  )
};

Upvotes: 6

James
James

Reputation: 5747

It's maintained that the right way is to have the component be controlled in a scenario like the accepted answer there, but you can also control the value in this gross and culturally unacceptable way.

<TextField ref='name'/>

this.refs.name.getInputNode().value = 'some value, hooray'

and you can of course retrieve the value like so

this.refs.name.getValue()

Upvotes: 6

WitVault
WitVault

Reputation: 24130

You can do it in this way

export default class MyCustomeField extends React.Component {

      constructor(props) {
        super(props);

        this.state = {
          value: 'Enter text',
        };
      }

      handleChange = (event) => {
        this.setState({
          value: event.target.value,
        });
      };

     handleClick = () => {
        this.setState({
          value:'',
        });
      };

      render() {
        return (
          <div>
            <TextField
              value={this.state.value}
              onChange={this.handleChange}
            />
            <button onClick={this.handleClick}>Reset Text</button>
          </div>
        );
      }
    }

Upvotes: 46

Related Questions