Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Material-UI TextField with preloaded value does not move the label (label overlaps with the TextField content)

I have Material-UI TextField:

<TextField id="gen_ref_num" className="col-6 form-control-sm" label="Number" variant="filled" value={this.props.invoice.gen_ref_num} onChange={this.handleChange}/>

And this.props.invoice.gen_ref_num has the value already preloaded. Unfortunately, the label overlaps with the content. When I am start editing the field, then the label moves upside and sits in the right position (no overlap anymore). How to ask Material-UI TextField to respect the pre-loaded values and to move label upwards?

like image 769
TomR Avatar asked Sep 15 '25 09:09

TomR


2 Answers

The following code is the answer:

InputLabelProps={{ shrink: !!this.state.value }}

When adapted to the code in the question, the code ir:

InputLabelProps={{ shrink: !!this.props.invoice.gen_ref_num }} 

More is written about this at https://github.com/mui-org/material-ui/issues/13013

like image 111
TomR Avatar answered Sep 17 '25 02:09

TomR


Wrap with form fixed this issue for me. It should be like this:

<form autoComplete="off">
    <TextField ... />
</form>
like image 34
TamTam Avatar answered Sep 17 '25 04:09

TamTam