Skip to content

Textfield label is not shrinking when we update the value with empty string using setValue #10671

Closed Answered by leapful
ViCkY-29 asked this question in Q&A
Discussion options

You must be logged in to vote

@ViCkY-29

The root cause is from MUI TextField component because its parent wrapper FormControl component will handle filled or empty value state (the one that shows label animation on UI) by using onChange callback or watch on change of value on TextField component. While setValue from react-hook-form cannot mutate inner locale state of the component, you need to trigger it manually.

Luckily, MUI is using FormControlProvider to manage filled or empty value state via function onFilled or onEmpty, you can access those methods via useFormControl() hook to trigger change UI state manually.

Workaround: https://codesandbox.io/s/react-hook-form-mui-textfield-forked-dhdlh8?file=/src/index.js

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ViCkY-29
Comment options

@leapful
Comment options

leapful Jul 14, 2023
Collaborator

Answer selected by ViCkY-29
@ViCkY-29
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants