Skip to content

Commit e5aeea8

Browse files
authored
feat(autocomplete-core): add enterKeyHint option to manually set hint on virtual keyboards (#1164)
1 parent 739b49e commit e5aeea8

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

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

+24
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,30 @@ describe('getInputProps', () => {
293293
expect(inputProps.enterKeyHint).toEqual('go');
294294
});
295295

296+
test('returns enterKeyHint "enter" when explicitly defined', () => {
297+
const { getInputProps, inputElement } = createPlayground(
298+
createAutocomplete,
299+
{
300+
enterKeyHint: 'enter',
301+
defaultActiveItemId: 0,
302+
initialState: {
303+
collections: [
304+
createCollection({
305+
source: { getItemUrl: ({ item }) => item.url },
306+
items: [
307+
{ label: '1', url: '#1' },
308+
{ label: '2', url: '#2' },
309+
],
310+
}),
311+
],
312+
},
313+
}
314+
);
315+
const inputProps = getInputProps({ inputElement });
316+
317+
expect(inputProps.enterKeyHint).toEqual('enter');
318+
});
319+
296320
describe('onChange', () => {
297321
test('sets the query', () => {
298322
const onStateChange = jest.fn();

packages/autocomplete-core/src/getDefaultProps.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function getDefaultProps<TItem extends BaseItem>(
2727
return {
2828
debug: false,
2929
openOnFocus: false,
30+
enterKeyHint: undefined,
3031
placeholder: '',
3132
autoFocus: false,
3233
defaultActiveItemId: null,

packages/autocomplete-core/src/getPropGetters.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ export function getPropGetters<
173173
const userAgent = props.environment.navigator?.userAgent || '';
174174
const shouldFallbackKeyHint = isSamsung(userAgent);
175175
const enterKeyHint =
176-
activeItem?.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search';
176+
props.enterKeyHint ||
177+
(activeItem?.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search');
177178

178179
return {
179180
'aria-autocomplete': 'both',

packages/autocomplete-shared/src/core/AutocompleteOptions.ts

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import {
1111
} from './AutocompleteSource';
1212
import { AutocompleteState } from './AutocompleteState';
1313

14+
export type AutocompleteEnterKeyHint =
15+
| 'enter'
16+
| 'done'
17+
| 'go'
18+
| 'next'
19+
| 'previous'
20+
| 'search'
21+
| 'send';
22+
1423
export interface OnSubmitParams<TItem extends BaseItem>
1524
extends AutocompleteScopeApi<TItem> {
1625
state: AutocompleteState<TItem>;
@@ -73,6 +82,12 @@ export interface AutocompleteOptions<TItem extends BaseItem> {
7382
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-onstatechange
7483
*/
7584
onStateChange?(props: OnStateChangeProps<TItem>): void;
85+
/**
86+
* The action label or icon to present for the enter key on virtual keyboards.
87+
*
88+
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-enterkeyhint
89+
*/
90+
enterKeyHint?: AutocompleteEnterKeyHint;
7691
/**
7792
* The placeholder text to show in the search input when there's no query.
7893
*
@@ -184,6 +199,7 @@ export interface InternalAutocompleteOptions<TItem extends BaseItem>
184199
debug: boolean;
185200
id: string;
186201
onStateChange(props: OnStateChangeProps<TItem>): void;
202+
enterKeyHint: AutocompleteEnterKeyHint | undefined;
187203
placeholder: string;
188204
autoFocus: boolean;
189205
defaultActiveItemId: number | null;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseItem } from './AutocompleteApi';
2+
import { AutocompleteEnterKeyHint } from './AutocompleteOptions';
23
import { InternalAutocompleteSource } from './AutocompleteSource';
34

45
export interface AutocompletePropGetters<
@@ -76,7 +77,7 @@ export type GetInputProps<TEvent, TMouseEvent, TKeyboardEvent> = (props: {
7677
autoComplete: 'on' | 'off';
7778
autoCorrect: 'on' | 'off';
7879
autoCapitalize: 'on' | 'off';
79-
enterKeyHint: 'go' | 'search';
80+
enterKeyHint: AutocompleteEnterKeyHint;
8081
spellCheck: 'false';
8182
maxLength: number;
8283
type: 'search';

0 commit comments

Comments
 (0)