Skip to content

Commit 7ff2971

Browse files
feat(core): add enterKeyHint prop to input
1 parent a695dfd commit 7ff2971

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

packages/autocomplete-core/src/__tests__/getInputProps.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,48 @@ describe('getInputProps', () => {
249249
expect(inputProps.type).toEqual('search');
250250
});
251251

252+
test('returns enterKeyHint "search" without item URL', () => {
253+
const { getInputProps, inputElement } = createPlayground(
254+
createAutocomplete,
255+
{
256+
defaultSelectedItemId: 0,
257+
initialState: {
258+
collections: [
259+
createCollection({
260+
items: [{ label: '1' }, { label: '2' }],
261+
}),
262+
],
263+
},
264+
}
265+
);
266+
const inputProps = getInputProps({ inputElement });
267+
268+
expect(inputProps.enterKeyHint).toEqual('search');
269+
});
270+
271+
test('returns enterKeyHint "go" with item URL', () => {
272+
const { getInputProps, inputElement } = createPlayground(
273+
createAutocomplete,
274+
{
275+
defaultSelectedItemId: 0,
276+
initialState: {
277+
collections: [
278+
createCollection({
279+
source: { getItemUrl: ({ item }) => item.url },
280+
items: [
281+
{ label: '1', url: '#1' },
282+
{ label: '2', url: '#2' },
283+
],
284+
}),
285+
],
286+
},
287+
}
288+
);
289+
const inputProps = getInputProps({ inputElement });
290+
291+
expect(inputProps.enterKeyHint).toEqual('go');
292+
});
293+
252294
describe('onChange', () => {
253295
test('sets the query', () => {
254296
const onStateChange = jest.fn();

packages/autocomplete-core/src/getPropGetters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export function getPropGetters<
180180

181181
const isTouchDevice = 'ontouchstart' in props.environment;
182182
const { inputElement, maxLength = 512, ...rest } = providedProps || {};
183+
const selectedItem = getSelectedItem(store.getState());
183184

184185
return {
185186
'aria-autocomplete': 'both',
@@ -194,6 +195,7 @@ export function getPropGetters<
194195
autoComplete: 'off',
195196
autoCorrect: 'off',
196197
autoCapitalize: 'off',
198+
enterKeyHint: selectedItem?.itemUrl ? 'go' : 'search',
197199
spellCheck: 'false',
198200
autoFocus: props.autoFocus,
199201
placeholder: props.placeholder,

packages/autocomplete-core/src/types/AutocompletePropGetters.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export type GetInputProps<TEvent, TMouseEvent, TKeyboardEvent> = (props: {
7676
autoComplete: 'on' | 'off';
7777
autoCorrect: 'on' | 'off';
7878
autoCapitalize: 'on' | 'off';
79+
enterKeyHint: 'go' | 'search';
7980
spellCheck: 'false';
8081
maxLength: number;
8182
type: 'search';

packages/website/docs/prop-getters.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type GetInputProps = (props: {
7373
autoComplete: 'on' | 'off';
7474
autoCorrect: 'on' | 'off';
7575
autoCapitalize: 'on' | 'off';
76+
enterKeyHint: 'go' | 'search';
7677
spellCheck: 'false';
7778
'aria-autocomplete': 'none' | 'inline' | 'list' | 'both';
7879
'aria-activedescendant': string | undefined;

0 commit comments

Comments
 (0)