-
Notifications
You must be signed in to change notification settings - Fork 367
/
Facet.js
127 lines (111 loc) · 2.98 KB
/
Facet.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
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
import PropTypes from "prop-types";
import { Component } from "react";
import { MultiCheckboxFacet } from "@elastic/react-search-ui-views";
import { Facet, Filter, FilterType } from "../types";
import { accentFold } from "../helpers";
import { withSearch } from "..";
function findFacetValueInFilters(name, filters, filterType) {
const filter = filters.find(f => f.field === name && f.type === filterType);
if (!filter) return;
return filter.values;
}
export class FacetContainer extends Component {
static propTypes = {
// Props
className: PropTypes.string,
field: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
filterType: FilterType,
show: PropTypes.number,
view: PropTypes.func,
isFilterable: PropTypes.bool,
// State
filters: PropTypes.arrayOf(Filter).isRequired,
facets: PropTypes.objectOf(PropTypes.arrayOf(Facet)).isRequired,
// Actions
addFilter: PropTypes.func.isRequired,
removeFilter: PropTypes.func.isRequired,
setFilter: PropTypes.func.isRequired
};
static defaultProps = {
filterType: "all",
isFilterable: false
};
constructor({ show = 5 }) {
super();
this.state = {
more: show,
searchTerm: ""
};
}
handleClickMore = () => {
this.setState(({ more }) => ({
more: more + 10
}));
};
handleFacetSearch = searchTerm => {
this.setState({ searchTerm });
};
render() {
const { more, searchTerm } = this.state;
const {
addFilter,
className,
facets,
field,
filterType,
filters,
label,
removeFilter,
setFilter,
view,
isFilterable
} = this.props;
const facetValues = facets[field];
if (!facetValues) return null;
let options = facetValues[0].data;
const selectedValues =
findFacetValueInFilters(field, filters, filterType) || [];
if (!options.length && !selectedValues.length) return null;
if (searchTerm.trim()) {
options = options.filter(option =>
accentFold(option.value)
.toLowerCase()
.includes(accentFold(searchTerm).toLowerCase())
);
}
const View = view || MultiCheckboxFacet;
return View({
className,
label: label,
onMoreClick: this.handleClickMore,
onRemove: value => {
removeFilter(field, value, filterType);
},
onChange: value => {
setFilter(field, value, filterType);
},
onSelect: value => {
addFilter(field, value, filterType);
},
options: options.slice(0, more),
optionsCount: options.length,
showMore: options.length > more,
values: selectedValues,
showSearch: isFilterable,
onSearch: value => {
this.handleFacetSearch(value);
},
searchPlaceholder: `Filter ${field}`
});
}
}
export default withSearch(
({ filters, facets, addFilter, removeFilter, setFilter }) => ({
filters,
facets,
addFilter,
removeFilter,
setFilter
})
)(FacetContainer);