File size: 2,317 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { translatable } from 'react-instantsearch-core';
import { createClassNames } from '../core/utils';

const cx = createClassNames('InfiniteHits');

class InfiniteHits extends Component {
  render() {
    const {
      hitComponent: HitComponent,
      hits,
      showPrevious,
      hasPrevious,
      hasMore,
      refinePrevious,
      refineNext,
      translate,
      className,
    } = this.props;

    return (
      <div className={classNames(cx(''), className)}>
        {showPrevious && (
          <button
            className={cx(
              'loadPrevious',
              !hasPrevious && 'loadPrevious--disabled'
            )}
            onClick={() => refinePrevious()}
            disabled={!hasPrevious}
          >
            {translate('loadPrevious')}
          </button>
        )}
        <ul className={cx('list')}>
          {hits.map((hit) => (
            <li key={hit.objectID} className={cx('item')}>
              <HitComponent hit={hit} />
            </li>
          ))}
        </ul>
        <button
          className={cx('loadMore', !hasMore && 'loadMore--disabled')}
          onClick={() => refineNext()}
          disabled={!hasMore}
        >
          {translate('loadMore')}
        </button>
      </div>
    );
  }
}

InfiniteHits.propTypes = {
  hits: PropTypes.arrayOf(PropTypes.object).isRequired,
  showPrevious: PropTypes.bool.isRequired,
  hasPrevious: PropTypes.bool.isRequired,
  hasMore: PropTypes.bool.isRequired,
  refinePrevious: PropTypes.func.isRequired,
  refineNext: PropTypes.func.isRequired,
  translate: PropTypes.func.isRequired,
  className: PropTypes.string,

  // this is actually PropTypes.elementType, but our prop-types version is outdated
  hitComponent: PropTypes.any,
};

InfiniteHits.defaultProps = {
  className: '',
  showPrevious: false,
  hitComponent: (hit) => (
    <div
      style={{
        borderBottom: '1px solid #bbb',
        paddingBottom: '5px',
        marginBottom: '5px',
        wordBreak: 'break-all',
      }}
    >
      {JSON.stringify(hit).slice(0, 100)}
      ...
    </div>
  ),
};

export default translatable({
  loadPrevious: 'Load previous',
  loadMore: 'Load more',
})(InfiniteHits);