Skip to content

Commit

Permalink
menu-button: fix double-invoking onSelect in StrictMode (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
variadicintegrity committed Feb 17, 2020
1 parent 81f5031 commit 3f5cbd1
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions packages/menu-button/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,13 @@ const MenuItemImpl = forwardRefWithAs<MenuItemImplProps, "div">(
let isSelected = index === selectionIndex;

function select() {
dispatch({
type: CLICK_MENU_ITEM,
payload: { buttonRef, callback: onSelect },
});
/*
* Focus the button first by default when an item is selected. We fire the
* onSelect callback next so the app can manage focus if needed.
*/
focus(buttonRef.current);
onSelect && onSelect();
dispatch({ type: CLICK_MENU_ITEM });
}

function handleClick(event: React.MouseEvent) {
Expand Down Expand Up @@ -915,10 +918,7 @@ interface MenuButtonState {
}

type MenuButtonAction =
| {
type: "CLICK_MENU_ITEM";
payload: { buttonRef: ButtonRef; callback?: () => any };
}
| { type: "CLICK_MENU_ITEM" }
| { type: "CLOSE_MENU"; payload: { buttonRef: ButtonRef } }
| { type: "OPEN_MENU_AT_FIRST_ITEM" }
| {
Expand Down Expand Up @@ -947,12 +947,6 @@ function reducer(
): MenuButtonState {
switch (action.type) {
case CLICK_MENU_ITEM:
/*
* Focus the button first by default when an item is selected. We fire the
* onSelect callback next so the app can manage focus if needed.
*/
focus(action.payload.buttonRef.current);
action.payload.callback && action.payload.callback();
return {
...state,
isOpen: false,
Expand Down

0 comments on commit 3f5cbd1

Please sign in to comment.