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

[Autocomplete] Fix when onHighlightChange is called #45438

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented Feb 28, 2025

Fixes #43213

The onHighlightChange prop, introduced in #20691, did not correctly determine when it should be triggered.

This PR:

Notice that the event parameter was undefined, further indicating incorrect handling.

I have also modified the test cases.

Before: https://stackblitz.com/edit/react-d4b5ls?file=Demo.tsx
After: https://codesandbox.io/p/sandbox/material-ui-cra-ts-forked-mpm2ft

@ZeeshanTamboli ZeeshanTamboli 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 Feb 28, 2025
@mui-bot
Copy link

mui-bot commented Feb 28, 2025

Netlify deploy preview

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

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 56de521

@ZeeshanTamboli
Copy link
Member Author

Can I get a review on this?

Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Left some questions

* @param {string} reason Can be: `"keyboard"`, `"auto"`, `"mouse"`, `"touch"`.
* @param {string} reason Can be: `"keyboard"`, `"mouse"`, `"touch"`.
Copy link
Member

Choose a reason for hiding this comment

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

Why is "auto" being removed? Under which scenarios is "auto" the reason?

Copy link
Member Author

@ZeeshanTamboli ZeeshanTamboli Mar 11, 2025

Choose a reason for hiding this comment

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

The auto reason is the default value for setHighlightedIndex under which onHighlightChange is triggered. It was introduced in #20691 here and is used when no keyboard, mouse, or touch interaction occurs, like maybe in the cases noted in the bug report and point 2 of defaultValue in the PR description. With the updated logic, reason should now be limited to keyboard, mouse, or touch interactions.

Edit: I think I can remove default auto reason from code as well if it isn't used anywhere else as a reason.

Edit: Removed in 678b49c

});

describe('prop: onHighlightChange', () => {
it('should trigger event when default value is passed', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Should we inverse this test? i.e. make it validate no event is triggered when the default value is passed.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could. Done.

const { getByRole, setProps } = render(
<Autocomplete
onHighlightChange={(event, option) => {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to validate that onHighlightChange is not being called?

Copy link
Member Author

@ZeeshanTamboli ZeeshanTamboli Mar 11, 2025

Choose a reason for hiding this comment

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

Yes, we can. Done.

Copy link
Member

@aarongarciah aarongarciah 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. Let's wait for another pair of eyes to review this.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[material-ui][Autocomplete] onHighlightChange called when listbox opens without any option being highlighted
3 participants