Skip to content

Commit

Permalink
feat(plugins): provide state to transformSource (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Apr 8, 2021
1 parent 5f1f92e commit eaa2026
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/recently-viewed-items/recentlyViewedItemsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export function createLocalStorageRecentlyViewedItems<

return search(params);
},
transformSource({ source, onRemove }) {
transformSource({ source, onRemove, state }) {
const transformedSource = params.transformSource
? params.transformSource({ source, onRemove })
? params.transformSource({ source, onRemove, state })
: source;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type CreateQuerySuggestionsPluginParams<
getSearchParams?(params: { state: AutocompleteState<TItem> }): SearchOptions;
transformSource?(params: {
source: AutocompleteSource<TItem>;
state: AutocompleteState<TItem>;
onTapAhead(item: TItem): void;
}): AutocompleteSource<TItem>;
/**
Expand Down Expand Up @@ -115,6 +116,7 @@ export function createQuerySuggestionsPlugin<
templates: getTemplates({ onTapAhead }),
},
onTapAhead,
state,
}),
];
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AutocompletePlugin,
AutocompleteState,
PluginSubscribeParams,
} from '@algolia/autocomplete-core';
import { AutocompleteSource } from '@algolia/autocomplete-js';
Expand All @@ -21,6 +22,7 @@ export type CreateRecentSearchesPluginParams<
storage: Storage<TItem>;
transformSource?(params: {
source: AutocompleteSource<TItem>;
state: AutocompleteState<TItem>;
onRemove(id: string): void;
onTapAhead(item: TItem): void;
}): AutocompleteSource<TItem>;
Expand Down Expand Up @@ -70,7 +72,7 @@ export function createRecentSearchesPlugin<TItem extends RecentSearchesItem>({
store.addItem(recentItem as TItem);
}
},
getSources({ query, setQuery, refresh }) {
getSources({ query, setQuery, refresh, state }) {
lastItemsRef.current = store.getAll(query);

function onRemove(id: string) {
Expand Down Expand Up @@ -102,6 +104,7 @@ export function createRecentSearchesPlugin<TItem extends RecentSearchesItem>({
},
onRemove,
onTapAhead,
state,
}),
];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function search({ query, items, limit }) {

### `transformSource`

> `(params: { source: AutocompleteSource, onRemove: () => void, onTapAhead: () => void })`
> `(params: { source: AutocompleteSource, state: AutocompleteState, onRemove: () => void, onTapAhead: () => void })`
A function to transform the provided source.

Expand Down
2 changes: 1 addition & 1 deletion packages/website/docs/createQuerySuggestionsPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ A function returning [Algolia search parameters](https://www.algolia.com/doc/api

### `transformSource`

> `(params: { source: AutocompleteSource, onTapAhead: () => void })`
> `(params: { source: AutocompleteSource, state: AutocompleteState, onTapAhead: () => void })`
A function to transform the provided source.

Expand Down
2 changes: 1 addition & 1 deletion packages/website/docs/createRecentSearchesPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type RecentSearchesStorage<TItem extends RecentSearchesItem> = {

### `transformSource`

> `(params: { source: AutocompleteSource, onRemove: () => void, onTapAhead: () => void })`
> `(params: { source: AutocompleteSource, state: AutocompleteState, onRemove: () => void, onTapAhead: () => void })`
A function to transform the provided source.

Expand Down

0 comments on commit eaa2026

Please sign in to comment.