Skip to content

Commit

Permalink
feat(core): add enterKeyHint prop to input
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Dec 11, 2020
1 parent a695dfd commit 7ff2971
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/autocomplete-core/src/__tests__/getInputProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,48 @@ describe('getInputProps', () => {
expect(inputProps.type).toEqual('search');
});

test('returns enterKeyHint "search" without item URL', () => {
const { getInputProps, inputElement } = createPlayground(
createAutocomplete,
{
defaultSelectedItemId: 0,
initialState: {
collections: [
createCollection({
items: [{ label: '1' }, { label: '2' }],
}),
],
},
}
);
const inputProps = getInputProps({ inputElement });

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

test('returns enterKeyHint "go" with item URL', () => {
const { getInputProps, inputElement } = createPlayground(
createAutocomplete,
{
defaultSelectedItemId: 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('go');
});

describe('onChange', () => {
test('sets the query', () => {
const onStateChange = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions packages/autocomplete-core/src/getPropGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export function getPropGetters<

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

return {
'aria-autocomplete': 'both',
Expand All @@ -194,6 +195,7 @@ export function getPropGetters<
autoComplete: 'off',
autoCorrect: 'off',
autoCapitalize: 'off',
enterKeyHint: selectedItem?.itemUrl ? 'go' : 'search',
spellCheck: 'false',
autoFocus: props.autoFocus,
placeholder: props.placeholder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type GetInputProps<TEvent, TMouseEvent, TKeyboardEvent> = (props: {
autoComplete: 'on' | 'off';
autoCorrect: 'on' | 'off';
autoCapitalize: 'on' | 'off';
enterKeyHint: 'go' | 'search';
spellCheck: 'false';
maxLength: number;
type: 'search';
Expand Down
1 change: 1 addition & 0 deletions packages/website/docs/prop-getters.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type GetInputProps = (props: {
autoComplete: 'on' | 'off';
autoCorrect: 'on' | 'off';
autoCapitalize: 'on' | 'off';
enterKeyHint: 'go' | 'search';
spellCheck: 'false';
'aria-autocomplete': 'none' | 'inline' | 'list' | 'both';
'aria-activedescendant': string | undefined;
Expand Down

0 comments on commit 7ff2971

Please sign in to comment.