Skip to content

Commit

Permalink
feat(Search): allow tracking Enter key press (#2600)
Browse files Browse the repository at this point in the history
  • Loading branch information
YossiSaadi authored Nov 21, 2024
1 parent 798497f commit 01a677a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const Search = forwardRef(
onFocus,
onBlur,
onClear,
onKeyDown,
className,
ariaExpanded,
ariaHasPopup,
Expand All @@ -53,14 +54,11 @@ const Search = forwardRef(
});

const onClearButtonClick = useCallback(() => {
if (disabled) {
return;
}

if (disabled) return;
inputRef.current?.focus?.();
clearValue();
onClear?.();
}, [disabled, clearValue]);
}, [disabled, clearValue, onClear]);

const SearchIcon = (
<Icon
Expand Down Expand Up @@ -114,6 +112,7 @@ const Search = forwardRef(
onChange={onEventChanged}
onBlur={onBlur}
onFocus={onFocus}
onKeyDown={onKeyDown}
autoComplete={autoComplete}
size={size}
wrapperRole="search"
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/components/Search/Search.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { SubIcon, VibeComponentProps } from "../../types";
import { InputSize } from "../BaseInput/BaseInput.types";
import { InputSize } from "../BaseInput";
import IconButton from "../IconButton/IconButton";
import MenuButton from "../MenuButton/MenuButton";

Expand Down Expand Up @@ -93,4 +93,8 @@ export interface SearchProps extends VibeComponentProps {
* Callback function that is called when the clear button is clicked.
*/
onClear?: () => void;
/**
* Callback function that is called when a keyboard key is down.
*/
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}
36 changes: 32 additions & 4 deletions packages/core/src/components/Search/__tests__/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ describe("Search", () => {
expect(queryByLabelText("Clear")).toBeNull();
});

it("should display both the search icon and clear icon when input has value", async () => {
it("should display both the search icon and clear icon when input has value", () => {
const { getByTestId, getAllByTestId } = renderSearch({ value: "Test" });
expect(getAllByTestId("icon")).toHaveLength(2);
expect(getByTestId("clean-search-button")).toBeInTheDocument();
});

it("should clear the input value when the clear icon is clicked", async () => {
it("should clear the input value when the clear icon is clicked", () => {
const { getByRole, getByLabelText } = renderSearch({ value: "Test" });
userEvent.click(getByLabelText("Clear"));
expect(getByRole("searchbox")).toHaveValue("");
});

it("should display the clear icon once user inputs", async () => {
it("should display the clear icon once user inputs", () => {
const { getByRole, getByTestId } = renderSearch();
userEvent.type(getByRole("searchbox"), "Test");
expect(getByTestId("clean-search-button")).toBeInTheDocument();
Expand All @@ -63,7 +63,7 @@ describe("Search", () => {
expect(onClear).toHaveBeenCalled;
});

it("should debounce the onChange call", async () => {
it("should debounce the onChange call", () => {
jest.useFakeTimers();
const onChange = jest.fn();

Expand Down Expand Up @@ -153,5 +153,33 @@ describe("Search", () => {
userEvent.tab();
expect(onBlur).toHaveBeenCalled();
});

it("should call onKeyDown when Enter key is pressed", () => {
const onKeyDown = jest.fn();
const { getByRole } = renderSearch({ onKeyDown });
const input = getByRole("searchbox");
userEvent.click(input);
userEvent.keyboard("{Enter}");
expect(onKeyDown).toHaveBeenCalledTimes(1);
});

it("should not call onKeyDown when input is disabled", () => {
const onKeyDown = jest.fn();
const { getByRole } = renderSearch({ onKeyDown, disabled: true });
const input = getByRole("searchbox");
userEvent.click(input);
userEvent.keyboard("{Enter}");
expect(onKeyDown).not.toHaveBeenCalled();
});

it("should call onKeyDown for each character when input is typed with content", () => {
const onKeyDown = jest.fn();
const string = "Hello, World!";
const { getByRole } = renderSearch({ onKeyDown });
const input = getByRole("searchbox");
userEvent.click(input);
userEvent.type(input, string);
expect(onKeyDown).toHaveBeenCalledTimes(string.length);
});
});
});

0 comments on commit 01a677a

Please sign in to comment.