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

[Menu] Add onFocusIndexChange property #5851

Merged

Conversation

gabrieldeal
Copy link

@gabrieldeal gabrieldeal commented Dec 29, 2016

Note

This PR is a follow-up to issue #5790, where I described the use case that I am trying to solve.

Description

Add a callback property that is called whenever the MenuItem focus is changed.

Sometimes it will be called when the focus isn't changed. For example:

  • PR has tests / docs demo, and is linted.
    • No docs demo
  • Commit and PR titles begin with [ComponentName], and are in imperative form: "[Component] Fix leaky abstraction".
  • Description explains the issue / use-case resolved, and auto-closes the related issue(s) (http://tr.im/vFqem).

Add a callback property that is called whenever the MenuItem focus is changed.

Sometimes it will be called when the focus isn't changed.  For example:
* If top menu item is keyboard focused (call mui#1) and then the up arrow is pressed (call mui#2).
* If some menu item is keyboard focused (call mui#1) and then that same menu item is clicked (call mui#2).
@muibot muibot added PR: Needs Review PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI and removed PR: Needs Review labels Dec 29, 2016
@gabrieldeal
Copy link
Author

Hmmm... I used npm run test:karma during development, which works fine on my computer. But npm run test fails on my computer, just like on Travis.

Looking into it.

Unmounting the Menu component in order to cancel the FocusRipple.pulsate() timer.  Before returning, this timer callback always starts a new setTimeout() for itself.  This outstanding timer stopped Node.js from exiting.
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

I have to test it but that looks promising :).

let index = this.state.focusIndex;
const maxIndex = this.getMenuItemCount(filteredChildren) - 1;

index++;
if (index > maxIndex) index = maxIndex;

this.setFocusIndex(index, true);
this.setFocusIndex(index, true, event);
Copy link
Member

Choose a reason for hiding this comment

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

We use the event as the last and first argument. I think that it should always been the first argument.
Could we change that here?


assert(onMenuItemFocusChangeSpy.calledWith(null, 0),
'initial focus should invoke callback with 0');
onMenuItemFocusChangeSpy.reset();
Copy link
Member

Choose a reason for hiding this comment

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

Why not using the beforeEach / afterEach pattern to better isolate the tests?

Copy link
Author

Choose a reason for hiding this comment

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

I am very interested in making sure we don't break what happens when the user enters a particular sequence of arrow key events. That would be difficult for me to test if I isolated each arrow key event inside its own it since I would lose all the Menu state along with the spy state.

Does that make sense? I'm not confident I interpreted your comment correctly.

Put the `event` argument at the front to be consistent with other functions.
Add a spec for selecting a MenuItem via hotkeys. The new spec uncovered two pre-existing bugs.
@gabrieldeal
Copy link
Author

Thanks for your feedback!

I just made your suggested change to the order of the event argument.

I also just added a unit test for some code that I realized I hadn't even tested manually. In the process, I noticed one or two pre-existing bugs in the hotkey code. They are documented in the tests. Do you think I should open issues for them?

@oliviertassinari oliviertassinari added PR: Review Accepted and removed PR: Needs Review PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI labels Jan 3, 2017
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

I noticed one or two pre-existing bugs in the hotkey code. They are documented in the tests. Do you think I should open issues for them?

Sounds like to issues indeed. You can open some issue. But I'm not sure someone will solve them as we are working on the next branch.

@oliviertassinari oliviertassinari merged commit e14bb06 into mui:master Jan 3, 2017
@oliviertassinari
Copy link
Member

Thanks!

@gabrieldeal gabrieldeal deleted the menu-handle-focus-index-change branch January 3, 2017 21:28
@zannager zannager added the component: menu This is the name of the generic UI component, not the React module! label Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: menu This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants