forked from elastic/search-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleSelectFacet.js
78 lines (68 loc) · 2.14 KB
/
SingleSelectFacet.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
import PropTypes from "prop-types";
import React from "react";
import Select, { components } from "react-select";
import { FacetValue } from "./types";
import { getFilterValueDisplay } from "./view-helpers";
import { appendClassName } from "./view-helpers";
function Option(props) {
return (
<components.Option {...props}>
<span className="sui-select__option-label">{props.data.label}</span>
<span className="sui-select__option-count">
{props.data.count.toLocaleString("en")}
</span>
</components.Option>
);
}
Option.propTypes = {
data: PropTypes.object.isRequired
};
function toSelectBoxOption(filterValue) {
return {
value: filterValue.value,
label: getFilterValueDisplay(filterValue.value),
count: filterValue.count
};
}
const setDefaultStyle = {
option: () => ({}),
control: () => ({}),
dropdownIndicator: () => ({}),
indicatorSeparator: () => ({})
};
function SingleSelectFacet({ className, label, onChange, options }) {
let selectedSelectBoxOption;
let isSelectedSelectBoxOptionSet = false;
const selectBoxOptions = options.map(option => {
const selectBoxOption = toSelectBoxOption(option);
// There should never be multiple filters set for this facet because it is single select,
// but if there is, we use the first value.
if (option.selected && !isSelectedSelectBoxOptionSet) {
selectedSelectBoxOption = selectBoxOption;
isSelectedSelectBoxOptionSet = true;
}
return selectBoxOption;
});
return (
<div className={appendClassName("sui-facet", className)}>
<div className="sui-facet__title">{label}</div>
<Select
className="sui-select"
classNamePrefix="sui-select"
components={{ Option }}
value={selectedSelectBoxOption}
onChange={o => onChange(o.value)}
options={selectBoxOptions}
isSearchable={false}
styles={setDefaultStyle}
/>
</div>
);
}
SingleSelectFacet.propTypes = {
label: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
options: PropTypes.arrayOf(FacetValue).isRequired,
className: PropTypes.string
};
export default SingleSelectFacet;