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[] }) => (
));
const storyProps = {
appId: 'latency',
apiKey: 'af044fb0788d6bb15f807e4420592bc5',
indexName: 'instant_search_movies',
linkedStoryGroup: 'QueryRuleContext.stories.tsx',
hitsElement: ,
};
stories
.add('default', () => (
-
On empty query, select the "Drama" category and The
Shawshank Redemption appears
-
On empty query, select the "Thriller" category and Pulp
Fiction appears
-
Type
music
and a banner will appear
['Drama', 'Thriller'],
}}
/>
{({ items }: { items: CustomDataItem[] }) =>
items.map(({ banner, title, link }) => {
if (!banner) {
return null;
}
return (
{title}
);
})
}
))
.add('with default rule context', () => (
- The rule context `ais-genre-Drama` is applied by default
-
Select the "Drama" category and The Shawshank Redemption
appears
-
Select the "Thriller" category and Pulp Fiction appears
-
Type
music
and a banner will appear
['Drama', 'Thriller'],
}}
transformRuleContexts={(ruleContexts: string[]) => {
if (ruleContexts.length === 0) {
return ['ais-genre-Drama'];
}
return ruleContexts;
}}
/>
{({ items }: { items: CustomDataItem[] }) =>
items.map(({ banner, title, link }) => {
if (!banner) {
return null;
}
return (
{title}
);
})
}
));