Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material ui Textfield Hinttext overlap with with text when text is set with setState in react

I am trying to autofill textfield using setState when user click on edit button. Text is set but default hintText and floatingLabelText overlap with text. When i click inside textfield lable go up but hintText overlap with text. How can i solve this?

this is textfield overlap image.

enter image description here

this is button

<button type="button" className="btn btn-primary btn-sm" id="edit"
onClick={this.editProduct.bind(this, product)} value="edit">Edit</button>

when user click on edit button editProduct function setState is set like this

editProduct = product => {
    this.setState({ 
        name: product.name,
        brand: product.brand,
        description: product.description, 
     });   
}

render() {
const {  name, brand, description } = this.state;
const values = { name, brand, description };

return ( 
    <div class="container">   
        <Addproduct
        handleChange={this.handleChange}
        values={values}
        />
  )
}

this is textfield inside Addproduct component

<TextField
hintText="Enter Your Product Name"
floatingLabelText="Product Name"
onChange={handleChange('name')}
errorText={values.nameError}
defaultValue={values.name}
fullWidth
/>
like image 923
vidy Avatar asked Mar 07 '19 13:03

vidy


2 Answers

You can check against the value and put '' empty string if no input there like this answer: React Material UI Label Overlaps with Text

<TextField
   hintText="Enter Your Product Name"
   floatingLabelText="Product Name"
   onChange={handleChange('name')}
   errorText={values.nameError}
   defaultValue={values.name}
   value={values.name || ''}
   fullWidth
/>

If you don't need defaultValue just remove it

like image 172
Nafis Avatar answered Oct 13 '22 03:10

Nafis


I faced the same issue, but when I changed my functional component to class component it worked. Not sure what was the reason but it worked. I'm still looking for the reason, I'll update in this thread once I find. But you can give it a try and check if that works.

like image 33
Bhavesh Suvalaka Avatar answered Oct 13 '22 04:10

Bhavesh Suvalaka