File size: 4,201 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 | import React from 'react';
import { storiesOf } from '@storybook/react';
import { connectHits } from 'react-instantsearch-core';
import {
QueryRuleCustomData,
QueryRuleContext,
Highlight,
RefinementList,
} from 'react-instantsearch-dom';
import { WrapWithHits } from './util';
type CustomDataItem = {
title: string;
banner: string;
link: string;
};
type MovieHit = {
actors: string[];
color: string;
genre: string[];
image: string;
objectID: string;
score: number;
title: string;
};
const stories = storiesOf('QueryRuleContext', module);
const StoryHits = connectHits(({ hits }: { hits: MovieHit[] }) => (
<div className="hits">
{hits.map((hit) => (
<div key={hit.objectID} className="hit">
<div className="hit-picture">
<img src={hit.image} />
</div>
<div className="hit-content">
<div>
<Highlight attribute="title" hit={hit} />
</div>
</div>
</div>
))}
</div>
));
const storyProps = {
appId: 'latency',
apiKey: 'af044fb0788d6bb15f807e4420592bc5',
indexName: 'instant_search_movies',
linkedStoryGroup: 'QueryRuleContext.stories.tsx',
hitsElement: <StoryHits />,
};
stories
.add('default', () => (
<WrapWithHits {...storyProps}>
<ul>
<li>
On empty query, select the "Drama" category and The
Shawshank Redemption appears
</li>
<li>
On empty query, select the "Thriller" category and Pulp
Fiction appears
</li>
<li>
Type <q>music</q> and a banner will appear
</li>
</ul>
<div style={{ display: 'flex' }}>
<aside style={{ flex: 1 }}>
<RefinementList attribute="genre" />
</aside>
<main style={{ flex: 2 }}>
<QueryRuleContext
trackedFilters={{
genre: () => ['Drama', 'Thriller'],
}}
/>
<QueryRuleCustomData>
{({ items }: { items: CustomDataItem[] }) =>
items.map(({ banner, title, link }) => {
if (!banner) {
return null;
}
return (
<section key={title}>
<h2>{title}</h2>
<a href={link}>
<img src={banner} alt={title} />
</a>
</section>
);
})
}
</QueryRuleCustomData>
</main>
</div>
</WrapWithHits>
))
.add('with default rule context', () => (
<WrapWithHits {...storyProps}>
<ul>
<li>The rule context `ais-genre-Drama` is applied by default</li>
<li>
Select the "Drama" category and The Shawshank Redemption
appears
</li>
<li>
Select the "Thriller" category and Pulp Fiction appears
</li>
<li>
Type <q>music</q> and a banner will appear
</li>
</ul>
<div style={{ display: 'flex' }}>
<aside style={{ flex: 1 }}>
<RefinementList attribute="genre" />
</aside>
<main style={{ flex: 2 }}>
<QueryRuleContext
trackedFilters={{
genre: () => ['Drama', 'Thriller'],
}}
transformRuleContexts={(ruleContexts: string[]) => {
if (ruleContexts.length === 0) {
return ['ais-genre-Drama'];
}
return ruleContexts;
}}
/>
<QueryRuleCustomData>
{({ items }: { items: CustomDataItem[] }) =>
items.map(({ banner, title, link }) => {
if (!banner) {
return null;
}
return (
<section key={title}>
<h2>{title}</h2>
<a href={link}>
<img src={banner} alt={title} />
</a>
</section>
);
})
}
</QueryRuleCustomData>
</main>
</div>
</WrapWithHits>
));
|