Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): rename private and public methods and properties #349

Merged
merged 13 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
12 changes: 7 additions & 5 deletions examples/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
autocomplete,
AutocompleteSource,
getAlgoliaHits,
reverseHighlightItem,
reverseHighlightHit,
} from '@algolia/autocomplete-js';
import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
import '@algolia/autocomplete-plugin-recent-searches/style';
Expand Down Expand Up @@ -40,17 +40,19 @@ autocomplete<Hit<QuerySuggestionHit>>({
}).then(([hits]) => {
return [
{
getInputValue: ({ suggestion }) => suggestion.query,
getSuggestions() {
getItemInputValue({ item }) {
return item.query;
},
getItems() {
return hits;
},
templates: {
item({ item }) {
return `
<div class="item-icon">${searchIcon}</div>
<div>
${reverseHighlightItem({
item,
${reverseHighlightHit({
Copy link
Contributor

Choose a reason for hiding this comment

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

what do we win by calling this a hit, while we talk about item in the other spots? Is it because it's called getAlgoliaHits? Could that maybe be called getAlgoliaItems or search to remove the word hits?

Copy link
Member Author

Choose a reason for hiding this comment

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

An item is a generic object manipulated by Autocomplete. A hit is an item transformed by Algolia which contains _highlightResult, etc. A hit is a search term and it reflects the Algolia brand.

hit: item,
attribute: 'query',
})}
</div>
Expand Down
30 changes: 15 additions & 15 deletions packages/autocomplete-core/src/__tests__/autocomplete.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { createAutocomplete } from '..';

function createSuggestion(items = []) {
function createCollection(items = []) {
return {
source: {
getInputValue: ({ suggestion }) => suggestion.label,
getSuggestionUrl: () => undefined,
getItemInputValue: ({ item }) => item.label,
getItemUrl: () => undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a good one, was inconsistent for sure!

onHighlight: () => {},
onSelect: () => {},
getSuggestions: () => items,
getItems: () => items,
},
items,
};
}

describe('createAutocomplete', () => {
test('setHighlightedIndex', () => {
test('setSelectedItemId', () => {
const onStateChange = jest.fn();
const { setHighlightedIndex } = createAutocomplete({
const { setSelectedItemId } = createAutocomplete({
getSources: () => [],
onStateChange,
});

setHighlightedIndex(1);
setSelectedItemId(1);

expect(onStateChange).toHaveBeenCalledTimes(1);
expect(onStateChange).toHaveBeenCalledWith(
expect.objectContaining({
state: expect.objectContaining({ highlightedIndex: 1 }),
state: expect.objectContaining({ selectedItemId: 1 }),
})
);

setHighlightedIndex(null);
setSelectedItemId(null);

expect(onStateChange).toHaveBeenCalledTimes(2);
expect(onStateChange).toHaveBeenCalledWith(
expect.objectContaining({
state: expect.objectContaining({ highlightedIndex: null }),
state: expect.objectContaining({ selectedItemId: null }),
})
);
});
Expand All @@ -57,20 +57,20 @@ describe('createAutocomplete', () => {
);
});

test('setSuggestions', () => {
test('setCollections', () => {
const onStateChange = jest.fn();
const { setSuggestions } = createAutocomplete({
const { setCollections } = createAutocomplete({
getSources: () => [],
onStateChange,
});
const suggestions = [createSuggestion()];
const collections = [createCollection()];

setSuggestions(suggestions);
setCollections(collections);

expect(onStateChange).toHaveBeenCalledTimes(1);
expect(onStateChange).toHaveBeenCalledWith(
expect.objectContaining({
state: expect.objectContaining({ suggestions }),
state: expect.objectContaining({ collections }),
})
);
});
Expand Down
16 changes: 8 additions & 8 deletions packages/autocomplete-core/src/createAutocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export function createAutocomplete<
const store = createStore(stateReducer, props, [completionStateEnhancer]);

const {
setHighlightedIndex,
setSelectedItemId,
setQuery,
setSuggestions,
setCollections,
setIsOpen,
setStatus,
setContext,
Expand All @@ -38,9 +38,9 @@ export function createAutocomplete<
} = getPropGetters<TItem, TEvent, TMouseEvent, TKeyboardEvent>({
store,
props,
setHighlightedIndex,
setSelectedItemId,
setQuery,
setSuggestions,
setCollections,
setIsOpen,
setStatus,
setContext,
Expand All @@ -53,9 +53,9 @@ export function createAutocomplete<
event: new Event('input'),
store,
props,
setHighlightedIndex,
setSelectedItemId,
setQuery,
setSuggestions,
setCollections,
setIsOpen,
setStatus,
setContext,
Expand All @@ -67,9 +67,9 @@ export function createAutocomplete<
}

return {
setHighlightedIndex,
setSelectedItemId,
setQuery,
setSuggestions,
setCollections,
setIsOpen,
setStatus,
setContext,
Expand Down
18 changes: 9 additions & 9 deletions packages/autocomplete-core/src/getAutocompleteSetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ interface GetAutocompleteSettersOptions<TItem> {
export function getAutocompleteSetters<TItem>({
store,
}: GetAutocompleteSettersOptions<TItem>) {
const setHighlightedIndex: AutocompleteApi<TItem>['setHighlightedIndex'] = (
const setSelectedItemId: AutocompleteApi<TItem>['setSelectedItemId'] = (
value
) => {
store.send('setHighlightedIndex', value);
store.send('setSelectedItemId', value);
};

const setQuery: AutocompleteApi<TItem>['setQuery'] = (value) => {
store.send('setQuery', value);
};

const setSuggestions: AutocompleteApi<TItem>['setSuggestions'] = (
const setCollections: AutocompleteApi<TItem>['setCollections'] = (
rawValue
) => {
let baseItemId = 0;
const value = rawValue.map((suggestion) => ({
...suggestion,
items: suggestion.items.map((item) => ({
const value = rawValue.map((collection) => ({
...collection,
items: collection.items.map((item) => ({
...item,
__autocomplete_id: baseItemId++,
})),
}));

store.send('setSuggestions', value);
store.send('setCollections', value);
};

const setIsOpen: AutocompleteApi<TItem>['setIsOpen'] = (value) => {
Expand All @@ -45,9 +45,9 @@ export function getAutocompleteSetters<TItem>({
};

return {
setHighlightedIndex,
setSelectedItemId,
setQuery,
setSuggestions,
setCollections,
Copy link
Contributor

Choose a reason for hiding this comment

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

collection is a list of items internally, while it's still called getSuggestions outside (or do I misread it?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, collections remain internal, except if you use the setters. getSuggestions is renamed to getItems, and the result of getItems is the items objet in a collection.

type Collection = { source, items };

setIsOpen,
setStatus,
setContext,
Expand Down
18 changes: 10 additions & 8 deletions packages/autocomplete-core/src/getCompletion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InternalAutocompleteOptions, AutocompleteState } from './types';
import { getHighlightedItem } from './utils';
import { getSelectedItem } from './utils';

interface GetCompletionProps<TItem> {
state: AutocompleteState<TItem>;
Expand All @@ -13,28 +13,30 @@ export function getCompletion<TItem>({
if (
props.enableCompletion === false ||
state.isOpen === false ||
state.highlightedIndex === null ||
state.selectedItemId === null ||
state.status === 'stalled'
) {
return null;
}

const { itemValue } = getHighlightedItem({ state })!;
const { itemInputValue } = getSelectedItem({ state })!;

// The completion should appear only if the _first_ characters of the query
// match with the suggestion.
// match with the item.
if (
state.query.length > 0 &&
itemValue.toLocaleLowerCase().indexOf(state.query.toLocaleLowerCase()) === 0
itemInputValue
.toLocaleLowerCase()
.indexOf(state.query.toLocaleLowerCase()) === 0
) {
// If the query typed has a different case than the suggestion, we want
// If the query typed has a different case than the item, we want
// to show the completion matching the case of the query. This makes both
// strings overlap correctly.
// Example:
// - query: 'Gui'
// - suggestion: 'guitar'
// - item: 'guitar'
// => completion: 'Guitar'
const completion = state.query + itemValue.slice(state.query.length);
const completion = state.query + itemInputValue.slice(state.query.length);

if (completion === state.query) {
return null;
Expand Down
22 changes: 9 additions & 13 deletions packages/autocomplete-core/src/getDefaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getDefaultProps<TItem>(
openOnFocus: false,
placeholder: '',
autoFocus: false,
defaultHighlightedIndex: null,
defaultSelectedItemId: null,
enableCompletion: false,
stallThreshold: 300,
environment,
Expand All @@ -31,10 +31,10 @@ export function getDefaultProps<TItem>(
id: props.id ?? generateAutocompleteId(),
// The following props need to be deeply defaulted.
initialState: {
highlightedIndex: null,
selectedItemId: null,
query: '',
completion: null,
suggestions: [],
collections: [],
isOpen: false,
status: 'idle',
statusContext: {},
Expand Down Expand Up @@ -83,22 +83,18 @@ export function getDefaultProps<TItem>(
);
},
navigator: {
navigate({ suggestionUrl }) {
environment.location.assign(suggestionUrl);
navigate({ itemUrl }) {
environment.location.assign(itemUrl);
},
navigateNewTab({ suggestionUrl }) {
const windowReference = environment.open(
suggestionUrl,
'_blank',
'noopener'
);
navigateNewTab({ itemUrl }) {
const windowReference = environment.open(itemUrl, '_blank', 'noopener');

if (windowReference) {
windowReference.focus();
}
},
navigateNewWindow({ suggestionUrl }) {
environment.open(suggestionUrl, '_blank', 'noopener');
navigateNewWindow({ itemUrl }) {
environment.open(itemUrl, '_blank', 'noopener');
},
...props.navigator,
},
Expand Down
Loading