-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
base: master
Are you sure you want to change the base?
[Autocomplete] Fix when onHighlightChange
is called
#45438
Conversation
…d-when-listbox-opens
Netlify deploy previewhttps://deploy-preview-45438--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
Can I get a review on this? |
There was a problem hiding this 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"`. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can. Done.
There was a problem hiding this 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.
Fixes #43213
The
onHighlightChange
prop, introduced in #20691, did not correctly determine when it should be triggered.This PR:
onHighlightChange
was incorrectly triggered when the listbox opened.defaultValue
: When adefaultValue
is provided, the highlighted option remains the same when opening the listbox, soonHighlightChange
should not be called. The existing test case for this behavior has been removed, as it does not align with the expected logic. Example: https://stackblitz.com/edit/stackblitz-starters-bocrcrmconHighlightChange
when there is a mouse, keyboard or touch interaction over the options.Notice that the
event
parameter wasundefined
, 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