Skip to content

Commit

Permalink
feat(js): pass source to header and footer templates
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Dec 2, 2020
1 parent 8638a98 commit 9983c64
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
14 changes: 12 additions & 2 deletions packages/autocomplete-js/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export function render<TItem extends BaseItem>(
const headerElement = SourceHeader({ classNames });

renderTemplate({
template: source.templates.header({ root: headerElement, state }),
template: source.templates.header({
root: headerElement,
state,
source,
items,
}),
parent: sectionElement,
element: headerElement,
});
Expand Down Expand Up @@ -108,7 +113,12 @@ export function render<TItem extends BaseItem>(
});

renderTemplate({
template: source.templates.footer({ root: footerElement, state }),
template: source.templates.footer({
root: footerElement,
state,
source,
items,
}),
parent: sectionElement,
element: footerElement,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/autocomplete-js/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ export type SourceTemplates<TItem extends BaseItem> = {
header?: Template<{
root: HTMLElement;
state: AutocompleteState<TItem>;
source: AutocompleteSource<TItem>;
items: TItem[];
}>;
/**
* The template for the section footer.
*/
footer?: Template<{
root: HTMLElement;
state: AutocompleteState<TItem>;
source: AutocompleteSource<TItem>;
items: TItem[];
}>;
};

Expand Down
18 changes: 15 additions & 3 deletions packages/website/docs/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,18 @@ type SourceTemplate = {
item: TItem;
state: AutocompleteState<TItem>;
}>;
header?: Template<{ root: HTMLElement; state: AutocompleteState<TItem> }>;
footer?: Template<{ root: HTMLElement; state: AutocompleteState<TItem> }>;
header?: Template<{
root: HTMLElement;
state: AutocompleteState<TItem>;
source: AutocompleteSource;
items: TItem[];
}>;
footer?: Template<{
root: HTMLElement;
state: AutocompleteState<TItem>;
source: AutocompleteSource;
items: TItem[];
}>;
};
```

Expand All @@ -321,7 +331,9 @@ const autocompleteSearch = autocomplete({
getSources() {
return [
{
getItemInputValue: ({ item }) => item.query,
getItemInputValue({ item }) {
return item.query;
},
getItems({ query }) {
return getAlgoliaHits({
searchClient,
Expand Down

0 comments on commit 9983c64

Please sign in to comment.