Skip to content

Commit

Permalink
Make keyboard events work with Tooltip when clickToOpen is true, pr…
Browse files Browse the repository at this point in the history
…event clicks and keyboard events bubbling up through the dom (#1384)

* Moved onClick/onKeyDown handlers to correct element (previously only the click handler was firing). Added stopPropagation to prevent the events bubbling up (e.g. when a tooltip is inside a button)

* Fix linting
  • Loading branch information
ashharrison90 authored and asudoh committed Oct 2, 2018
1 parent 374fd54 commit d32424c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,18 @@ export default class Tooltip extends Component {
document.body;

handleMouse = evt => {
const state =
typeof evt === 'string'
? evt
: { mouseover: 'over', mouseout: 'out', focus: 'over', blur: 'out' }[
evt.type
];
const state = {
mouseover: 'over',
mouseout: 'out',
focus: 'over',
blur: 'out',
click: 'click',
}[evt.type];
const hadContextMenu = this._hasContextMenu;
this._hasContextMenu = evt.type === 'contextmenu';
if (this.props.clickToOpen) {
if (state === 'click') {
evt.stopPropagation();
const shouldOpen = !this.state.open;
if (shouldOpen) {
this.getTriggerPosition();
Expand All @@ -304,6 +306,7 @@ export default class Tooltip extends Component {
const key = evt.key || evt.which;

if (key === 'Enter' || key === 13 || key === ' ' || key === 32) {
evt.stopPropagation();
this.setState({ open: !this.state.open });
}
};
Expand Down Expand Up @@ -361,6 +364,8 @@ export default class Tooltip extends Component {
id={triggerId}
role="button"
tabIndex="0"
onClick={evt => this.handleMouse(evt)}
onKeyDown={evt => this.handleKeyPress(evt)}
onMouseOver={evt => this.handleMouse(evt)}
onMouseOut={evt => this.handleMouse(evt)}
onFocus={evt => this.handleMouse(evt)}
Expand All @@ -369,8 +374,6 @@ export default class Tooltip extends Component {
aria-expanded={open}
{...ariaOwnsProps}>
<Icon
onKeyDown={this.handleKeyPress}
onClick={() => this.handleMouse('click')}
icon={!icon && !iconName ? iconInfoGlyph : icon}
name={iconName}
description={iconDescription}
Expand Down

0 comments on commit d32424c

Please sign in to comment.