Skip to content

Commit 23cff13

Browse files
feat(js): type highlight utils
1 parent 9f4b6bd commit 23cff13

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

examples/js/app.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { Hit } from '@algolia/client-search';
12
import algoliasearch from 'algoliasearch/lite';
23
import {
34
autocomplete,
5+
AutocompleteSource,
46
getAlgoliaHits,
57
reverseHighlightItem,
68
} from '@algolia/autocomplete-js';
@@ -12,7 +14,7 @@ const searchClient = algoliasearch(
1214

1315
type QuerySuggestionHit = { query: string };
1416

15-
autocomplete<QuerySuggestionHit>({
17+
autocomplete<Hit<QuerySuggestionHit>>({
1618
container: '#autocomplete',
1719
debug: true,
1820
// dropdownPlacement: 'start',
@@ -40,15 +42,15 @@ autocomplete<QuerySuggestionHit>({
4042
return `
4143
<div class="item-icon">${searchIcon}</div>
4244
<div>
43-
${reverseHighlightItem<QuerySuggestionHit>({
45+
${reverseHighlightItem({
4446
item,
4547
attribute: 'query',
4648
})}
4749
</div>
4850
`;
4951
},
5052
},
51-
},
53+
} as AutocompleteSource<Hit<QuerySuggestionHit>>,
5254
];
5355
});
5456
},

examples/js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"start": "parcel index.html"
1111
},
1212
"dependencies": {
13+
"@algolia/client-search": "4.5.1",
1314
"@algolia/autocomplete-js": "^1.0.0-alpha.29",
1415
"algoliasearch": "4.5.1"
1516
},

packages/autocomplete-js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dist/"
3232
],
3333
"dependencies": {
34+
"@algolia/client-search": "4.5.1",
3435
"@algolia/autocomplete-core": "^1.0.0-alpha.29",
3536
"@algolia/autocomplete-preset-algolia": "^1.0.0-alpha.29"
3637
}

packages/autocomplete-js/src/autocomplete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { setProperties, setPropertiesWithoutEvents } from './setProperties';
1313
import {
1414
AutocompleteOptions,
1515
AutocompleteApi,
16-
AutocompleteSource,
16+
InternalAutocompleteSource,
1717
} from './types';
1818

1919
function defaultRender({ root, sections }) {
@@ -146,7 +146,7 @@ export function autocomplete<TItem>({
146146

147147
const sections = state.suggestions.map((suggestion) => {
148148
const items = suggestion.items;
149-
const source = suggestion.source as AutocompleteSource<TItem>;
149+
const source = suggestion.source as InternalAutocompleteSource<TItem>;
150150

151151
const section = document.createElement('section');
152152
setProperties(section, {

packages/autocomplete-js/src/highlight.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import {
44
parseAlgoliaHitSnippet,
55
parseAlgoliaHitReverseSnippet,
66
} from '@algolia/autocomplete-preset-algolia';
7+
import { Hit } from '@algolia/client-search';
78

8-
type ParsedAttribute = { value: string; isHighlighted: boolean };
9+
type ParsedAttribute = {
10+
value: string;
11+
isHighlighted: boolean;
12+
};
913

1014
function concatParts(
1115
parts: ParsedAttribute[],
@@ -32,7 +36,7 @@ type HighlightItemParams<TItem> = {
3236
/**
3337
* Highlights and escapes the matching parts of an Algolia hit.
3438
*/
35-
export function highlightItem<TItem extends object>({
39+
export function highlightItem<TItem extends Hit<{}>>({
3640
item,
3741
attribute,
3842
highlightPreTag = '<mark>',
@@ -54,7 +58,7 @@ export function highlightItem<TItem extends object>({
5458
*
5559
* This is a common pattern for Query Suggestions.
5660
*/
57-
export function reverseHighlightItem<TItem extends object>({
61+
export function reverseHighlightItem<TItem extends Hit<{}>>({
5862
item,
5963
attribute,
6064
highlightPreTag = '<mark>',
@@ -74,7 +78,7 @@ export function reverseHighlightItem<TItem extends object>({
7478
/**
7579
* Highlights and escapes the matching parts of an Algolia hit snippet.
7680
*/
77-
export function snippetItem<TItem extends object>({
81+
export function snippetItem<TItem extends Hit<{}>>({
7882
item,
7983
attribute,
8084
highlightPreTag = '<mark>',
@@ -96,7 +100,7 @@ export function snippetItem<TItem extends object>({
96100
*
97101
* This is a common pattern for Query Suggestions.
98102
*/
99-
export function reverseSnippetItem<TItem extends object>({
103+
export function reverseSnippetItem<TItem extends Hit<{}>>({
100104
item,
101105
attribute,
102106
highlightPreTag = '<mark>',

packages/autocomplete-js/src/types/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99

1010
type Template<TParams> = (params: TParams) => string | void;
1111

12-
export type AutocompleteSource<TItem> = InternalAutocompleteCoreSource<
13-
TItem
14-
> & {
12+
type SourceTemplates<TItem> = {
1513
/**
1614
* Templates to display in the autocomplete dropdown.
1715
*
@@ -43,6 +41,14 @@ export type AutocompleteSource<TItem> = InternalAutocompleteCoreSource<
4341
};
4442
};
4543

44+
export type AutocompleteSource<TItem> = AutocompleteCoreSource<TItem> &
45+
SourceTemplates<TItem>;
46+
47+
export type InternalAutocompleteSource<TItem> = InternalAutocompleteCoreSource<
48+
TItem
49+
> &
50+
SourceTemplates<TItem>;
51+
4652
type GetSources<TItem> = (
4753
params: GetSourcesParams<TItem>
4854
) =>

0 commit comments

Comments
 (0)