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

Conversation

sai6855
Copy link
Contributor

@sai6855 sai6855 commented Oct 4, 2024

closes #43992

before: https://codesandbox.io/p/sandbox/silent-star-dfld6y?from-embed=&workspaceId=e3af0474-6cb1-4121-8a53-58b7b0668e3e

Not able to create sandbox from preview, please copy code from before sandbox and paste it this preview to see the effect.

https://deploy-preview-43994--material-ui.netlify.app/material-ui/react-autocomplete/#combo-box

@sai6855 sai6855 added bug 🐛 Something doesn't work package: material-ui Specific to @mui/material component: autocomplete This is the name of the generic UI component, not the React module! labels Oct 4, 2024
@sai6855 sai6855 marked this pull request as draft October 4, 2024 12:02
@mui-bot
Copy link

mui-bot commented Oct 4, 2024

Netlify deploy preview

https://deploy-preview-43994--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 1d1cd1b

@@ -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

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

The issue is a bit different here. We define the ListboxSlot as slots, but then we don't use it as a slot, but rather as a value inside the as prop. Slots by definition should replace the element that is rendered by default, not just used as an as prop.

The better fix would be to use the AutocompleteListbox on this line instead, at least based on the implementation.

@DiegoAndai we may want to revise the implementation of slots, we need to be consistent into what slots represents (they are replacement for the default parts of the component, they shouldn't behave as an alias to the as/component prop.

@sai6855
Copy link
Contributor Author

sai6855 commented Oct 5, 2024

@mnajdova Using AutocompleteListBox as elementType and using ListBoxSLot as slot instead of value to as prop, seems to do the trick. updated code here a4f4d2c

@sai6855 sai6855 requested a review from mnajdova October 5, 2024 06:07
@mnajdova
Copy link
Member

mnajdova commented Oct 7, 2024

@mnajdova Using AutocompleteListBox as elementType and using ListBoxSLot as slot instead of value to as prop, seems to do the trick. updated code here a4f4d2c

Yeah, that is another solution that may work. Let's hold off the merging tough, as the slots inconsistency implementation is what worries me the most.

@DiegoAndai
Copy link
Member

they are replacement for the default parts of the component, they shouldn't behave as an alias to the as/component prop.

I agree with @mnajdova. This was an oversight in #41875.

@sai6855's latest changes seem to follow this.

However, this is a breaking change for the (deprecated) ListboxComponent prop. Would this fix still work if we kept the original behavior for the ListboxComponent prop? Something like

// line 685
<ListboxSlot as={ListboxComponent} {...listboxProps}>
// ...

(And remove it from externalForwardedProps)

What do you think, @mnajdova? I know this makes the ListboxComponent prop different from the other *Component props, but I think that's okay. It has been like this for a long time, and that prop is now deprecated, so it's better not to change it.

@mnajdova
Copy link
Member

mnajdova commented Oct 9, 2024

What do you think, @mnajdova? I know this makes the ListboxComponent prop different from the other *Component props, but I think that's okay. It has been like this for a long time, and that prop is now deprecated, so it's better not to change it.

Let's keep the deprecation and add a TODO comment to remember to change it in the next major (or just remove the prop if that's the case).

@DiegoAndai
Copy link
Member

@sai6855 let's implement what I described in this comment #43994 (comment)

Let me know if you have any questions about it 😊

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Looks good, sorry for the delay. Let's merge this to fix the regression 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Autocomplete ] Using ownerState is undefined in styleOverrides for Autocomplete on opening the options
5 participants