File size: 4,955 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import React, { useState, useMemo } from 'react';
import PropTypes from 'prop-types';
import { linkTo } from '@storybook/addon-links';
import algoliasearch from 'algoliasearch/lite';
import {
InstantSearch,
ClearRefinements,
SearchBox,
Pagination,
Highlight,
Configure,
connectHits,
} from 'react-instantsearch-dom';
import 'instantsearch.css/themes/algolia.css';
const Hits = ({ hits }) => (
<div className="hits">
{hits.map((hit) => (
<div key={hit.objectID} className="hit">
{hit.image && (
<div className="hit-picture">
<img
src={`https://res.cloudinary.com/hilnmyskv/image/fetch/h_100,q_100,f_auto/${hit.image}`}
/>
</div>
)}
<div className="hit-content">
<div>
<Highlight attribute="name" hit={hit} />
<span> - ${hit.price}</span>
<span> - {hit.rating} stars</span>
</div>
<div className="hit-type">
<Highlight attribute="type" hit={hit} />
</div>
<div className="hit-description">
<Highlight attribute="description" hit={hit} />
</div>
</div>
</div>
))}
</div>
);
Hits.propTypes = {
hits: PropTypes.array.isRequired,
};
export const CustomHits = connectHits(Hits);
export function Content({
hasPlayground,
linkedStoryGroup,
children,
resultsView,
}) {
const sourceCodeUrl = `https://github.com/algolia/react-instantsearch/tree/master/stories/${linkedStoryGroup}`;
const playgroundLink = hasPlayground ? (
<button
onClick={linkTo(linkedStoryGroup, 'playground')}
className="playground-url"
>
<span>Play with props</span>
</button>
) : null;
const footer = linkedStoryGroup ? (
<div className="footer-container">
{playgroundLink}
<a
href={sourceCodeUrl}
className="source-code-url"
target="_blank"
rel="noopener noreferrer"
>
<div>View source code</div>
</a>
</div>
) : null;
const results = resultsView ? (
<div
style={linkedStoryGroup ? {} : { borderRadius: '0px 0px 5px 5px' }}
className="container hits-container"
>
{resultsView}
</div>
) : null;
return (
<div>
<div className="container widget-container">{children}</div>
{results}
{footer}
</div>
);
}
Content.propTypes = {
linkedStoryGroup: PropTypes.string,
hasPlayground: PropTypes.bool,
children: PropTypes.node,
resultsView: PropTypes.node,
};
export function WrapWithHits({
searchParameters: askedSearchParameters = {},
children,
searchBox = true,
hasPlayground = false,
linkedStoryGroup,
pagination = true,
hitsElement,
appId,
apiKey,
indexName,
initialSearchState,
onSearchStateChange,
}) {
const searchClient = useMemo(() => {
return algoliasearch(appId, apiKey);
}, [appId, apiKey]);
const hits = hitsElement || <CustomHits />;
const searchParameters = {
hitsPerPage: 3,
...askedSearchParameters,
};
const [searchState, setSearchState] = useState(initialSearchState);
const setNextSearchState = (nextSearchState) => {
setSearchState(nextSearchState);
onSearchStateChange(nextSearchState);
};
return (
<InstantSearch
searchClient={searchClient}
indexName={indexName}
searchState={searchState}
onSearchStateChange={setNextSearchState}
>
<Configure {...searchParameters} />
<Content
linkedStoryGroup={linkedStoryGroup}
hasPlayground={hasPlayground}
resultsView={
<div>
<div className="hit-actions">
{searchBox ? (
<SearchBox
translations={{
placeholder: 'Search into our products: phones, tv...',
}}
/>
) : null}
<ClearRefinements translations={{ reset: 'Clear all filters' }} />
</div>
{hits}
<div className="hit-pagination">
{pagination ? <Pagination showLast={true} /> : null}
</div>
</div>
}
>
{children}
</Content>
</InstantSearch>
);
}
WrapWithHits.propTypes = {
appId: PropTypes.string,
apiKey: PropTypes.string,
indexName: PropTypes.string,
children: PropTypes.node,
searchBox: PropTypes.bool,
linkedStoryGroup: PropTypes.string,
hasPlayground: PropTypes.bool,
pagination: PropTypes.bool,
searchParameters: PropTypes.object,
hitsElement: PropTypes.element,
initialSearchState: PropTypes.object,
onSearchStateChange: PropTypes.func,
};
// defaultProps added so that they're displayed in the JSX addon
WrapWithHits.defaultProps = {
appId: 'latency',
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
indexName: 'instant_search',
initialSearchState: {},
onSearchStateChange: (searchState) => searchState,
};
|