Skip to content

Conversation

@bcarthic
Copy link
Collaborator

@bcarthic bcarthic commented May 26, 2018

Pull request checklist

Description of changes

With #4741 , the behavior of the context menu is regressed, If you have both onclick with preventdefault and href of the context menu item, href is now obeyed and onclick is never been called. This is because now _onItemClick from ContextualMenuItemWrapper is called instead from props.

With this change, the onclick is passed from props instead of baseclass.

Focus areas to test

(ContextualMenu)

Microsoft Reviewers: Open in CodeFlow

@bcarthic bcarthic changed the title Office ui fabric react/5002 Fix onitem click for contextual menu with anchor item May 27, 2018
index={ 0 }
focusableElementIndex={ 0 }
totalItemCount={ 1 }
onItemClick={ itemOnClick }
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need to set this in the test if it's not going to be used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because the snapshot has it has a function, The actual behavior should be if we have onclick then we should invoke otherwise we shouldnt have any default method.

describe('creates a normal button', () => {
let menuItem: IContextualMenuItem;
let menuClassNames: IMenuItemClassNames;
let itemOnClick: () => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

If you are going to use this function, can you use jest.fn() here instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

aria-disabled={ isItemDisabled(item) }
style={ item.style }
onClick={ this._onItemClick }
onClick={ onItemClick ? onItemClick.bind(this, item) : undefined }
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe that this is the correct place to make this change. The issue is that ContextualMenuAnchor doesn't get an onItemBaseClick passed to it, so the onClick is never called in this._onItemClick. I believe the correct response is change contextualmenuitemwrapper's this._onItemClick to the following.

protected _onItemClick = (ev: React.MouseEvent<HTMLElement>): void => {
    const { item, onItemClickBase, onItemClick } = this.props;
    if (onItemClickBase) {
      onItemClickBase(item, ev, ev.currentTarget as HTMLElement);
    } else if (onItemClick) {
      onItemClick(item, ev);
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we dont pass onItemClickBase to ContextualMenuAnchor, it is best override the onItemClick here as per our suggestion

@bcarthic bcarthic merged commit 141279b into microsoft:master May 29, 2018
Markionium added a commit to Markionium/office-ui-fabric-react that referenced this pull request May 29, 2018
* master:
  ComboBox: Fix styling when navigating menu with single select (microsoft#5017)
  Use require.resolve logic in builds & watch .js files from node_modules (microsoft#5007)
  Fix onitem click for contextual menu with anchor item (microsoft#5003)
  CommandBar example: fix link color on website (microsoft#4975)
@microsoft microsoft locked as resolved and limited conversation to collaborators Aug 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Onclick event not invoked for contextual menu item with href

3 participants