Skip to content

Commit 86a966c

Browse files
reidbarberLFDanLu
andauthored
Fix SearchField to call onKeyDown (#2617)
* chain onKeyDown prop * add test for onKeyDown Co-authored-by: Daniel Lu <[email protected]>
1 parent 532bfaf commit 86a966c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/@react-aria/searchfield/src/useSearchField.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import {AriaButtonProps} from '@react-types/button';
1414
import {AriaSearchFieldProps} from '@react-types/searchfield';
15+
import {chain} from '@react-aria/utils';
1516
import {HTMLAttributes, InputHTMLAttributes, LabelHTMLAttributes, RefObject} from 'react';
1617
// @ts-ignore
1718
import intlMessages from '../intl/*.json';
@@ -92,7 +93,7 @@ export function useSearchField(
9293
...props,
9394
value: state.value,
9495
onChange: state.setValue,
95-
onKeyDown,
96+
onKeyDown: chain(onKeyDown, props.onKeyDown),
9697
type
9798
}, inputRef);
9899

packages/@react-aria/searchfield/test/useSearchField.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('useSearchField hook', () => {
5454
let preventDefault = jest.fn();
5555
let stopPropagation = jest.fn();
5656
let onSubmit = jest.fn();
57+
let onKeyDown = jest.fn();
5758
let event = (key) => ({
5859
key,
5960
preventDefault,
@@ -97,6 +98,12 @@ describe('useSearchField hook', () => {
9798
let {inputProps} = renderSearchHook({onClear, onSubmit, defaultValue: 'ABC'});
9899
expect(inputProps.defaultValue).not.toBeDefined();
99100
});
101+
102+
it('onKeyDown prop is called', () => {
103+
let {inputProps} = renderSearchHook({onKeyDown});
104+
inputProps.onKeyDown(event('Enter'));
105+
expect(onKeyDown).toHaveBeenCalledTimes(1);
106+
});
100107
});
101108
});
102109

0 commit comments

Comments
 (0)