File size: 4,339 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
167
168
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import SearchBox from '../components/SearchBox';

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

class List extends Component {
  static propTypes = {
    cx: PropTypes.func.isRequired,
    // Only required with showMore.
    translate: PropTypes.func,
    items: itemsPropType,
    renderItem: PropTypes.func.isRequired,
    selectItem: PropTypes.func,
    className: PropTypes.string,
    showMore: PropTypes.bool,
    limit: PropTypes.number,
    showMoreLimit: PropTypes.number,
    show: PropTypes.func,
    searchForItems: PropTypes.func,
    searchable: PropTypes.bool,
    isFromSearch: PropTypes.bool,
    canRefine: PropTypes.bool,
  };

  static defaultProps = {
    className: '',
    isFromSearch: false,
  };

  constructor() {
    super();

    this.state = {
      extended: false,
      query: '',
    };
  }

  onShowMoreClick = () => {
    this.setState((state) => ({
      extended: !state.extended,
    }));
  };

  getLimit = () => {
    const { limit, showMoreLimit } = this.props;
    const { extended } = this.state;
    return extended ? showMoreLimit : limit;
  };

  resetQuery = () => {
    this.setState({ query: '' });
  };

  renderItem = (item, resetQuery) => {
    const itemHasChildren = item.items && Boolean(item.items.length);

    return (
      <li
        key={item.key || item.label}
        className={this.props.cx(
          'item',
          item.isRefined && 'item--selected',
          item.noRefinement && 'item--noRefinement',
          itemHasChildren && 'item--parent'
        )}
      >
        {this.props.renderItem(item, resetQuery)}
        {itemHasChildren && (
          <ul className={this.props.cx('list', 'list--child')}>
            {item.items
              .slice(0, this.getLimit())
              .map((child) => this.renderItem(child, item))}
          </ul>
        )}
      </li>
    );
  };

  renderShowMore() {
    const { showMore, translate, cx } = this.props;
    const { extended } = this.state;
    const disabled = this.props.limit >= this.props.items.length;
    if (!showMore) {
      return null;
    }

    return (
      <button
        disabled={disabled}
        className={cx('showMore', disabled && 'showMore--disabled')}
        onClick={this.onShowMoreClick}
      >
        {translate('showMore', extended)}
      </button>
    );
  }

  renderSearchBox() {
    const { cx, searchForItems, isFromSearch, translate, items, selectItem } =
      this.props;

    const noResults =
      items.length === 0 && this.state.query !== '' ? (
        <div className={cx('noResults')}>{translate('noResults')}</div>
      ) : null;
    return (
      <div className={cx('searchBox')}>
        <SearchBox
          currentRefinement={this.state.query}
          refine={(value) => {
            this.setState({ query: value });
            searchForItems(value);
          }}
          focusShortcuts={[]}
          translate={translate}
          onSubmit={(e) => {
            e.preventDefault();
            e.stopPropagation();
            if (isFromSearch && items.length > 0) {
              selectItem(items[0], this.resetQuery);
            }
          }}
        />
        {noResults}
      </div>
    );
  }

  render() {
    const { cx, items, className, searchable, canRefine } = this.props;
    const searchBox = searchable ? this.renderSearchBox() : null;
    const rootClassName = classNames(
      cx('', !canRefine && '-noRefinement'),
      className
    );

    if (items.length === 0) {
      return <div className={rootClassName}>{searchBox}</div>;
    }

    // Always limit the number of items we show on screen, since the actual
    // number of retrieved items might vary with the `maxValuesPerFacet` config
    // option.
    return (
      <div className={rootClassName}>
        {searchBox}
        <ul className={cx('list', !canRefine && 'list--noRefinement')}>
          {items
            .slice(0, this.getLimit())
            .map((item) => this.renderItem(item, this.resetQuery))}
        </ul>
        {this.renderShowMore()}
      </div>
    );
  }
}

export default List;