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

[select] fix(Select2): fix regression in keyboard closing behavior #6163

Merged
merged 1 commit into from
May 16, 2023
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
3 changes: 2 additions & 1 deletion packages/select/src/components/select/select2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ export class Select2<T> extends AbstractPureComponent2<Select2Props<T>, Select2S
const menuItemDismiss =
menuItem?.matches(`.${Popover2Classes.POPOVER2_DISMISS}`) ||
menuItem?.matches(`.${CoreClasses.POPOVER_DISMISS}`);
const shouldDismiss = menuItemDismiss ?? true;

this.setState({ isOpen: !menuItemDismiss ?? false });
this.setState({ isOpen: !shouldDismiss });
this.props.onItemSelect?.(item, event);
};

Expand Down
41 changes: 28 additions & 13 deletions packages/select/test/select2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/

import { assert } from "chai";
import { mount } from "enzyme";
import { HTMLAttributes, mount, ReactWrapper } from "enzyme";
import * as React from "react";
import * as sinon from "sinon";

import { InputGroup, Keys, MenuItem } from "@blueprintjs/core";
import { Button, Classes as CoreClasses, InputGroup, Keys, MenuItem } from "@blueprintjs/core";
import { MenuItem2, Popover2 } from "@blueprintjs/popover2";

import { ItemRendererProps, Select2, Select2Props, Select2State } from "../src";
Expand Down Expand Up @@ -105,7 +105,7 @@ describe("<Select2>", () => {
const onOpening = sinon.spy();
const modifiers = {}; // our own instance
const wrapper = select({ popoverProps: { onOpening, modifiers } });
wrapper.find("[data-testid='target-button']").simulate("click");
findTargetButton(wrapper).simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("modifiers"), modifiers);
assert.isTrue(onOpening.calledOnce);
});
Expand All @@ -116,7 +116,7 @@ describe("<Select2>", () => {
const wrapper = select({ popoverProps: { usePortal: false } });
// should be closed to start
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);
wrapper.find("[data-testid='target-button']").simulate("keydown", { which: Keys.ARROW_DOWN });
findTargetButton(wrapper).simulate("keydown", { which: Keys.ARROW_DOWN });
// ...then open after key down
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);
});
Expand All @@ -128,6 +128,17 @@ describe("<Select2>", () => {
assert.isTrue(handlers.onItemSelect.calledOnce);
});

it("closes Popover2 after selecting active item with the Enter key", () => {
// override isOpen in defaultProps so that the popover can actually be closed
const wrapper = select({
popoverProps: { usePortal: true },
});
findTargetButton(wrapper).simulate("click");
wrapper.find("input").simulate("keydown", { keyCode: Keys.ENTER });
wrapper.find("input").simulate("keyup", { keyCode: Keys.ENTER });
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);
});

// N.B. it's not worth refactoring these tests to be DRY since there will soon
// only be 1 MenuItem component in Blueprint v5

Expand All @@ -141,11 +152,11 @@ describe("<Select2>", () => {
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);

// popover should open after clicking the button
wrapper.find("[data-testid='target-button']").simulate("click");
findTargetButton(wrapper).simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);

// and should close after the a menu item is clicked
wrapper.find(Popover2).find(".bp4-menu-item").first().simulate("click");
wrapper.find(Popover2).find(`.${CoreClasses.MENU_ITEM}`).first().simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);
});

Expand All @@ -159,11 +170,11 @@ describe("<Select2>", () => {
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);

// popover should open after clicking the button
wrapper.find("[data-testid='target-button']").simulate("click");
findTargetButton(wrapper).simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);

// and should not close after the a menu item is clicked
wrapper.find(Popover2).find(".bp4-menu-item").first().simulate("click");
wrapper.find(Popover2).find(`.${CoreClasses.MENU_ITEM}`).first().simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);
});

Expand All @@ -177,11 +188,11 @@ describe("<Select2>", () => {
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);

// popover should open after clicking the button
wrapper.find("[data-testid='target-button']").simulate("click");
findTargetButton(wrapper).simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);

// and should close after the a menu item is clicked
wrapper.find(Popover2).find(".bp4-menu-item").first().simulate("click");
wrapper.find(Popover2).find(`.${CoreClasses.MENU_ITEM}`).first().simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);
});

Expand All @@ -195,18 +206,18 @@ describe("<Select2>", () => {
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), false);

// popover should open after clicking the button
wrapper.find("[data-testid='target-button']").simulate("click");
findTargetButton(wrapper).simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);

// and should not close after the a menu item is clicked
wrapper.find(Popover2).find(".bp4-menu-item").first().simulate("click");
wrapper.find(Popover2).find(`.${CoreClasses.MENU_ITEM}`).first().simulate("click");
assert.strictEqual(wrapper.find(Popover2).prop("isOpen"), true);
});

function select(props: Partial<Select2Props<Film>> = {}, query?: string) {
const wrapper = mount(
<Select2<Film> {...defaultProps} {...handlers} {...props}>
<button data-testid="target-button">Target</button>
<Button data-testid="target-button" text="Target" />
</Select2>,
{ attachTo: testsContainerElement },
);
Expand All @@ -215,6 +226,10 @@ describe("<Select2>", () => {
}
return wrapper;
}

function findTargetButton(wrapper: ReactWrapper): ReactWrapper<HTMLAttributes> {
return wrapper.find("[data-testid='target-button']").hostNodes();
}
});

function filterByYear(query: string, film: Film) {
Expand Down