Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ESC to propagate if EuiPopover is not open #2089

Merged
merged 5 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added TS defs for `EuiComboBox`'s props spreading onto a `div` ([#2080](https://github.com/elastic/eui/pull/2080))
- Fixed responsive display of inline `EuiDatePicker` ([#1820](https://github.com/elastic/eui/pull/1820))
- Removed time from default `dateFormat` of `EuiDatePicker` ([#1820](https://github.com/elastic/eui/pull/1820))
- Fixed `EuiPopover` from catching and preventing propagation of keydown events when closed ([#2089](https://github.com/elastic/eui/pull/2089))

## [`12.2.0`](https://github.com/elastic/eui/tree/v12.2.0)

Expand Down
5 changes: 3 additions & 2 deletions src/components/color_picker/color_picker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, mount } from 'enzyme';

import { EuiColorPicker } from './color_picker';
import { VISUALIZATION_COLORS, keyCodes } from '../../services';
import { requiredProps, findTestSubject } from '../../test';
import { requiredProps, findTestSubject, sleep } from '../../test';

jest.mock('../portal', () => ({
EuiPortal: ({ children }) => children,
Expand Down Expand Up @@ -117,7 +117,7 @@ test('popover color selector is shown when the input is clicked', () => {
expect(colorSelector.length).toBe(1);
});

test('popover color selector is hidden when the ESC key pressed', () => {
test('popover color selector is hidden when the ESC key pressed', async () => {
const onBlurHandler = jest.fn();
const colorPicker = mount(
<EuiColorPicker
Expand All @@ -129,6 +129,7 @@ test('popover color selector is hidden when the ESC key pressed', () => {
);

findTestSubject(colorPicker, 'colorPickerAnchor').simulate('click');
await sleep();
findTestSubject(colorPicker, 'colorPickerPopover').simulate('keydown', {
keyCode: keyCodes.ESCAPE,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AsyncInterval } from './async_interval';
import { times } from 'lodash';
import { sleep } from '../../../test';

describe('AsyncInterval', () => {
beforeEach(() => {
Expand Down Expand Up @@ -67,7 +68,6 @@ describe('AsyncInterval', () => {
let instance;
let spy;
beforeEach(() => {
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
spy = jest.fn(async () => await sleep(2000));
instance = new AsyncInterval(spy, 1000);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { FunctionComponent } from 'react';
import { mount } from 'enzyme';
import { EuiMutationObserver } from './mutation_observer';

async function sleep(duration: number) {
return new Promise(resolve => {
setTimeout(resolve, duration);
});
}
import { sleep } from '../../../test';

export async function waitforMutationObserver(period = 30) {
// `period` defaults to 30 because its the delay used by the MutationObserver polyfill
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { FunctionComponent } from 'react';
import { mount } from 'enzyme';
import { EuiResizeObserver } from './resize_observer';

async function sleep(duration: number) {
return new Promise(resolve => {
setTimeout(resolve, duration);
});
}
import { sleep } from '../../../test';

export async function waitforResizeObserver(period = 30) {
// `period` defaults to 30 because its the delay used by the ResizeObserver polyfill
Expand Down
18 changes: 9 additions & 9 deletions src/components/popover/__snapshots__/popover.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports[`EuiPopover props anchorClassName is rendered 1`] = `
exports[`EuiPopover props anchorPosition defaults to centerDown 1`] = `
<div
class="euiPopover euiPopover--anchorDownCenter"
id="5"
id="6"
>
<div
class="euiPopover__anchor"
Expand All @@ -57,7 +57,7 @@ exports[`EuiPopover props anchorPosition defaults to centerDown 1`] = `
exports[`EuiPopover props anchorPosition downRight is rendered 1`] = `
<div
class="euiPopover euiPopover--anchorDownRight"
id="7"
id="8"
>
<div
class="euiPopover__anchor"
Expand All @@ -70,7 +70,7 @@ exports[`EuiPopover props anchorPosition downRight is rendered 1`] = `
exports[`EuiPopover props anchorPosition leftCenter is rendered 1`] = `
<div
class="euiPopover euiPopover--anchorLeftCenter"
id="6"
id="7"
>
<div
class="euiPopover__anchor"
Expand All @@ -83,7 +83,7 @@ exports[`EuiPopover props anchorPosition leftCenter is rendered 1`] = `
exports[`EuiPopover props isOpen defaults to false 1`] = `
<div
class="euiPopover euiPopover--anchorDownCenter"
id="8"
id="9"
>
<div
class="euiPopover__anchor"
Expand All @@ -97,7 +97,7 @@ exports[`EuiPopover props isOpen renders true 1`] = `
<div>
<div
class="euiPopover euiPopover--anchorDownCenter euiPopover-isOpen"
id="9"
id="10"
>
<div
class="euiPopover__anchor"
Expand Down Expand Up @@ -144,7 +144,7 @@ exports[`EuiPopover props ownFocus defaults to false 1`] = `
<div>
<div
class="euiPopover euiPopover--anchorDownCenter euiPopover-isOpen"
id="10"
id="11"
>
<div
class="euiPopover__anchor"
Expand Down Expand Up @@ -191,7 +191,7 @@ exports[`EuiPopover props ownFocus renders true 1`] = `
<div>
<div
class="euiPopover euiPopover--anchorDownCenter euiPopover-isOpen"
id="11"
id="12"
>
<div
class="euiPopover__anchor"
Expand Down Expand Up @@ -246,7 +246,7 @@ exports[`EuiPopover props panelClassName is rendered 1`] = `
<div>
<div
class="euiPopover euiPopover--anchorDownCenter euiPopover-isOpen"
id="12"
id="13"
>
<div
class="euiPopover__anchor"
Expand Down Expand Up @@ -293,7 +293,7 @@ exports[`EuiPopover props panelPaddingSize is rendered 1`] = `
<div>
<div
class="euiPopover euiPopover--anchorDownCenter euiPopover-isOpen"
id="13"
id="14"
>
<div
class="euiPopover__anchor"
Expand Down
8 changes: 5 additions & 3 deletions src/components/popover/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ export class EuiPopover extends Component {

onKeyDown = e => {
if (e.keyCode === cascadingMenuKeyCodes.ESCAPE) {
e.preventDefault();
e.stopPropagation();
this.props.closePopover();
if (this.state.isOpenStable || this.state.isOpening) {
e.preventDefault();
e.stopPropagation();
this.props.closePopover();
}
}
};

Expand Down
25 changes: 21 additions & 4 deletions src/components/popover/popover.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { render, mount } from 'enzyme';
import sinon from 'sinon';
import { requiredProps } from '../../test/required_props';

import {
Expand Down Expand Up @@ -74,20 +73,38 @@ describe('EuiPopover', () => {
});

describe('closePopover', () => {
it('is called when ESC key is hit', () => {
const closePopoverHandler = sinon.stub();
it('is called when ESC key is hit and the popover is open', () => {
const closePopoverHandler = jest.fn();

const component = mount(
<EuiPopover
id={getId()}
withTitle
button={<button />}
closePopover={closePopoverHandler}
isOpen
/>
);

component.simulate('keydown', { keyCode: keyCodes.ESCAPE });
sinon.assert.calledOnce(closePopoverHandler);
expect(closePopoverHandler).toBeCalledTimes(1);
});

it('is not called when ESC key is hit and the popover is closed', () => {
const closePopoverHandler = jest.fn();

const component = mount(
<EuiPopover
id={getId()}
withTitle
button={<button />}
closePopover={closePopoverHandler}
isOpen={false}
/>
);

component.simulate('keydown', { keyCode: keyCodes.ESCAPE });
expect(closePopoverHandler).not.toBeCalled();
});
});

Expand Down
1 change: 1 addition & 0 deletions src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export {
startThrowingReactWarnings,
stopThrowingReactWarnings,
} from './react_warnings';
export { sleep } from './sleep';
6 changes: 6 additions & 0 deletions src/test/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// async timeout function for awaiting state or DOM updates
export function sleep(ms: number = 50) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}