File size: 2,202 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
import React from 'react';
import PropTypes from 'prop-types';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import {
  Hits,
  Highlight,
  Panel,
  Snippet,
  Configure,
} from 'react-instantsearch-dom';
import { connectHitInsights } from 'react-instantsearch-core';
import { WrapWithHits } from './util';

const stories = storiesOf('Hits', module);

stories
  .add('default', () => (
    <WrapWithHits linkedStoryGroup="Hits.stories.js">
      <Hits />
    </WrapWithHits>
  ))
  .add('with custom rendering', () => {
    function Product({ hit }) {
      return (
        <div>
          <Highlight attribute="name" hit={hit} />
          <p>
            <Highlight attribute="type" hit={hit} />
          </p>
          <p>
            <Snippet attribute="description" hit={hit} />
          </p>
        </div>
      );
    }
    Product.propTypes = {
      hit: PropTypes.object.isRequired,
    };
    return (
      <WrapWithHits linkedStoryGroup="Hits.stories.js">
        <Hits hitComponent={Product} />
      </WrapWithHits>
    );
  })
  .add('with Panel', () => (
    <WrapWithHits linkedStoryGroup="Hits.stories.js">
      <Panel header="Hits" footer="Footer">
        <Hits />
      </Panel>
    </WrapWithHits>
  ))
  .add('with Insights', () => {
    const insightsClient = (method, payload) =>
      action(`[InsightsClient] sent ${method} with payload`)(payload);
    const ProductWithInsights = connectHitInsights(insightsClient)(Product);
    function Product({ hit, insights }) {
      return (
        <div>
          <Highlight attribute="name" hit={hit} />
          <button
            onClick={() =>
              insights('clickedObjectIDsAfterSearch', {
                eventName: 'Add to cart',
              })
            }
          >
            Add to cart
          </button>
        </div>
      );
    }
    Product.propTypes = {
      hit: PropTypes.object.isRequired,
      insights: PropTypes.func.isRequired,
    };
    return (
      <WrapWithHits linkedStoryGroup="Hits.stories.js">
        <Configure clickAnalytics />
        <Hits hitComponent={ProductWithInsights} />
      </WrapWithHits>
    );
  });