Skip to content

Commit

Permalink
feat: focus searchbox input after clearing canonical#775
Browse files Browse the repository at this point in the history
  • Loading branch information
petermakowski committed Sep 13, 2022
1 parent 84ec66f commit cb64daa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/SearchBox/SearchBox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { shallow, mount } from "enzyme";
import React from "react";

import userEvent from "@testing-library/user-event";
import { render, screen } from "@testing-library/react";
import SearchBox from "./SearchBox";

describe("SearchBox ", () => {
Expand Down Expand Up @@ -73,4 +74,15 @@ describe("SearchBox ", () => {
expect(wrapper.find("input").getDOMNode()).toHaveFocus();
document.body.removeChild(container);
});

it("focuses on the input field after clearing", async () => {
render(
<SearchBox externallyControlled onChange={jest.fn()} value="admin" />
);
await userEvent.click(screen.getByLabelText("Search"));
await userEvent.click(
screen.getByRole("button", { name: "Clear search field" })
);
expect(screen.getByLabelText("Search")).toHaveFocus();
});
});
1 change: 1 addition & 0 deletions src/components/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const SearchBox = React.forwardRef<HTMLInputElement, Props>(
onChange && onChange("");
if (internalRef.current) {
internalRef.current.value = "";
internalRef.current.focus();
}
};

Expand Down

0 comments on commit cb64daa

Please sign in to comment.