-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSelectBox.jsx
executable file
·67 lines (62 loc) · 1.75 KB
/
SelectBox.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import './_SelectBox.scss';
const isSelected = (selected, val) =>
Array.isArray(selected)
? selected.some(selectedEntry => selectedEntry === val)
: selected === val;
const SelectBox = props => {
const Comp = props.component ? props.component : 'button';
return (
<div
className={cx(
'SelectBox',
props.component
? `SelectBox--${props.component.name}`
: 'SelectBox--simple',
{ disabled: props.isDisabled }
)}
>
<div>
{props.options.map(option => (
<Comp
key={
typeof option.value !== 'object'
? option.value
: JSON.stringify(option.value)
}
onClick={() => props.onClick(option.value)}
alt={props.component ? option.alt : undefined}
className={cx(option.className, {
'is-active': isSelected(props.selected, option.value),
})}
icon={props.component ? option.icon : undefined}
title={
option.title ||
(typeof option.value === 'string' ? option.value : '')
}
value={option.value}
/>
))}
</div>
</div>
);
};
SelectBox.propTypes = {
component: PropTypes.func,
className: PropTypes.string,
title: PropTypes.string,
selected: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
isDisabled: PropTypes.bool,
options: PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.any,
title: PropTypes.string,
icon: PropTypes.string,
alt: PropTypes.string,
className: PropTypes.string,
})
),
};
export default SelectBox;