Skip to content
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]TextField] label doesn't take its place after clearing value via inputRef (uncontrolled state) #42901

Closed
1 task done
evgeniyworkbel opened this issue Jul 10, 2024 · 16 comments
Assignees
Labels
component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information

Comments

@evgeniyworkbel
Copy link

evgeniyworkbel commented Jul 10, 2024

Search keywords

mui textfield clear ref uncontrolled

Latest version

  • I have tested the latest version

Steps to reproduce

Link to live example: https://codesandbox.io/p/sandbox/cranky-moon-lcxtdf?file=%2Fsrc%2FControlledInput.tsx%3A5%2C52

Steps:

  1. see default textfield ui - label inside input, placeholder is not shown
  2. focus input - see that label shrink off, placeholder is shown
  3. type something to input
  4. click button which clears input via ref
  5. see unexpected behaviour - label didn't back to its place, it is at top of input, placeholder is shown

Current behavior

After clearing value of input, label didn't back to its place, it is at top of input, placeholder is shown

Expected behavior

After clearing value of input, label back to its place(inside of input), placeholder hides

Context

I use uncontrolled state control of text field: i pass defaultValue prop and inputRef.

I use Google Chrome lates version

Your environment

npx @mui/envinfo
 System:
    OS: macOS 14.5
  Binaries:
    Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
    npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 126.0.6478.127
    Edge: Not Found
    Safari: 17.5
  npmPackages:
    @emotion/react: ^11.11.4 => 11.11.4 
    @emotion/styled: ^11.11.5 => 11.11.5 
    @mui/base:  5.0.0-beta.40 
    @mui/core-downloads-tracker:  5.15.21 
    @mui/material: ^5.15.18 => 5.15.21 
    @mui/private-theming:  5.15.20 
    @mui/styled-engine:  5.15.14 
    @mui/system:  5.15.20 
    @mui/types:  7.2.14 
    @mui/utils:  5.15.20 
    @types/react: ^18.2.66 => 18.3.3 
    react: ^18.2.0 => 18.3.1 
    react-dom: ^18.2.0 => 18.3.1 
    typescript: 5.5.2 => 5.5.2 
@evgeniyworkbel evgeniyworkbel added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jul 10, 2024
@zannager zannager added the component: text field This is the name of the generic UI component, not the React module! label Jul 11, 2024
@ZeeshanTamboli
Copy link
Member

@evgeniyworkbel The CodeSandbox link you provided is private. Can you make it public?

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 13, 2024
@evgeniyworkbel
Copy link
Author

@ZeeshanTamboli I had made

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 15, 2024
@ZeeshanTamboli
Copy link
Member

@evgeniyworkbel It is because the TextField component isn't re-rendering when the value is cleared through ref because direct manipulation via ref doesn't trigger a re-render in React. You can manually control the shrink state instead: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. However, it would be better to convert your component to a controlled TextField component to handle value clearing more effectively.

@ZeeshanTamboli ZeeshanTamboli changed the title TextField label doesn't take its place after clearing value via inputRef (uncontrolled state) [material-ui]TextField] label doesn't take its place after clearing value via inputRef (uncontrolled state) Jul 15, 2024
@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 15, 2024
@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to @mui/material label Jul 15, 2024
@evgeniyworkbel
Copy link
Author

@evgeniyworkbel It is because the TextField component isn't re-rendering when the value is cleared through ref because direct manipulation via ref doesn't trigger a re-render in React. You can manually control the shrink state instead: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. However, it would be better to convert your component to a controlled TextField component to handle value clearing more effectively.

The CodeSandbox link you provided is private. Can you make it public?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 16, 2024
@ZeeshanTamboli
Copy link
Member

@evgeniyworkbel It is because the TextField component isn't re-rendering when the value is cleared through ref because direct manipulation via ref doesn't trigger a re-render in React. You can manually control the shrink state instead: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. However, it would be better to convert your component to a controlled TextField component to handle value clearing more effectively.

The CodeSandbox link you provided is private. Can you make it public?

Made it public now.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 16, 2024
@evgeniyworkbel
Copy link
Author

The behaviour in example is weird

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 16, 2024
@ZeeshanTamboli
Copy link
Member

The behaviour in example is weird

What is weird?

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 16, 2024
@evgeniyworkbel
Copy link
Author

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 17, 2024
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Jul 17, 2024

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 17, 2024
@evgeniyworkbel
Copy link
Author

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

Because I need to debounce value inside to update search params

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 17, 2024
@ZeeshanTamboli
Copy link
Member

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

Because I need to debounce value inside to update search params

And you can't do that with a controlled component? Can you show an example? Does the updated CodeSandbox work for you?

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 17, 2024
@evgeniyworkbel
Copy link
Author

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

Because I need to debounce value inside to update search params

And you can't do that with a controlled component? Can you show an example? Does the updated CodeSandbox work for you?

https://codesandbox.io/p/sandbox/cranky-moon-forked-s55hpw?file=%2Fsrc%2FApp.tsx%3A17%2C36

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 18, 2024
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Jul 19, 2024

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

Because I need to debounce value inside to update search params

And you can't do that with a controlled component? Can you show an example? Does the updated CodeSandbox work for you?

https://codesandbox.io/p/sandbox/cranky-moon-forked-s55hpw?file=%2Fsrc%2FApp.tsx%3A17%2C36

You are debouncing the function wrong. debounce from @mui/material returns a callback. See the updated CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-fn2x9x?file=%2Fsrc%2FControlledInput.tsx%3A18%2C17. Also make sure to clear the callback after unmounting with debouncedOnChange.clear().

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 19, 2024
@evgeniyworkbel
Copy link
Author

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

Because I need to debounce value inside to update search params

And you can't do that with a controlled component? Can you show an example? Does the updated CodeSandbox work for you?

https://codesandbox.io/p/sandbox/cranky-moon-forked-s55hpw?file=%2Fsrc%2FApp.tsx%3A17%2C36

You are debouncing the function wrong. debounce from @mui/material returns a callback. See the updated CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-fn2x9x?file=%2Fsrc%2FControlledInput.tsx%3A18%2C17. Also make sure to clear the callback after unmounting with debouncedOnChange.clear().

it makes sense but the problem remains: controlled input is debounced badly - user wants to type fast and controlled input don't allow this

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 19, 2024
@ZeeshanTamboli
Copy link
Member

The behaviour in example is weird

What is weird?

Label is not shrinked when is typing

I updated the CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-8wkr3q. As asked before, why don't you convert your component to controlled component to handle such cases seamlessly.

Because I need to debounce value inside to update search params

And you can't do that with a controlled component? Can you show an example? Does the updated CodeSandbox work for you?

https://codesandbox.io/p/sandbox/cranky-moon-forked-s55hpw?file=%2Fsrc%2FApp.tsx%3A17%2C36

You are debouncing the function wrong. debounce from @mui/material returns a callback. See the updated CodeSandbox: https://codesandbox.io/p/sandbox/cranky-moon-forked-fn2x9x?file=%2Fsrc%2FControlledInput.tsx%3A18%2C17. Also make sure to clear the callback after unmounting with debouncedOnChange.clear().

it makes sense but the problem remains: controlled input is debounced badly - user wants to type fast and controlled input don't allow this

You can sync the value with local state: https://codesandbox.io/p/sandbox/cranky-moon-forked-fn2x9x?file=%2Fsrc%2FControlledInput.tsx%3A15%2C31. I suggest asking such questions on StackOverflow.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 19, 2024
Copy link

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

4 participants