/
DropdownRefinementList.js
95 lines (89 loc) 路 2.55 KB
/
DropdownRefinementList.js
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
import React, { Component } from 'react';
import { connectRefinementList } from 'react-instantsearch/connectors';
import PropTypes from 'prop-types';
/**
*
* Dropdown menu item for DropdownRefinementList.
*
* Each component manages its own state and uses the RefinementList connector.
*
* Not typically interacted with directly by devs.
*
* Renders click event if hoverable=false or if on mobile
*
*/
const cx = label => `ais-DropdownRefinementList-${label}`;
class DropdownRefinementList extends Component {
constructor(props) {
super(props);
this.state = {
active: false,
mobile: false,
};
}
componentDidMount() {
this.setState({
mobile: /Mobi/.test(navigator.userAgent),
});
}
capitalizeFirst(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
renderItem = (item, i) => (
<label key={i} className="ais-DropdownRefinementList-item-label">
<input
type="checkbox"
checked={item.isRefined}
onChange={() => {
this.selectItem(item);
}}
/>
<span>{item.label}</span>
<span className={cx('item-count')}> ({item.count})</span>
</label>
);
selectItem = (item, resetQuery) => {
this.props.refine(item.value);
};
handleEvent = e => {
this.setState({ active: !this.state.active });
};
render() {
const { items, attribute, hoverable, currentRefinement } = this.props;
const { active, mobile } = this.state;
const title = this.capitalizeFirst(attribute);
console.log(this.state);
return (
<div
className="ais-DropdownRefinementList-container"
onMouseLeave={hoverable && !mobile && this.handleEvent}
onMouseEnter={hoverable && !mobile && this.handleEvent}
>
<div className={cx('title-container')} onClick={this.handleEvent}>
<span className="ais-DropdownRefinementList-title">
{title}{' '}
{currentRefinement.length > 0 ? (
<p className={cx('active-facets')}>{currentRefinement.length}</p>
) : (
<i className={cx('caret-down')} />
)}
</span>
</div>
{active && (
<div className="ais-DropdownRefinementList-List">
{items.map(this.renderItem)}
</div>
)}
</div>
);
}
}
DropdownRefinementList.propTypes = {
attribute: PropTypes.string.isRequired,
hoverable: PropTypes.bool,
limit: PropTypes.number,
};
DropdownRefinementList.defaultProps = {
hoverable: false,
};
export default connectRefinementList(DropdownRefinementList);