Skip to content

Commit 24ec148

Browse files
docs: update AutocompleteExample
1 parent 9f0b494 commit 24ec148

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

packages/website/docs/getting-started.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Check out how the template displays items by searching in the input below:
217217
getSources={({ query }) => {
218218
return [
219219
{
220+
sourceId: 'docsearch',
220221
getItems() {
221222
return getAlgoliaHits({
222223
searchClient,
@@ -292,6 +293,7 @@ Now give it a try: navigate to one of the items using your keyboard and hitting
292293
getSources={({ query }) => {
293294
return [
294295
{
296+
sourceId: 'docsearch',
295297
getItems() {
296298
return getAlgoliaHits({
297299
searchClient,

packages/website/docs/introduction.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ For example, try typing the letter "s" in the search box below.
2121
getSources={({ query }) => {
2222
return [
2323
{
24+
sourceId: 'docsearch',
2425
getItems() {
2526
return getAlgoliaHits({
2627
searchClient,
@@ -29,10 +30,21 @@ For example, try typing the letter "s" in the search box below.
2930
indexName: 'autocomplete',
3031
query,
3132
params: {
32-
hitsPerPage: 5
33-
}
34-
}
35-
]
33+
hitsPerPage: 5,
34+
attributesToSnippet: [
35+
'content:10',
36+
'hierarchy.lvl0:10',
37+
'hierarchy.lvl1:10',
38+
'hierarchy.lvl2:10',
39+
'hierarchy.lvl3:10',
40+
'hierarchy.lvl4:10',
41+
'hierarchy.lvl5:10',
42+
'hierarchy.lvl6:10',
43+
],
44+
snippetEllipsisText: '…',
45+
},
46+
},
47+
],
3648
});
3749
},
3850
getItemUrl({ item }) {
@@ -48,7 +60,7 @@ For example, try typing the letter "s" in the search box below.
4860
.slice(0, -1)}
4961
/>
5062
);
51-
}
63+
},
5264
},
5365
},
5466
];

packages/website/src/components/AutocompleteDocSearchItem.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { snippetHit } from '@algolia/autocomplete-js';
12
import { Hit } from '@algolia/client-search';
2-
import React from 'react';
3+
import React, { createElement } from 'react';
34

45
type DocSearchItem = {
56
content: string | null;
@@ -25,14 +26,20 @@ type DocSearchItem = {
2526
lvl6: string | null;
2627
};
2728
};
28-
type DocSearchHit = Hit<DocSearchItem>;
29-
type AutocompleteItemProps = { hit: DocSearchHit; breadcrumb: string[] };
29+
type AutocompleteItemProps = {
30+
hit: Hit<DocSearchItem>;
31+
breadcrumb: string[];
32+
};
3033

3134
export function AutocompleteDocSearchItem({
3235
hit,
3336
breadcrumb,
3437
}: AutocompleteItemProps) {
35-
const title = hit.type === 'content' ? hit.content : hit.hierarchy[hit.type];
38+
const title = snippetHit({
39+
hit,
40+
attribute: hit.type === 'content' ? 'content' : ['hierarchy', hit.type],
41+
createElement,
42+
});
3643

3744
return (
3845
<a href={hit.url} className="aa-ItemLink">

packages/website/src/components/AutocompleteExample.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { BaseItem } from '@algolia/autocomplete-core';
12
import { autocomplete, AutocompleteOptions } from '@algolia/autocomplete-js';
23
import React, { createElement, Fragment, useEffect, useRef } from 'react';
34
import { render } from 'react-dom';
45

5-
export function AutocompleteExample<TItem extends {}>(
6+
export function AutocompleteExample<TItem extends BaseItem>(
67
props: Omit<AutocompleteOptions<TItem>, 'container'>
78
) {
89
const containerRef = useRef<HTMLDivElement>(null);
@@ -28,5 +29,10 @@ export function AutocompleteExample<TItem extends {}>(
2829
};
2930
}, [props]);
3031

31-
return <div ref={containerRef} style={{ padding: '1rem 0' }} />;
32+
return (
33+
<div
34+
ref={containerRef}
35+
style={{ marginBottom: 'var(--ifm-paragraph-margin-bottom)' }}
36+
/>
37+
);
3238
}

0 commit comments

Comments
 (0)