Skip to content

Commit 8046fdd

Browse files
authored
[core] Fix React 18.3 warnings about spreading keys in the Material UI Autocomplete component (#42099) (#42241)
1 parent c9a020e commit 8046fdd

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

packages/mui-base/src/useAutocomplete/useAutocomplete.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ describe('useAutocomplete', () => {
3131
{groupedOptions.length > 0 ? (
3232
<ul {...getListboxProps()}>
3333
{groupedOptions.map((option, index) => {
34-
return <li {...getOptionProps({ option, index })}>{option}</li>;
34+
const { key, ...optionProps } = getOptionProps({ option, index });
35+
return (
36+
<li key={key} {...optionProps}>
37+
{option}
38+
</li>
39+
);
3540
})}
3641
</ul>
3742
) : null}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,18 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
552552
if (renderTags) {
553553
startAdornment = renderTags(value, getCustomizedTagProps, ownerState);
554554
} else {
555-
startAdornment = value.map((option, index) => (
556-
<Chip
557-
label={getOptionLabel(option)}
558-
size={size}
559-
{...getCustomizedTagProps({ index })}
560-
{...ChipProps}
561-
/>
562-
));
555+
startAdornment = value.map((option, index) => {
556+
const { key, ...customTagProps } = getCustomizedTagProps({ index });
557+
return (
558+
<Chip
559+
key={key}
560+
label={getOptionLabel(option)}
561+
size={size}
562+
{...customTagProps}
563+
{...ChipProps}
564+
/>
565+
);
566+
});
563567
}
564568
}
565569

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,10 @@ describe('<Autocomplete />', () => {
638638
renderTags={(value, getTagProps) =>
639639
value
640640
.filter((x, index) => index === 1)
641-
.map((option, index) => <Chip label={option.title} {...getTagProps({ index })} />)
641+
.map((option, index) => {
642+
const { key, ...tagProps } = getTagProps({ index });
643+
return <Chip key={key} label={option.title} {...tagProps} />;
644+
})
642645
}
643646
onChange={handleChange}
644647
renderInput={(params) => <TextField {...params} autoFocus />}

0 commit comments

Comments
 (0)