Skip to content

Commit

Permalink
[core] Fix React 18.3 warnings about spreading keys in the Material U…
Browse files Browse the repository at this point in the history
…I `Autocomplete` component (#42099) (#42241)
  • Loading branch information
DiegoAndai authored May 23, 2024
1 parent c9a020e commit 8046fdd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ describe('useAutocomplete', () => {
{groupedOptions.length > 0 ? (
<ul {...getListboxProps()}>
{groupedOptions.map((option, index) => {
return <li {...getOptionProps({ option, index })}>{option}</li>;
const { key, ...optionProps } = getOptionProps({ option, index });
return (
<li key={key} {...optionProps}>
{option}
</li>
);
})}
</ul>
) : null}
Expand Down
20 changes: 12 additions & 8 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,18 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
if (renderTags) {
startAdornment = renderTags(value, getCustomizedTagProps, ownerState);
} else {
startAdornment = value.map((option, index) => (
<Chip
label={getOptionLabel(option)}
size={size}
{...getCustomizedTagProps({ index })}
{...ChipProps}
/>
));
startAdornment = value.map((option, index) => {
const { key, ...customTagProps } = getCustomizedTagProps({ index });
return (
<Chip
key={key}
label={getOptionLabel(option)}
size={size}
{...customTagProps}
{...ChipProps}
/>
);
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,10 @@ describe('<Autocomplete />', () => {
renderTags={(value, getTagProps) =>
value
.filter((x, index) => index === 1)
.map((option, index) => <Chip label={option.title} {...getTagProps({ index })} />)
.map((option, index) => {
const { key, ...tagProps } = getTagProps({ index });
return <Chip key={key} label={option.title} {...tagProps} />;
})
}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
Expand Down

0 comments on commit 8046fdd

Please sign in to comment.