Skip to content

Commit cf86445

Browse files
neotobneotrow
andauthored
add onkeydown event (#138)
Co-authored-by: Timothy Trowbridge <92933708+neotrow@users.noreply.github.com>
1 parent f66d5c8 commit cf86445

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- property `onKeyDown` to `Input` component
13+
1014
## [3.7.1] - 2025-07-17
1115

1216
### Fixed

cypress/cypress/component/Input/BasicInputField.cy.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,31 @@ describe("Input.cy.tsx", () => {
249249
cy.get("@onBlurSpy").should("be.calledWithMatch", { target: { value: randomWord } });
250250
});
251251

252+
it("on key down handler gets called", () => {
253+
const name = faker.random.alpha(10);
254+
const schema = yup.object().shape({
255+
[name]: yup.string(),
256+
});
257+
258+
const randomWord = faker.random.word();
259+
260+
cy.mount(
261+
<Form onSubmit={cy.spy().as("onSubmitSpy")} resolver={yupResolver(schema)}>
262+
{/* <Input name={name} label={name} onKeyDown={(e) => console.log("key down", e)} /> */}
263+
<Input name={name} label={name} onKeyDown={cy.spy().as("onKeyDownSpy")} />
264+
265+
<input type={"submit"} />
266+
</Form>,
267+
);
268+
269+
cy.contains("label", name).click();
270+
cy.focused().type(randomWord.toString());
271+
for (const char of randomWord) {
272+
cy.get("@onKeyDownSpy").should("be.calledWithMatch", { key: char });
273+
}
274+
cy.get("@onKeyDownSpy").should("have.callCount", randomWord.length);
275+
});
276+
252277
it("text area works", () => {
253278
const name = faker.random.alpha(10);
254279
const textAreaRows = faker.datatype.number({ min: 6, max: 10 });

src/lib/components/Input/InputInternal.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const InputInternal = <T extends FieldValues>(props: InputProps<T>) => {
1414
type,
1515
onBlur,
1616
onChange,
17+
onKeyDown,
1718
value,
1819
options,
1920
multiple,
@@ -93,6 +94,7 @@ const InputInternal = <T extends FieldValues>(props: InputProps<T>) => {
9394
await rest.onChange(e);
9495
})();
9596
}}
97+
onKeyDown={onKeyDown}
9698
onFocus={focusHandler}
9799
className={className}
98100
>

src/lib/types/CommonInputProps.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ interface CommonInputProps<T extends FieldValues, TRenderAddon = unknown> {
8686
* @example <Input<T> type="text" maxlength={10} />
8787
*/
8888
maxLength?: number;
89+
90+
/**
91+
* Component prop that represents the onKeyDown event handler for the input element
92+
* @type {(e: React.KeyboardEvent<HTMLInputElement>) => void}
93+
* @example <Input<T> type="text" onKeyDown={(e) => console.log(e.key)} />
94+
*/
95+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
8996
}
9097

9198
export { CommonInputProps };

0 commit comments

Comments
 (0)