Skip to content

Commit

Permalink
SearchField: Add onKeyUp prop (#2326)
Browse files Browse the repository at this point in the history
## Summary:

The SearchField component now accepts an `onKeyUp` prop that will be called when
the user releases a key while the input is focused.

This is to bring consistency with the other input components in the library and
also to allow for more flexibility when using the SearchField component.

Also took the opportunity to clear some of the SB argtype descriptions, as we no
longer need to duplicate them as of SB 8.0.

NOTE: This will be used as part of the Search page refactor.

Issue: FEI-5728

## Test plan:

Verify that the unit tests pass.

Author: jandrade

Reviewers: beaesguerra, jandrade

Required Reviewers:

Approved By: beaesguerra

Checks: ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 2/2), ✅ Lint (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 1/2), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ⏭️  Chromatic - Skip on Release PR (changesets), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ gerald, ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ⏭️  dependabot

Pull Request URL: #2326
  • Loading branch information
jandrade authored Sep 19, 2024
1 parent 910e2eb commit 659a031
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-bats-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-search-field": minor
---

Add onKeyUp prop to the `SearchField` component
30 changes: 8 additions & 22 deletions __docs__/wonder-blocks-search-field/search-field.argtypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export default {
clearAriaLabel: {
description: `ARIA label for the clear button. Defaults to "Clear search".`,
type: {name: "string", required: false},
table: {
type: {
Expand All @@ -13,8 +12,6 @@ export default {
},
},
id: {
description: `The unique identifier for the input. If one is not
provided, a unique id will be generated.`,
type: {name: "string", required: false},
table: {
type: {
Expand All @@ -26,7 +23,6 @@ export default {
},
},
value: {
description: "The text input value.",
type: {name: "string", required: true},
table: {
type: {
Expand All @@ -36,8 +32,6 @@ export default {
control: {type: "text"},
},
name: {
description: `The name for the input control. This is submitted along
with the form data.`,
type: {name: "string", required: false},
table: {
type: {
Expand All @@ -49,9 +43,6 @@ export default {
},
},
placeholder: {
description: `Provide hints or examples of what to enter.
This shows up as a grayed out text in the field before
a value is entered.`,
type: {name: "string", required: false},
table: {
type: {
Expand All @@ -63,7 +54,6 @@ export default {
},
},
autoFocus: {
description: "Whether this field should autofocus on page load.",
type: {name: "boolean", required: false},
table: {
type: {
Expand All @@ -78,8 +68,6 @@ export default {
},
},
disabled: {
description: `Makes a read-only input field that cannot be focused.
Defaults to false.`,
type: {name: "boolean", required: false},
table: {
type: {
Expand All @@ -94,8 +82,6 @@ export default {
},
},
light: {
description:
"Change the default focus ring color to fit a dark background.",
type: {name: "boolean", required: false},
table: {
type: {
Expand All @@ -110,7 +96,6 @@ export default {
},
},
style: {
description: "Custom styles for the main wrapper.",
table: {
type: {
summary: "Style",
Expand All @@ -124,7 +109,6 @@ export default {
},
},
testId: {
description: "Test ID used for e2e testing.",
type: {name: "string", required: false},
table: {
type: {
Expand All @@ -136,7 +120,6 @@ export default {
},
},
onChange: {
description: "Called when the value has changed.",
table: {
type: {
summary: "function",
Expand All @@ -145,8 +128,6 @@ export default {
},
},
onClick: {
description: `Handler that is triggered when this component is clicked.
For example, use this to adjust focus in parent component.`,
table: {
type: {
summary: "function",
Expand All @@ -155,7 +136,14 @@ export default {
},
},
onKeyDown: {
description: "Called when a key is pressed.",
table: {
type: {
summary: "function",
},
category: "Events",
},
},
onKeyUp: {
table: {
type: {
summary: "function",
Expand All @@ -164,7 +152,6 @@ export default {
},
},
onFocus: {
description: "Called when the element has been focused.",
table: {
type: {
summary: "function",
Expand All @@ -173,7 +160,6 @@ export default {
},
},
onBlur: {
description: "Called when the element has been blurred.",
table: {
type: {
summary: "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,27 @@ describe("SearchField", () => {
// Assert
expect(searchField).not.toHaveFocus();
});

it("onKeyDown is called after keyboard key press", async () => {
// Arrange
const handleOnKeyDown = jest.fn(
(event: React.KeyboardEvent<HTMLInputElement>) => {
return event.key;
},
);

render(
<SearchField
value="something"
onChange={() => {}}
onKeyDown={handleOnKeyDown}
/>,
);

// Act
await userEvent.type(await screen.findByRole("textbox"), "{enter}");

// Assert
expect(handleOnKeyDown).toHaveReturnedWith("Enter");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ type Props = AriaProps & {
* Called when a key is pressed.
*/
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
/**
* Called when a key is released.
*/
onKeyUp?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
/**
* Called when the element has been focused.
*/
Expand Down

0 comments on commit 659a031

Please sign in to comment.