7

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
/>
vidy
  • 1,572
  • 6
  • 24
  • 43

3 Answers3

9

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

Nafis
  • 1,020
  • 17
  • 36
1

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.

0

You need to change the Textfield attributes to

<TextField
 placeholder="Enter Your Product Name"
 label="Product Name"
 onChange={handleChange('name')}
 errorText={values.nameError}
 value={values.name}
 fullWidth
/>
will92
  • 956
  • 7
  • 12