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 }) => (
{hits.map((hit) => (
{hit.image && (
)}
- ${hit.price} - {hit.rating} stars
))}
); 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 ? ( ) : null; const footer = linkedStoryGroup ? (
{playgroundLink}
View source code
) : null; const results = resultsView ? (
{resultsView}
) : null; return (
{children}
{results} {footer}
); } 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 || ; const searchParameters = { hitsPerPage: 3, ...askedSearchParameters, }; const [searchState, setSearchState] = useState(initialSearchState); const setNextSearchState = (nextSearchState) => { setSearchState(nextSearchState); onSearchStateChange(nextSearchState); }; return (
{searchBox ? ( ) : null}
{hits}
{pagination ? : null}
} > {children}
); } 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, };