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

[Autocomplete] Prevent shrink animation in uncontrolled Autocomplete when default value is set #44873

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented Dec 28, 2024

Closes #44506

Also related to #36548 (see comment 1 and comment 2).

Problem

The shrink animation on input label is triggered when the HTML input value in the notched outlined input component changes from empty to filled.

In the Autocomplete component, the HTML input value is determined by the inputValue prop. Initially, inputValue is empty (source) when defaultValue prop is provided. The input value is later updated via state (source), causing a re-render and the shrink animation.

Fix

Compute the initial inputValue on the first render when defaultValue is provided. This ensures the input is correctly initialized, preventing the shrink animation on InputLabel.

Alternative that can be considered

Tell developer to use a controlled Autocomplete by managing inputValue and onInputValueChange. This avoids reliance on the default state mechanism. CodeSandbox: https://codesandbox.io/p/sandbox/stoic-fire-zz67mz-forked-8k9lmg

But since we already set the HTML input value using defaultValue, it makes sense to initialize inputValue accordingly during the first render to avoid the animation. And it already works for other components like TextField and Select.

Not sure how to add a test case for this.

Demos

@ZeeshanTamboli ZeeshanTamboli added the component: autocomplete This is the name of the generic UI component, not the React module! label Dec 28, 2024
@mui-bot
Copy link

mui-bot commented Dec 28, 2024

Netlify deploy preview

https://deploy-preview-44873--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against fe990dd

@ZeeshanTamboli ZeeshanTamboli changed the title [Autocomplete] Fix shrink animation in Autocomplete when value is set [WIP][Autocomplete] Fix shrink animation in Autocomplete when value is set Dec 28, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [WIP][Autocomplete] Fix shrink animation in Autocomplete when value is set [Autocomplete] Fix shrink animation in Autocomplete when value is set Jan 2, 2025
@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work package: material-ui Specific to @mui/material labels Jan 2, 2025
@ZeeshanTamboli ZeeshanTamboli marked this pull request as ready for review January 2, 2025 13:38
@ZeeshanTamboli ZeeshanTamboli changed the title [Autocomplete] Fix shrink animation in Autocomplete when value is set [Autocomplete] Prevent shrink animation in Autocomplete when value is set Jan 2, 2025
@ZeeshanTamboli ZeeshanTamboli changed the title [Autocomplete] Prevent shrink animation in Autocomplete when value is set [Autocomplete] Prevent shrink animation in Autocomplete when value is set for uncontrolled Autocomplete Jan 4, 2025
@ZeeshanTamboli ZeeshanTamboli changed the title [Autocomplete] Prevent shrink animation in Autocomplete when value is set for uncontrolled Autocomplete [Autocomplete] Prevent shrink animation in Autocomplete when default value is set for uncontrolled Autocomplete Jan 4, 2025
@ZeeshanTamboli ZeeshanTamboli changed the title [Autocomplete] Prevent shrink animation in Autocomplete when default value is set for uncontrolled Autocomplete [Autocomplete] Prevent shrink animation in uncontrolled Autocomplete when default value is set Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
2 participants