Skip to content

Commit 2043d80

Browse files
[Autocomplete] Fix label shrink issue when renderValue is used with empty array in multiple mode (#46047)
1 parent d8cf363 commit 2043d80

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

packages/mui-material/src/Autocomplete/Autocomplete.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -588,23 +588,29 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
588588
...getItemProps(params),
589589
});
590590

591-
if (renderTags && multiple && value.length > 0) {
592-
startAdornment = renderTags(value, getCustomizedItemProps, ownerState);
591+
if (multiple) {
592+
if (value.length > 0) {
593+
if (renderTags) {
594+
startAdornment = renderTags(value, getCustomizedItemProps, ownerState);
595+
} else if (renderValue) {
596+
startAdornment = renderValue(value, getCustomizedItemProps, ownerState);
597+
} else {
598+
startAdornment = value.map((option, index) => {
599+
const { key, ...customItemProps } = getCustomizedItemProps({ index });
600+
return (
601+
<Chip
602+
key={key}
603+
label={getOptionLabel(option)}
604+
size={size}
605+
{...customItemProps}
606+
{...externalForwardedProps.slotProps.chip}
607+
/>
608+
);
609+
});
610+
}
611+
}
593612
} else if (renderValue && value != null) {
594613
startAdornment = renderValue(value, getCustomizedItemProps, ownerState);
595-
} else if (multiple && value.length > 0) {
596-
startAdornment = value.map((option, index) => {
597-
const { key, ...customItemProps } = getCustomizedItemProps({ index });
598-
return (
599-
<Chip
600-
key={key}
601-
label={getOptionLabel(option)}
602-
size={size}
603-
{...customItemProps}
604-
{...externalForwardedProps.slotProps.chip}
605-
/>
606-
);
607-
});
608614
}
609615

610616
if (limitTags > -1 && Array.isArray(startAdornment)) {

packages/mui-material/src/Autocomplete/Autocomplete.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,4 +3614,29 @@ describe('<Autocomplete />', () => {
36143614
expect(container.querySelector(`.${chipClasses.root}`)).to.have.text('0');
36153615
});
36163616
});
3617+
3618+
it('should not shrink the input label when value is an empty array in multiple mode using renderValue', () => {
3619+
const { getByTestId } = render(
3620+
<Autocomplete
3621+
multiple
3622+
value={[]}
3623+
options={['one', 'two', 'three']}
3624+
renderValue={(values, getItemProps) =>
3625+
values.map((option, index) => {
3626+
const { key, ...itemProps } = getItemProps({ index });
3627+
return <Chip key={key} label={option.title} {...itemProps} />;
3628+
})
3629+
}
3630+
renderInput={(params) => (
3631+
<TextField
3632+
{...params}
3633+
label="Fixed tag"
3634+
slotProps={{ inputLabel: { 'data-testid': 'label' } }}
3635+
/>
3636+
)}
3637+
/>,
3638+
);
3639+
3640+
expect(getByTestId('label')).to.have.attribute('data-shrink', 'false');
3641+
});
36173642
});

0 commit comments

Comments
 (0)