Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Fix passing onclick from props for contextual anchor item",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ describe('ContextualMenuButton', () => {
describe('creates a normal button', () => {
let menuItem: IContextualMenuItem;
let menuClassNames: IMenuItemClassNames;
let itemOnClick: jest.Mock;

beforeEach(() => {
menuItem = { key: '123' };
menuClassNames = getMenuItemClassNames();
itemOnClick = jest.fn();
});

it('renders the contextual menu split button correctly', () => {
Expand All @@ -22,6 +24,7 @@ describe('ContextualMenuButton', () => {
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.

/>);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class ContextualMenuAnchor extends ContextualMenuItemWrapper {
aria-setsize={ totalItemCount }
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

onMouseEnter={ this._onItemMouseEnter }
onMouseLeave={ this._onItemMouseLeave }
onKeyDown={ itemHasSubmenu ? this._onItemKeyDown : null }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export class ContextualMenuBasicExample extends React.Component {
href: 'http://bing.com',
target: '_blank'
},
{
key: 'linkWithOnClick',
name: 'Link click',
href: 'http://bing.com',
onClick: (ev: React.MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => {
alert('Link clicked');
ev.preventDefault();
},
target: '_blank'
},
{
key: 'disabled',
name: 'Disabled item',
Expand Down