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

Stop Combobox Esc propagation #683

Closed
Closed
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
28 changes: 28 additions & 0 deletions packages/combobox/__tests__/combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ComboboxInputProps,
useComboboxContext,
} from "@reach/combobox";
import { Dialog } from "@reach/dialog";
import matchSorter from "match-sorter";
import cities from "../examples/cities";

Expand Down Expand Up @@ -219,9 +220,36 @@ describe("<Combobox />", () => {
expect(getByTextWithMarkup(optionToSelect)).toBeInTheDocument();
});
});

describe("Combobox inside dialog", () => {
it("should not close the dialog when Esc key is pressed", async () => {
let { getByTestId, queryByTestId } = render(<BasicDialog />);
let input = getByTestId("input");

await userEvent.type(input, "e");

expect(getByTestId("list")).toBeInTheDocument();

await userEvent.type(input, "{esc}");

expect(queryByTestId("list")).not.toBeInTheDocument();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frontsideair thanks for your PR contribution!

The issue with your test seems to be the use of queryByTestId. Using ByTestId is not recommended if there are other queries more inline with how the software is used by people or screen readers. We can use ByRole for most things associated with Combobox, and indeed your tests pass with these query selectors.

Given #395 also cares about the event propagating, we should also assert that our dialog remains open when escape is pressed. Perhaps something like this:

it("should not close the dialog when Esc key is pressed", async () => {
  let { getByRole, queryByRole } = render(<BasicDialog />);
  let input = getByRole("combobox");

  expect(getByRole("dialog")).toBeInTheDocument();

  await userEvent.type(input, "e");

  expect(getByRole("listbox")).toBeInTheDocument();

  await userEvent.type(input, "{esc}");

  // escape should close our listbox
  expect(queryByRole("listbox")).not.toBeInTheDocument();

  // but our dialog should still be open
  expect(queryByRole("dialog")).toBeInTheDocument();
});

expect(getByTestId("input")).toBeInTheDocument();
});
});
});

////////////////////////////////////////////////////////////////////////////////
function BasicDialog() {
const [showDialog, setShowDialog] = React.useState(true);
const close = () => setShowDialog(false);

return (
<Dialog aria-label="Test" isOpen={showDialog} onDismiss={close}>
<BasicCombobox />
</Dialog>
);
}

function BasicCombobox() {
let [term, setTerm] = useState("");
let results = useCityMatch(term);
Expand Down
1 change: 1 addition & 0 deletions packages/combobox/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ function useKeyDown() {
case "Escape":
if (state !== IDLE) {
transition(ESCAPE);
event.stopPropagation();
}
break;
case "Enter":
Expand Down