首页 > 解决方案 > 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
/>

标签: javascriptreactjsmaterial-ui

解决方案


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


推荐阅读