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 all 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
9 changes: 5 additions & 4 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {

const externalForwardedProps = {
slots: {
listbox: ListboxComponentProp,
paper: PaperComponentProp,
popper: PopperComponentProp,
...slots,
Expand All @@ -558,7 +557,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
};

const [ListboxSlot, listboxProps] = useSlot('listbox', {
elementType: 'ul',
elementType: AutocompleteListbox,
externalForwardedProps,
ownerState,
className: classes.listbox,
Expand Down Expand Up @@ -682,7 +681,9 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
let autocompletePopper = null;
if (groupedOptions.length > 0) {
autocompletePopper = renderAutocompletePopperChildren(
<AutocompleteListbox as={ListboxSlot} {...listboxProps}>
// TODO v7: remove `as` prop and move ListboxComponentProp to externalForwardedProps or remove ListboxComponentProp entirely
// https://github.com/mui/material-ui/pull/43994#issuecomment-2401945800
<ListboxSlot as={ListboxComponentProp} {...listboxProps}>
{groupedOptions.map((option, index) => {
if (groupBy) {
return renderGroup({
Expand All @@ -695,7 +696,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
}
return renderListOption(option, index);
})}
</AutocompleteListbox>,
</ListboxSlot>,
);
} else if (loading && groupedOptions.length === 0) {
autocompletePopper = renderAutocompletePopperChildren(
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
Loading