Skip to content

[Autocomplete] Fix label shrink issue when renderValue is used with empty array in multiple mode #46047

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,23 +588,29 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
...getItemProps(params),
});

if (renderTags && multiple && value.length > 0) {
startAdornment = renderTags(value, getCustomizedItemProps, ownerState);
if (multiple) {
if (value.length > 0) {
if (renderTags) {
startAdornment = renderTags(value, getCustomizedItemProps, ownerState);
} else if (renderValue) {
startAdornment = renderValue(value, getCustomizedItemProps, ownerState);
} else {
startAdornment = value.map((option, index) => {
const { key, ...customItemProps } = getCustomizedItemProps({ index });
return (
<Chip
key={key}
label={getOptionLabel(option)}
size={size}
{...customItemProps}
{...externalForwardedProps.slotProps.chip}
/>
);
});
}
}
} else if (renderValue && value) {
startAdornment = renderValue(value, getCustomizedItemProps, ownerState);
} else if (multiple && value.length > 0) {
startAdornment = value.map((option, index) => {
const { key, ...customItemProps } = getCustomizedItemProps({ index });
return (
<Chip
key={key}
label={getOptionLabel(option)}
size={size}
{...customItemProps}
{...externalForwardedProps.slotProps.chip}
/>
);
});
}

if (limitTags > -1 && Array.isArray(startAdornment)) {
Expand Down
25 changes: 25 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3598,4 +3598,29 @@ describe('<Autocomplete />', () => {
expect(textbox).toHaveFocus();
});
});

it('should not shrink the input label when value is an empty array in multiple mode using renderValue', () => {
const { getByTestId } = render(
<Autocomplete
multiple
value={[]}
options={['one', 'two', 'three']}
renderValue={(values, getItemProps) =>
values.map((option, index) => {
const { key, ...itemProps } = getItemProps({ index });
return <Chip key={key} label={option.title} {...itemProps} />;
})
}
renderInput={(params) => (
<TextField
{...params}
label="Fixed tag"
slotProps={{ inputLabel: { 'data-testid': 'label' } }}
/>
)}
/>,
);

expect(getByTestId('label')).to.have.attribute('data-shrink', 'false');
});
});
Loading