File size: 1,976 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { translatable } from 'react-instantsearch-core';
import { createClassNames } from '../core/utils';
import List from './List';
import Link from './Link';

const cx = createClassNames('HierarchicalMenu');

const itemsPropType = PropTypes.arrayOf(
  PropTypes.shape({
    label: PropTypes.string.isRequired,
    value: PropTypes.string,
    count: PropTypes.number.isRequired,
    items: (...args) => itemsPropType(...args),
  })
);

class HierarchicalMenu extends Component {
  static propTypes = {
    translate: PropTypes.func.isRequired,
    refine: PropTypes.func.isRequired,
    createURL: PropTypes.func.isRequired,
    canRefine: PropTypes.bool.isRequired,
    items: itemsPropType,
    showMore: PropTypes.bool,
    className: PropTypes.string,
    limit: PropTypes.number,
    showMoreLimit: PropTypes.number,
    transformItems: PropTypes.func,
  };

  static defaultProps = {
    className: '',
  };

  renderItem = (item) => {
    const { createURL, refine } = this.props;

    return (
      <Link
        className={cx('link', item.isRefined && 'link--selected')}
        onClick={() => refine(item.value)}
        href={createURL(item.value)}
      >
        <span className={cx('label')}>{item.label}</span>{' '}
        <span className={cx('count')}>{item.count}</span>
      </Link>
    );
  };

  render() {
    const {
      translate,
      items,
      showMore,
      limit,
      showMoreLimit,
      canRefine,
      className,
    } = this.props;
    return (
      <List
        renderItem={this.renderItem}
        cx={cx}
        translate={translate}
        items={items}
        showMore={showMore}
        limit={limit}
        showMoreLimit={showMoreLimit}
        canRefine={canRefine}
        className={className}
      />
    );
  }
}

export default translatable({
  showMore: (extended) => (extended ? 'Show less' : 'Show more'),
})(HierarchicalMenu);