Skip to content
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

[material-ui][Autocomplete] Fix ownerState is undefined in styleOverrides for Autocomplete #43994

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
let autocompletePopper = null;
if (groupedOptions.length > 0) {
autocompletePopper = renderAutocompletePopperChildren(
<AutocompleteListbox as={ListboxSlot} {...listboxProps}>
<AutocompleteListbox as={ListboxSlot} ownerState={ownerState} {...listboxProps}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason for value of ownerState is undefined in styleOverrides is, absence of ownerState key in listBoxprops

useSlot hook doesn't attach ownerState if elementType is an html element , since elementType of ListBoxSlot is 'ul' not a React component (check here ), ownerState was missing in listBoxprops.

if (elementType === undefined || isHostComponent(elementType)) {
return otherProps as AppendOwnerStateReturnType<ElementType, OtherProps, OwnerState>;
}

Attaching ownerState explicity seems to fix the issue

{groupedOptions.map((option, index) => {
if (groupBy) {
return renderGroup({
Expand Down
28 changes: 28 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ describe('<Autocomplete />', () => {
});
});

it('should not throw error when accessing ownerState in styleOverrides', () => {
const theme = createTheme({
components: {
MuiAutocomplete: {
styleOverrides: {
root: ({ ownerState }) => {
return {
outlineColor: ownerState.size === 'small' ? 'magenta' : 'crimson',
};
},
},
},
},
});

expect(() => {
render(
<ThemeProvider theme={theme}>
<Autocomplete
open
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
/>
</ThemeProvider>,
);
}).not.to.throw();
});

describe('combobox', () => {
it('should clear the input when blur', () => {
const { getByRole } = render(
Expand Down