Skip to content

Commit

Permalink
fix(tooltip): prevent escape keydown from bubbling (#9875)
Browse files Browse the repository at this point in the history
* fix(tooltip): prevent escape keydown from bubbling

* Update packages/react/src/components/Tooltip/Tooltip.js

Co-authored-by: Scott Strubberg <sstrubberg@protonmail.com>

* chore: remove story used for testing

Co-authored-by: Scott Strubberg <sstrubberg@protonmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 18, 2021
1 parent 3d06011 commit 4165cc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/react/src/components/Tooltip/Tooltip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
*/

import React, { Component } from 'react';
import debounce from 'lodash.debounce';
import debounce from 'lodash.debounce'; // eslint-disable-line no-unused-vars
import FloatingMenu from '../../internal/FloatingMenu';
import Tooltip from '../Tooltip';
import { mount } from 'enzyme';
import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
Information16 as Information,
Add16 as Add,
OverflowMenuVertical16,
} from '@carbon/icons-react';
import { settings } from 'carbon-components';
import '@testing-library/jest-dom';

const { prefix } = settings;

jest.mock('lodash.debounce');

debounce.mockImplementation((fn) => fn);
jest.mock('lodash.debounce', () => (fn) => {
fn.cancel = jest.fn();
return fn;
});

describe('Tooltip', () => {
// An icon component class
Expand Down Expand Up @@ -191,4 +195,21 @@ describe('Tooltip', () => {
expect(rootWrapper.find('Tooltip').instance().state.open).toEqual(false);
});
});

it('escape key keyDown should not bubble outside the tooltip', () => {
const onKeyDown = jest.fn();
render(
<>
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
<div onKeyDown={onKeyDown}>
<Tooltip triggerText="Tooltip" />
</div>
</>
);

userEvent.click(screen.getAllByRole('button')[0]);
userEvent.keyboard('{esc}');

expect(onKeyDown).not.toHaveBeenCalled();
});
});
8 changes: 8 additions & 0 deletions packages/react/src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,16 @@ class Tooltip extends Component {
this._tooltipEl = node;
}}
updateOrientation={this.updateOrientation}>
{/* This rule is disabled because the onKeyDown event handler is only
* being used to capture and prevent the event from bubbling: */}
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
<div
className={tooltipClasses}
onKeyDown={(event) => {
if (keyDownMatch(event, [keys.Escape])) {
event.stopPropagation();
}
}}
{...other}
id={this._tooltipId}
data-floating-menu-direction={storedDirection}
Expand Down

0 comments on commit 4165cc4

Please sign in to comment.