-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Material UI - label overlap when using setValue #2192
Comments
It has nothing to do with this lib. |
@xiaoyu-tamu
How come? |
Have you check this limitation? https://material-ui.com/components/text-fields/#limitations Your CSB looks good on my laptop. |
hey @michalbundyra please use Controller for this, because MUI only will move the label when external prop (value) gets updated, it doesn't check whether the input value has been updated. so Controller will solve that problem for you, you can easily use |
@bluebill1049 Thanks for your answer. I am afraid that it does not work, see: https://codesandbox.io/s/react-hook-form-useform-template-kqhqv ... but I see the problem now only on Safari, not on Chrome anymore. In my application I can still see the same problem in Chrome, also when I use Controller. So far I do not have a valid solution for that problem... |
here you go in Safari: https://codesandbox.io/s/react-hook-form-useform-template-czc4r?file=/src/index.js |
@bluebill1049 Thanks! SOLVED. I guess |
please have a read the doc under Controller @michalbundyra. There are some really useful info there: https://react-hook-form.com/api#Controller |
We experience a similar problem on all of our forms on several projects. Here is an example: <TextField label="Name" name={name} inputRef={register} />
<TextField label="Name" defaultValue={'foo-bar-2'} /> and here is the result: As you see, the field's "shrink" state to the left wasn't updated after receiving the value from form data. Does this mean that we should also wrap basic TextField in Controller? |
Yeah, it worked with the Controller: <Controller
as={TextField}
label="Name"
control={control}
name={name}
/> Actually the docs made this confusion. Here: https://react-hook-form.com/get-started#IntegratingwithUIlibraries the first example for the TextField reads: <TextField inputRef={register} name="FirstName"/> which basically doesn't work. Maybe it's worth updating the docs then? |
what's not working? where is the codesandbox? I fork the link above: https://codesandbox.io/s/react-hook-form-useform-template-forked-kmmeo?file=/src/index.js which i have posted. |
As far as I know, Material-UI has internal state to check whether a field is filled or not. The state is available in FormControl, which it passes down to every children with FormControlContext. TextField is one component that uses FormControl. This filled state is used by MUI's InputLabel to determine whether the label should shrink or not. When the field is uncontrolled and the value changed through setting HTMLInputElement.value directly, this changed value is not reflected in the filled state of FormControl. To fix this you'd have to change the filled state by yourself. Here's a demo that shows how to change the filled state based on RHF's form value while keeping the field uncontrolled: The trick is to listen to FormControlContext by becoming its children (through end adornment in InputProps) and set filled state using |
InputLabelProps={{ shrink: true }} |
For me it was the order, label had to come before value |
With Class component `class MyComponent extends Component {
}` |
In my instance the issue was the form element going from As others have said, the way to fix it was to set a |
Describe the bug
I am using material-ui with react-hook-form. I am loading some data in useEffect hook and I'd like to set these to the form.
Unfortunately when I do that, the label is overlapping the value (value is set to the field).
I guess I can use
value
+onChange
but I am not sure if it is the best solution (performance?).To Reproduce
Please see codesandbox link below
Codesandbox link (Required)
https://codesandbox.io/s/react-hook-form-useform-template-5egkh?fontsize=14&hidenavigation=1&theme=dark
Expected behavior
Label is not overlapped with the field value.
Screenshots
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: