Skip to content

Commit

Permalink
feat(core): run onSelect on item click
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Sep 29, 2020
1 parent 59ee58b commit 445c7f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 51 deletions.
69 changes: 30 additions & 39 deletions packages/autocomplete-core/src/getPropGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
GetMenuProps,
GetRootProps,
} from './types';
import { getHighlightedItem, isOrContainsNode, isSpecialClick } from './utils';
import { getHighlightedItem, isOrContainsNode } from './utils';

interface GetPropGettersOptions<TItem> extends AutocompleteSetters<TItem> {
store: AutocompleteStore<TItem>;
Expand Down Expand Up @@ -331,53 +331,44 @@ export function getPropGetters<TItem, TEvent, TMouseEvent, TKeyboardEvent>({
((event as unknown) as MouseEvent).preventDefault();
},
onClick(event) {
const inputValue = source.getInputValue({
suggestion: item,
state: store.getState(),
});
const suggestionUrl = source.getSuggestionUrl({
suggestion: item,
state: store.getState(),
});

// If `getSuggestionUrl` is provided, it means that the suggestion
// is a link, not plain text that aims at updating the query.
// We can therefore skip the state change because it will update
// the `highlightedIndex`, resulting in a UI flash, especially
// noticeable on mobile.
if (
source.getSuggestionUrl({
suggestion: item,
state: store.getState(),
}) !== undefined
) {
return;
}

// We ignore all modified clicks to support default browsers' behavior.
if (isSpecialClick((event as unknown) as MouseEvent)) {
return;
}
const runPreCommand = suggestionUrl
? Promise.resolve()
: onInput({
query: inputValue,
event,
store,
props,
setHighlightedIndex,
setQuery,
setSuggestions,
setIsOpen,
setStatus,
setContext,
refresh,
nextState: {
isOpen: false,
},
});

const inputValue = source.getInputValue({
suggestion: item,
state: store.getState(),
});

onInput({
query: inputValue,
event,
store,
props,
setHighlightedIndex,
setQuery,
setSuggestions,
setIsOpen,
setStatus,
setContext,
nextState: {
isOpen: false,
},
refresh,
}).then(() => {
runPreCommand.then(() => {
source.onSelect({
suggestion: item,
suggestionValue: inputValue,
suggestionUrl: source.getSuggestionUrl({
suggestion: item,
state: store.getState(),
}),
suggestionUrl,
source,
state: store.getState(),
setHighlightedIndex,
Expand Down
12 changes: 0 additions & 12 deletions packages/autocomplete-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ export function getItemsCount(state: AutocompleteState<any>) {
);
}

export function isSpecialClick(event: MouseEvent): boolean {
const isMiddleClick = event.button === 1;

return (
isMiddleClick ||
event.altKey ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey
);
}

function normalizeSource<TItem>(
source: AutocompleteSource<TItem>
): InternalAutocompleteSource<TItem> {
Expand Down

0 comments on commit 445c7f8

Please sign in to comment.