Skip to content

Commit

Permalink
feat(js): type highlight utils
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Sep 17, 2020
1 parent 9f4b6bd commit 23cff13
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
8 changes: 5 additions & 3 deletions examples/js/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Hit } from '@algolia/client-search';
import algoliasearch from 'algoliasearch/lite';
import {
autocomplete,
AutocompleteSource,
getAlgoliaHits,
reverseHighlightItem,
} from '@algolia/autocomplete-js';
Expand All @@ -12,7 +14,7 @@ const searchClient = algoliasearch(

type QuerySuggestionHit = { query: string };

autocomplete<QuerySuggestionHit>({
autocomplete<Hit<QuerySuggestionHit>>({
container: '#autocomplete',
debug: true,
// dropdownPlacement: 'start',
Expand Down Expand Up @@ -40,15 +42,15 @@ autocomplete<QuerySuggestionHit>({
return `
<div class="item-icon">${searchIcon}</div>
<div>
${reverseHighlightItem<QuerySuggestionHit>({
${reverseHighlightItem({
item,
attribute: 'query',
})}
</div>
`;
},
},
},
} as AutocompleteSource<Hit<QuerySuggestionHit>>,
];
});
},
Expand Down
1 change: 1 addition & 0 deletions examples/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"start": "parcel index.html"
},
"dependencies": {
"@algolia/client-search": "4.5.1",
"@algolia/autocomplete-js": "^1.0.0-alpha.29",
"algoliasearch": "4.5.1"
},
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dist/"
],
"dependencies": {
"@algolia/client-search": "4.5.1",
"@algolia/autocomplete-core": "^1.0.0-alpha.29",
"@algolia/autocomplete-preset-algolia": "^1.0.0-alpha.29"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { setProperties, setPropertiesWithoutEvents } from './setProperties';
import {
AutocompleteOptions,
AutocompleteApi,
AutocompleteSource,
InternalAutocompleteSource,
} from './types';

function defaultRender({ root, sections }) {
Expand Down Expand Up @@ -146,7 +146,7 @@ export function autocomplete<TItem>({

const sections = state.suggestions.map((suggestion) => {
const items = suggestion.items;
const source = suggestion.source as AutocompleteSource<TItem>;
const source = suggestion.source as InternalAutocompleteSource<TItem>;

const section = document.createElement('section');
setProperties(section, {
Expand Down
14 changes: 9 additions & 5 deletions packages/autocomplete-js/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
parseAlgoliaHitSnippet,
parseAlgoliaHitReverseSnippet,
} from '@algolia/autocomplete-preset-algolia';
import { Hit } from '@algolia/client-search';

type ParsedAttribute = { value: string; isHighlighted: boolean };
type ParsedAttribute = {
value: string;
isHighlighted: boolean;
};

function concatParts(
parts: ParsedAttribute[],
Expand All @@ -32,7 +36,7 @@ type HighlightItemParams<TItem> = {
/**
* Highlights and escapes the matching parts of an Algolia hit.
*/
export function highlightItem<TItem extends object>({
export function highlightItem<TItem extends Hit<{}>>({
item,
attribute,
highlightPreTag = '<mark>',
Expand All @@ -54,7 +58,7 @@ export function highlightItem<TItem extends object>({
*
* This is a common pattern for Query Suggestions.
*/
export function reverseHighlightItem<TItem extends object>({
export function reverseHighlightItem<TItem extends Hit<{}>>({
item,
attribute,
highlightPreTag = '<mark>',
Expand All @@ -74,7 +78,7 @@ export function reverseHighlightItem<TItem extends object>({
/**
* Highlights and escapes the matching parts of an Algolia hit snippet.
*/
export function snippetItem<TItem extends object>({
export function snippetItem<TItem extends Hit<{}>>({
item,
attribute,
highlightPreTag = '<mark>',
Expand All @@ -96,7 +100,7 @@ export function snippetItem<TItem extends object>({
*
* This is a common pattern for Query Suggestions.
*/
export function reverseSnippetItem<TItem extends object>({
export function reverseSnippetItem<TItem extends Hit<{}>>({
item,
attribute,
highlightPreTag = '<mark>',
Expand Down
12 changes: 9 additions & 3 deletions packages/autocomplete-js/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {

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

export type AutocompleteSource<TItem> = InternalAutocompleteCoreSource<
TItem
> & {
type SourceTemplates<TItem> = {
/**
* Templates to display in the autocomplete dropdown.
*
Expand Down Expand Up @@ -43,6 +41,14 @@ export type AutocompleteSource<TItem> = InternalAutocompleteCoreSource<
};
};

export type AutocompleteSource<TItem> = AutocompleteCoreSource<TItem> &
SourceTemplates<TItem>;

export type InternalAutocompleteSource<TItem> = InternalAutocompleteCoreSource<
TItem
> &
SourceTemplates<TItem>;

type GetSources<TItem> = (
params: GetSourcesParams<TItem>
) =>
Expand Down

0 comments on commit 23cff13

Please sign in to comment.