-
Notifications
You must be signed in to change notification settings - Fork 332
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
Changes from 11 commits
becd01d
4f39454
7ae75b3
1fee372
d4bba2a
70fcebd
d23e7bc
ddfbf18
f59f74b
83a0e84
ab67a96
e162b51
9922243
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }), | ||
}) | ||
); | ||
}); | ||
|
@@ -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 }), | ||
}) | ||
); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
|
@@ -45,9 +45,9 @@ export function getAutocompleteSetters<TItem>({ | |
}; | ||
|
||
return { | ||
setHighlightedIndex, | ||
setSelectedItemId, | ||
setQuery, | ||
setSuggestions, | ||
setCollections, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, collections remain internal, except if you use the setters. type Collection = { source, items }; |
||
setIsOpen, | ||
setStatus, | ||
setContext, | ||
|
There was a problem hiding this comment.
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
orsearch
to remove the word hits?There was a problem hiding this comment.
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.