Skip to content

Commit e5cfabe

Browse files
[core] Fix a few more key spread issues (#42168)
1 parent f3b9172 commit e5cfabe

File tree

7 files changed

+43
-28
lines changed

7 files changed

+43
-28
lines changed

docs/data/material/components/autocomplete/CustomizedHook.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,24 @@ export default function CustomizedHook() {
181181
<div {...getRootProps()}>
182182
<Label {...getInputLabelProps()}>Customized hook</Label>
183183
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
184-
{value.map((option, index) => (
185-
<StyledTag label={option.title} {...getTagProps({ index })} />
186-
))}
184+
{value.map((option, index) => {
185+
const { key, ...tagProps } = getTagProps({ index });
186+
return <StyledTag key={key} {...tagProps} label={option.title} />;
187+
})}
187188
<input {...getInputProps()} />
188189
</InputWrapper>
189190
</div>
190191
{groupedOptions.length > 0 ? (
191192
<Listbox {...getListboxProps()}>
192-
{groupedOptions.map((option, index) => (
193-
<li {...getOptionProps({ option, index })}>
194-
<span>{option.title}</span>
195-
<CheckIcon fontSize="small" />
196-
</li>
197-
))}
193+
{groupedOptions.map((option, index) => {
194+
const { key, ...optionProps } = getOptionProps({ option, index });
195+
return (
196+
<li key={key} {...optionProps}>
197+
<span>{option.title}</span>
198+
<CheckIcon fontSize="small" />
199+
</li>
200+
);
201+
})}
198202
</Listbox>
199203
) : null}
200204
</Root>

docs/data/material/components/autocomplete/CustomizedHook.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,24 @@ export default function CustomizedHook() {
179179
<div {...getRootProps()}>
180180
<Label {...getInputLabelProps()}>Customized hook</Label>
181181
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
182-
{value.map((option: FilmOptionType, index: number) => (
183-
<StyledTag label={option.title} {...getTagProps({ index })} />
184-
))}
182+
{value.map((option: FilmOptionType, index: number) => {
183+
const { key, ...tagProps } = getTagProps({ index });
184+
return <StyledTag key={key} {...tagProps} label={option.title} />;
185+
})}
185186
<input {...getInputProps()} />
186187
</InputWrapper>
187188
</div>
188189
{groupedOptions.length > 0 ? (
189190
<Listbox {...getListboxProps()}>
190-
{(groupedOptions as typeof top100Films).map((option, index) => (
191-
<li {...getOptionProps({ option, index })}>
192-
<span>{option.title}</span>
193-
<CheckIcon fontSize="small" />
194-
</li>
195-
))}
191+
{(groupedOptions as typeof top100Films).map((option, index) => {
192+
const { key, ...optionProps } = getOptionProps({ option, index });
193+
return (
194+
<li key={key} {...optionProps}>
195+
<span>{option.title}</span>
196+
<CheckIcon fontSize="small" />
197+
</li>
198+
);
199+
})}
196200
</Listbox>
197201
) : null}
198202
</Root>

docs/data/material/components/autocomplete/Tags.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ export default function Tags() {
4444
defaultValue={[top100Films[13].title]}
4545
freeSolo
4646
renderTags={(value, getTagProps) =>
47-
value.map((option, index) => (
48-
<Chip variant="outlined" label={option} {...getTagProps({ index })} />
49-
))
47+
value.map((option, index) => {
48+
const { key, ...tagProps } = getTagProps({ index });
49+
return (
50+
<Chip variant="outlined" label={option} key={key} {...tagProps} />
51+
);
52+
})
5053
}
5154
renderInput={(params) => (
5255
<TextField

docs/data/material/components/autocomplete/Tags.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ export default function Tags() {
4444
defaultValue={[top100Films[13].title]}
4545
freeSolo
4646
renderTags={(value: readonly string[], getTagProps) =>
47-
value.map((option: string, index: number) => (
48-
<Chip variant="outlined" label={option} {...getTagProps({ index })} />
49-
))
47+
value.map((option: string, index: number) => {
48+
const { key, ...tagProps } = getTagProps({ index });
49+
return (
50+
<Chip variant="outlined" label={option} key={key} {...tagProps} />
51+
);
52+
})
5053
}
5154
renderInput={(params) => (
5255
<TextField

docs/pages/base-ui/api/use-autocomplete.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@
197197
},
198198
"getOptionProps": {
199199
"type": {
200-
"name": "(renderedOption: UseAutocompleteRenderedOption&lt;Value&gt;) =&gt; React.HTMLAttributes&lt;HTMLLIElement&gt;",
201-
"description": "(renderedOption: UseAutocompleteRenderedOption&lt;Value&gt;) =&gt; React.HTMLAttributes&lt;HTMLLIElement&gt;"
200+
"name": "(renderedOption: UseAutocompleteRenderedOption&lt;Value&gt;) =&gt; React.HTMLAttributes&lt;HTMLLIElement&gt; &amp; { key: any }",
201+
"description": "(renderedOption: UseAutocompleteRenderedOption&lt;Value&gt;) =&gt; React.HTMLAttributes&lt;HTMLLIElement&gt; &amp; { key: any }"
202202
},
203203
"required": true
204204
},

packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export interface UseAutocompleteReturnValue<
411411
*/
412412
getOptionProps: (
413413
renderedOption: UseAutocompleteRenderedOption<Value>,
414-
) => React.HTMLAttributes<HTMLLIElement>;
414+
) => React.HTMLAttributes<HTMLLIElement> & { key: any };
415415
/**
416416
* Id for the Autocomplete.
417417
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,9 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
640640
const renderGroup = renderGroupProp || defaultRenderGroup;
641641
const defaultRenderOption = (props2, option) => {
642642
// Need to clearly apply key because of https://github.com/vercel/next.js/issues/55642
643+
const { key, ...otherProps } = props2;
643644
return (
644-
<li {...props2} key={props2.key}>
645+
<li key={key} {...otherProps}>
645646
{getOptionLabel(option)}
646647
</li>
647648
);

0 commit comments

Comments
 (0)