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
LocalStorageRecentSearchesPlugin not working with other hooks than onSubmit #1030
Comments
This is a workaround written in plain ES6+, which works for me at the moment: import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches'
import { createLocalStorage } from '@algolia/autocomplete-plugin-recent-searches/dist/esm/createLocalStorage'
import { LOCAL_STORAGE_KEY } from '@algolia/autocomplete-plugin-recent-searches/dist/esm/constants'
const storage = createLocalStorage({
key: `${LOCAL_STORAGE_KEY}:my_search`,
limit: 5
})
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
key: 'my_search',
subscribe: ({ onSelect }) => {
onSelect(({ item }) => {
storage.onRemove(item.id)
storage.onAdd(item)
})
}
})
recentSearchesPlugin.storage = storage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
You can’t use
LocalStorageRecentSearchesPlugin
with autocomplete optiondefaultActiveItemId
(e.g.defaultActiveItemId: 0
), becauseonSubmit
lifecycle hook is not fired by autocomplete in this case.LocalStorageRecentSearchesPlugin
is based on this hook. It would be good to have the choice, which lifecycle hooks trigger the plugin. In practice alsoonSelect
is needed, but it’s currently only forquerySuggestionsPlugin
implemented:autocomplete/packages/autocomplete-plugin-recent-searches/src/createRecentSearchesPlugin.ts
Line 57 in dc8b6f2
PS: It's unclear, whether incompatibility with
defaultActiveItemId
is intended behavior or a bug. At least it's very confusing and not mentioned anywhere in the docs.Expected behavior
LocalStorageRecentSearchesPlugin
should handleonSubmit
andonSelect
hook. At least, it should be mentioned in the docs, if it's not possible.Environment
The text was updated successfully, but these errors were encountered: