File size: 4,586 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import { Hit as AlgoliaHit } from 'instantsearch.js';
import algoliasearch from 'algoliasearch/lite';
import React from 'react';
import {
InstantSearch,
Breadcrumb,
Configure,
ClearRefinements,
CurrentRefinements,
DynamicWidgets,
HierarchicalMenu,
Highlight,
Hits,
HitsPerPage,
InfiniteHits,
Menu,
Pagination,
RangeInput,
RefinementList,
PoweredBy,
SearchBox,
SortBy,
ToggleRefinement,
} from 'react-instantsearch-hooks-web';
import {
Panel,
QueryRuleContext,
QueryRuleCustomData,
Refresh,
} from './components';
import { Tab, Tabs } from './components/layout';
import './App.css';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
type HitProps = {
hit: AlgoliaHit<{
name: string;
price: number;
}>;
};
function Hit({ hit }: HitProps) {
return (
<>
<Highlight hit={hit} attribute="name" className="Hit-label" />
<span className="Hit-price">${hit.price}</span>
</>
);
}
export function App() {
return (
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
routing={true}
>
<Configure ruleContexts={[]} />
<div className="Container">
<div>
<DynamicWidgets>
<Panel header="Brands">
<RefinementList
attribute="brand"
searchable={true}
searchablePlaceholder="Search brands"
showMore={true}
/>
</Panel>
<Panel header="Categories">
<Menu attribute="categories" showMore={true} />
</Panel>
<Panel header="Hierarchy">
<HierarchicalMenu
attributes={[
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
'hierarchicalCategories.lvl2',
]}
showMore={true}
/>
</Panel>
<Panel header="Price">
<RangeInput attribute="price" />
</Panel>
<Panel header="Free Shipping">
<ToggleRefinement
attribute="free_shipping"
label="Free shipping"
/>
</Panel>
</DynamicWidgets>
</div>
<div className="Search">
<Breadcrumb
attributes={[
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
'hierarchicalCategories.lvl2',
]}
/>
<SearchBox placeholder="Search" autoFocus />
<div className="Search-header">
<PoweredBy />
<HitsPerPage
items={[
{ label: '20 hits per page', value: 20, default: true },
{ label: '40 hits per page', value: 40 },
]}
/>
<SortBy
items={[
{ label: 'Relevance', value: 'instant_search' },
{ label: 'Price (asc)', value: 'instant_search_price_asc' },
{ label: 'Price (desc)', value: 'instant_search_price_desc' },
]}
/>
<Refresh />
</div>
<div className="CurrentRefinements">
<ClearRefinements />
<CurrentRefinements
transformItems={(items) =>
items.map((item) => {
const label = item.label.startsWith('hierarchicalCategories')
? 'Hierarchy'
: item.label;
return {
...item,
attribute: label,
};
})
}
/>
</div>
<QueryRuleContext
trackedFilters={{
brand: () => ['Apple'],
}}
/>
<QueryRuleCustomData>
{({ items }) => (
<>
{items.map((item) => (
<a href={item.link} key={item.banner}>
<img src={item.banner} alt={item.title} />
</a>
))}
</>
)}
</QueryRuleCustomData>
<Tabs>
<Tab title="Hits">
<Hits hitComponent={Hit} />
<Pagination className="Pagination" />
</Tab>
<Tab title="InfiniteHits">
<InfiniteHits showPrevious hitComponent={Hit} />
</Tab>
</Tabs>
</div>
</div>
</InstantSearch>
);
}
|