Skip to content

Commit

Permalink
feat(autocomplete-core): add enterKeyHint option to manually set hi…
Browse files Browse the repository at this point in the history
…nt on virtual keyboards (#1164)
  • Loading branch information
dhayab authored Jun 30, 2023
1 parent 739b49e commit e5aeea8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/autocomplete-core/src/__tests__/getInputProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,30 @@ describe('getInputProps', () => {
expect(inputProps.enterKeyHint).toEqual('go');
});

test('returns enterKeyHint "enter" when explicitly defined', () => {
const { getInputProps, inputElement } = createPlayground(
createAutocomplete,
{
enterKeyHint: 'enter',
defaultActiveItemId: 0,
initialState: {
collections: [
createCollection({
source: { getItemUrl: ({ item }) => item.url },
items: [
{ label: '1', url: '#1' },
{ label: '2', url: '#2' },
],
}),
],
},
}
);
const inputProps = getInputProps({ inputElement });

expect(inputProps.enterKeyHint).toEqual('enter');
});

describe('onChange', () => {
test('sets the query', () => {
const onStateChange = jest.fn();
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-core/src/getDefaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function getDefaultProps<TItem extends BaseItem>(
return {
debug: false,
openOnFocus: false,
enterKeyHint: undefined,
placeholder: '',
autoFocus: false,
defaultActiveItemId: null,
Expand Down
3 changes: 2 additions & 1 deletion packages/autocomplete-core/src/getPropGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export function getPropGetters<
const userAgent = props.environment.navigator?.userAgent || '';
const shouldFallbackKeyHint = isSamsung(userAgent);
const enterKeyHint =
activeItem?.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search';
props.enterKeyHint ||
(activeItem?.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search');

return {
'aria-autocomplete': 'both',
Expand Down
16 changes: 16 additions & 0 deletions packages/autocomplete-shared/src/core/AutocompleteOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import {
} from './AutocompleteSource';
import { AutocompleteState } from './AutocompleteState';

export type AutocompleteEnterKeyHint =
| 'enter'
| 'done'
| 'go'
| 'next'
| 'previous'
| 'search'
| 'send';

export interface OnSubmitParams<TItem extends BaseItem>
extends AutocompleteScopeApi<TItem> {
state: AutocompleteState<TItem>;
Expand Down Expand Up @@ -73,6 +82,12 @@ export interface AutocompleteOptions<TItem extends BaseItem> {
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-onstatechange
*/
onStateChange?(props: OnStateChangeProps<TItem>): void;
/**
* The action label or icon to present for the enter key on virtual keyboards.
*
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-enterkeyhint
*/
enterKeyHint?: AutocompleteEnterKeyHint;
/**
* The placeholder text to show in the search input when there's no query.
*
Expand Down Expand Up @@ -184,6 +199,7 @@ export interface InternalAutocompleteOptions<TItem extends BaseItem>
debug: boolean;
id: string;
onStateChange(props: OnStateChangeProps<TItem>): void;
enterKeyHint: AutocompleteEnterKeyHint | undefined;
placeholder: string;
autoFocus: boolean;
defaultActiveItemId: number | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseItem } from './AutocompleteApi';
import { AutocompleteEnterKeyHint } from './AutocompleteOptions';
import { InternalAutocompleteSource } from './AutocompleteSource';

export interface AutocompletePropGetters<
Expand Down Expand Up @@ -76,7 +77,7 @@ export type GetInputProps<TEvent, TMouseEvent, TKeyboardEvent> = (props: {
autoComplete: 'on' | 'off';
autoCorrect: 'on' | 'off';
autoCapitalize: 'on' | 'off';
enterKeyHint: 'go' | 'search';
enterKeyHint: AutocompleteEnterKeyHint;
spellCheck: 'false';
maxLength: number;
type: 'search';
Expand Down

0 comments on commit e5aeea8

Please sign in to comment.