Skip to content

Commit

Permalink
feat(Search): replace onEnterKey with generic onKeyDown
Browse files Browse the repository at this point in the history
leave the handling for the consumer
  • Loading branch information
YossiSaadi committed Nov 21, 2024
1 parent 1b2923c commit 626211e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
12 changes: 2 additions & 10 deletions packages/core/src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const Search = forwardRef(
onFocus,
onBlur,
onClear,
onEnterKey,
onKeyDown,
className,
ariaExpanded,
ariaHasPopup,
Expand All @@ -60,14 +60,6 @@ const Search = forwardRef(
onClear?.();
}, [disabled, clearValue, onClear]);

const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
if (disabled || event.key !== "Enter") return;
onEnterKey?.(event);
},
[disabled, onEnterKey]
);

const SearchIcon = (
<Icon
icon={searchIconName}
Expand Down Expand Up @@ -120,7 +112,7 @@ const Search = forwardRef(
onChange={onEventChanged}
onBlur={onBlur}
onFocus={onFocus}
onKeyDown={handleKeyDown}
onKeyDown={onKeyDown}
autoComplete={autoComplete}
size={size}
wrapperRole="search"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/components/Search/Search.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface SearchProps extends VibeComponentProps {
*/
onClear?: () => void;
/**
* Callback function that is called when the Enter keyboard key is down.
* Callback function that is called when a keyboard key is down.
*/
onEnterKey?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}
37 changes: 14 additions & 23 deletions packages/core/src/components/Search/__tests__/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,41 +154,32 @@ describe("Search", () => {
expect(onBlur).toHaveBeenCalled();
});

it("should call onEnterKey when Enter key is pressed", () => {
const onEnterKey = jest.fn();
const { getByRole } = renderSearch({ onEnterKey });
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(onEnterKey).toHaveBeenCalledTimes(1);
expect(onKeyDown).toHaveBeenCalledTimes(1);
});

it("should not call onEnterKey when other keys are pressed", () => {
const onEnterKey = jest.fn();
const { getByRole } = renderSearch({ onEnterKey });
const input = getByRole("searchbox");
userEvent.click(input);
userEvent.keyboard("a");
expect(onEnterKey).not.toHaveBeenCalled();
});

it("should not call onEnterKey when input is disabled", () => {
const onEnterKey = jest.fn();
const { getByRole } = renderSearch({ onEnterKey, disabled: true });
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(onEnterKey).not.toHaveBeenCalled();
expect(onKeyDown).not.toHaveBeenCalled();
});

it("should call onEnterKey when input has content and Enter is pressed", () => {
const onEnterKey = jest.fn();
const { getByRole } = renderSearch({ onEnterKey });
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, "Hello, World!");
userEvent.keyboard("{Enter}");
expect(onEnterKey).toHaveBeenCalledTimes(1);
userEvent.type(input, string);
expect(onKeyDown).toHaveBeenCalledTimes(string.length);
});
});
});

0 comments on commit 626211e

Please sign in to comment.