Skip to content

Commit

Permalink
feat(js): change renderer implementation to virtual DOM (#381)
Browse files Browse the repository at this point in the history
* feat(js): change renderer implementation to virtual DOM

* feat(highlighting): revamp highlighting system to VDOM (#399)
  • Loading branch information
francoischalifour committed Feb 1, 2021
1 parent 9d25fc4 commit 2fc30e1
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 165 deletions.
66 changes: 12 additions & 54 deletions examples/js/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
import {
autocomplete,
getAlgoliaHits,
snippetHit,
highlightHit,
} from '@algolia/autocomplete-js';
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
import { Hit } from '@algolia/client-search';
import algoliasearch from 'algoliasearch';
import { h, Fragment } from 'preact';
import { h } from 'preact';
import insightsClient from 'search-insights';

import '@algolia/autocomplete-theme-classic';

import { shortcutsPlugin } from './shortcutsPlugin';

type Product = {
name: string;
image: string;
description: string;
};
type Product = { name: string; image: string };
type ProductHit = Hit<Product>;

const appId = 'latency';
Expand Down Expand Up @@ -50,7 +44,6 @@ autocomplete({
debug: true,
openOnFocus: true,
plugins: [
shortcutsPlugin,
algoliaInsightsPlugin,
recentSearchesPlugin,
querySuggestionsPlugin,
Expand All @@ -65,38 +58,16 @@ autocomplete({
getItems() {
return getAlgoliaHits<Product>({
searchClient,
queries: [
{
indexName: 'instant_search',
query,
params: {
clickAnalytics: true,
attributesToSnippet: ['name:10', 'description:35'],
snippetEllipsisText: '…',
},
},
],
queries: [{ indexName: 'instant_search', query }],
});
},
templates: {
header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Products</span>
<div className="aa-SourceHeaderLine"></div>
</Fragment>
);
},
item({ item }) {
return <ProductItem hit={item} />;
},
empty() {
return (
<div className="aa-ItemContent">
<div className="aa-ItemContentTitle">
No products for this query.
</div>
</div>
<div className="aa-ItemContent">No results for this query.</div>
);
},
},
Expand All @@ -111,27 +82,14 @@ type ProductItemProps = {

function ProductItem({ hit }: ProductItemProps) {
return (
<Fragment>
<div className="aa-ItemIcon">
<img src={hit.image} alt={hit.name} width="40" height="40" />
<div className="aa-ItemContent">
<div className="aa-ItemSourceIcon">
<img src={hit.image} alt={hit.name} width="20" height="20" />
</div>
<div className="aa-ItemContent">
<div className="aa-ItemContentTitle">
{snippetHit<ProductHit>({ hit, attribute: 'name' })}
</div>
<div className="aa-ItemContentDescription">
{snippetHit<ProductHit>({ hit, attribute: 'description' })}
</div>

<div className="aa-ItemTitle">
{highlightHit<ProductHit>({ hit, attribute: 'name' })}
</div>
<button
className="aa-ItemActionButton aa-TouchOnly aa-ActiveOnly"
type="button"
title="Select"
>
<svg fill="currentColor" viewBox="0 0 24 24" width="20" height="20">
<path d="M18.984 6.984h2.016v6h-15.188l3.609 3.609-1.406 1.406-6-6 6-6 1.406 1.406-3.609 3.609h13.172v-4.031z"></path>
</svg>
</button>
</Fragment>
</div>
);
}
103 changes: 0 additions & 103 deletions examples/js/createTagPlugin.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions examples/js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
</div>

<script src="env.ts"></script>
<<<<<<< HEAD
<script src="darkMode.ts"></script>
=======
>>>>>>> 7be2249c (feat(js): change renderer implementation to virtual DOM (#381))
<script src="app.tsx"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { fireEvent, waitFor } from '@testing-library/dom';

import { wait } from '../../../../test/utils';
import { autocomplete } from '../autocomplete';

describe('autocomplete-js', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function autocomplete<TItem extends BaseItem>(
);

const render =
(!getItemsCount(renderProps.state) &&
(!getItemsCount(state) &&
!hasEmptySourceTemplateRef.current &&
props.value.renderer.renderEmpty) ||
props.value.renderer.render;
Expand Down
Loading

0 comments on commit 2fc30e1

Please sign in to comment.