Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't edit Text Field in Material-UI

I developed a React App using Material-UI then I tried to create independent Components,

check the below independent components(<PanelDiv/>),

render() {
        return (
            <div className="panelDiv-component" style={{display:this.props.display}}>
                <div className="panel-field-content">
                    <TextField
                        floatingLabelText={this.props.heading}
                        type={this.props.inputType}
                        value={this.props.value}
                    />
                   {/* <input  defaultValue className="form-control"></input>*/}
                </div>
            </div>
        );
    }

I tried to use the component like this,

<PanelDiv
    heading='name'
    inputType="text"
    value={this.state.userData.name}
    display={this.state.display[0]}
/>

But I can't update input field in this way.Also there is no error. How can i solve this? I want to update my input field.

Please check my input filed in the below image :

enter image description here

like image 896
Thilina Sampath Avatar asked Sep 08 '17 14:09

Thilina Sampath


People also ask

How do you customize a TextField in material UI?

const styles = theme => ({ textField: { width: '90%', marginLeft: 'auto', marginRight: 'auto', color: 'white', paddingBottom: 0, marginTop: 0, fontWeight: 500 }, });

How do you make a TextField mandatory in material UI?

We can use TextField component in Material UI to show input textbox. import TextField from "@material-ui/core/TextField"; When using the TextField component, we can pass the required attribute to mark the component as required.

How do I validate a TextField material UI?

To invalidate a TextField in React Material UI, we set the error and helperText props of the TextField to display the error message we want. We set the error prop to make the TextField 's border red when we enter invalid input. And we set the helperText prop to display the error text at the bottom.


2 Answers

Because you are controlling the value of TextField by using value attribute but you are not updating the value by using onChange function, Since value of TextField is not changing so it becomes read only.

Solution:

Specify the onChange function with TextField and update the value inside that, Like this:

<TextField
    floatingLabelText={this.props.heading}
    type={this.props.inputType}
    value={this.props.value}
    onChange={this.props._change}
/>

Inside parent component:

_Change(event, value){
   //update the value here
}


<PanelDiv
    heading='name'
    inputType="text"
    value={this.state.userData.name}
    _change={this._change}
    display={this.state.display[0]}
/>
like image 58
Mayank Shukla Avatar answered Sep 19 '22 20:09

Mayank Shukla


If you pass value as a prop to TextField you can't change that text! On Material-UI official documentation they have used defaultValue="default val" as a prop. So I used defaultValue as a prop! It worked fine for me!

<TextField 
   type="text"
   defaultValue={this.props.val}
   onChange={handleChange} 
  />
like image 34
Adarsh Shete Avatar answered Sep 18 '22 20:09

Adarsh Shete