Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
feat(docsearch): introduce resultsFooterComponent option
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Jun 5, 2020
1 parent 36af642 commit b613bb2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createAutocomplete,
AutocompleteState,
} from '@francoischalifour/autocomplete-core';
import { getAlgoliaHits } from '@francoischalifour/autocomplete-preset-algolia';
import { getAlgoliaResults } from '@francoischalifour/autocomplete-preset-algolia';

import { MAX_QUERY_SIZE } from './constants';
import {
Expand Down Expand Up @@ -35,6 +35,7 @@ export function DocSearchModal({
onClose = noop,
transformItems = (x) => x,
hitComponent = Hit,
resultsFooterComponent = () => null,
navigator,
}: DocSearchModalProps) {
const [state, setState] = React.useState<
Expand Down Expand Up @@ -144,7 +145,7 @@ export function DocSearchModal({
];
}

return getAlgoliaHits({
return getAlgoliaResults({
searchClient,
queries: [
{
Expand Down Expand Up @@ -193,7 +194,9 @@ export function DocSearchModal({

throw error;
})
.then((hits: DocSearchHit[]) => {
.then((results) => {
const hits: DocSearchHit[] = results[0].hits;
const nbHits: number = results[0].nbHits;
const sources = groupBy(hits, (hit) => hit.hierarchy.lvl0);

// We store the `lvl0`s to display them as search suggestions
Expand All @@ -207,6 +210,8 @@ export function DocSearchModal({
});
}

setContext({ nbHits });

return Object.values<DocSearchHit[]>(sources).map((items) => {
return {
onSelect({ suggestion }) {
Expand Down Expand Up @@ -339,6 +344,7 @@ export function DocSearchModal({
{...autocomplete}
state={state}
hitComponent={hitComponent}
resultsFooterComponent={resultsFooterComponent}
recentSearches={recentSearches}
favoriteSearches={favoriteSearches}
onItemClick={(item) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/docsearch-react/src/ResultsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ interface ResultsScreenProps
React.KeyboardEvent
> {
state: AutocompleteState<InternalDocSearchHit>;
resultsFooterComponent(props: {
state: AutocompleteState<InternalDocSearchHit>;
}): JSX.Element;
onItemClick(item: InternalDocSearchHit): void;
}

Expand Down Expand Up @@ -69,6 +72,10 @@ export function ResultsScreen(props: ResultsScreenProps) {
/>
);
})}

<section className="DocSearch-HitsFooter">
<props.resultsFooterComponent state={props.state} />
</section>
</div>
);
}
13 changes: 13 additions & 0 deletions packages/docsearch-react/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ html[data-theme='dark'] {
color: var(--docsearch-highlight-color);
}

.DocSearch-HitsFooter {
margin-bottom: var(--docsearch-spacing);
padding: var(--docsearch-spacing);
color: var(--docsearch-muted-color);
display: flex;
justify-content: center;
}

.DocSearch-HitsFooter a {
color: inherit;
border-bottom: 1px solid;
}

.DocSearch-Hit {
display: flex;
position: relative;
Expand Down

0 comments on commit b613bb2

Please sign in to comment.