Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Comment on lines +14 to +21
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly HTMLElement['enterKeyHint'] is too broad, so I went for an explicit listing of allowed values, based on https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/enterkeyhint#values.


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