-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Autocomplete] Prevent shrink animation in uncontrolled Autocomplete when default value is set #44873
Conversation
Netlify deploy previewhttps://deploy-preview-44873--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ZeeshanTamboli this solution makes sense 👍🏼
About the test, have you tried adding a regression test? Animations should be disabled on those, so with the bug the label would be on top of the input.
@DiegoAndai Yes, I tried adding a regression test in the In it('should not perform shrink animation when default value is provided', async () => {
const testcase = await renderFixture(
'/regression-Autocomplete/ShrinkAnimationDefaultValue',
);
await takeScreenshot({
testcase,
route: '/regression-Autocomplete/ShrinkAnimationDefaultValue2',
});
}); The import * as React from 'react';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
const options = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
];
export default function Demo() {
return (
<Autocomplete
options={options}
getOptionLabel={(option) => option.title}
defaultValue={options[1]}
renderInput={(params) => <TextField {...params} label="limitTags" />}
sx={{ width: '500px', m: 4 }}
/>
);
} However, the shrink animation still doesn't trigger for the screenshot. Here's a video of the issue on the Screen-Recording.2.mp4 |
@DiegoAndai This is ready for further review. Just pinging you as a reminder. |
@ZeeshanTamboli, I don't understand. Isn't that what we want to fix? The shrink animation not to trigger? Or is it that it doesn't trigger without the fix? |
@DiegoAndai Yes, the animation doesn't trigger without the fix. I tried this by running the test on the |
@ZeeshanTamboli you're right, I was wrong about how the animation would work. I found a way to test this by spying on Let me know if this makes sense. |
@DiegoAndai Good idea. It makes sense. 👍 |
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 theinputValue
prop. Initially,inputValue
is empty (source) whendefaultValue
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 whendefaultValue
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 managinginputValue
andonInputValueChange
. This avoids reliance on the default state mechanism. CodeSandbox: https://codesandbox.io/p/sandbox/stoic-fire-zz67mz-forked-8k9lmgBut since we already set the HTML input value using
defaultValue
, it makes sense to initializeinputValue
accordingly during the first render to avoid the animation. And it already works for other components likeTextField
andSelect
.Not sure how to add a test case for this.
Demos