-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathFilterButton.jsx
186 lines (163 loc) · 5.13 KB
/
FilterButton.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { useRef } from 'react';
import Icon from '../../react-chayns-icon/component/Icon';
import './FilterButton.scss';
const isValue = (val) => typeof val !== 'undefined' && val !== null;
const PREFIX = 'CC_FB_';
let currentId = 0;
/**
* A chip-like component that is used to filter lists. Usually used in a group
* of 2 or more.
*/
const FilterButton = ({
icon,
label,
count,
checked = false,
onChange,
name,
value,
className,
style,
id,
disabled = false,
stopPropagation = false,
small = false,
rectangular = false,
}) => {
const htmlId = useRef(id || `${PREFIX}${(currentId += 1)}`);
if (!isValue(label) && !isValue(icon)) {
return null;
}
const captureClick = stopPropagation
? (e) => e.stopPropagation()
: undefined;
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<label
className={classNames(className, 'button--filter', {
'button--filter--disabled': disabled,
'button--filter--active': checked,
'chayns__color--headline': checked && !className,
'button--filter--small': small,
'button--filter--rectangular': rectangular,
})}
style={style}
htmlFor={htmlId.current}
onClick={captureClick}
>
<input
id={htmlId.current}
type={name ? 'radio' : 'checkbox'}
name={name}
className="button--filter__input"
onChange={(e) => {
if (onChange) {
if (e.target.type === 'checkbox') {
onChange(e.target.checked, htmlId.current);
} else {
onChange(isValue(value) ? value : e.target.value);
}
}
}}
checked={checked}
disabled={disabled}
/>
<span className="label chayns__color--text">
{icon ? (
<Icon
icon={icon}
className="button--filter__icon chayns__color--headlinei"
/>
) : null}
{/* eslint-disable-next-line no-nested-ternary */}
{isValue(label) ? <span>{label}</span> : null}
{isValue(count) ? <b>{count}</b> : null}
</span>
</label>
);
};
FilterButton.propTypes = {
/**
* A string or `ReactNode` that is shown inside of the filter button.
*/
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
PropTypes.element,
]),
/**
* A number that is shown in bold on the right side of the button.
*/
count: PropTypes.number,
/**
* A function that will be called when the state of the button changes.
*/
onChange: PropTypes.func,
/**
* Wether the button is checked or not.
*/
checked: PropTypes.bool,
/**
* Multiple filter buttons with the same name belong to one group. Only one
* of the buttons in a group can be active at one time.
*/
name: PropTypes.string,
/**
* The value that is provided as the first argument to the
* `onChanged`-callback when the `name`-property is set.
*/
value: PropTypes.any, // eslint-disable-line react/forbid-prop-types
/**
* An icon that will be shown in the button. Use a FontAwesome icon like
* this: `"fa fa-plane"`.
*/
icon: PropTypes.oneOfType([
PropTypes.string.isRequired,
PropTypes.shape({
iconName: PropTypes.string.isRequired,
prefix: PropTypes.string.isRequired,
}).isRequired,
]),
/**
* A classname string that will be set on the `<label>`-element. To change
* the color of the filter button give it a class that sets the CSS
* `color`-attribute, not `background-color`.
*/
className: PropTypes.string,
/**
* A React style object that will be applied to the `<label>`-element. To
* change the color of the filter button, add a `color`-style attribute to
* the button, not `background-color`.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
/**
* An HTML id that will be set on the (invisible) `<input>`-element. This is
* given as the second argument to onChange if `name` is unset.
*/
id: PropTypes.string,
/**
* Disables any interaction and renders the filter button as disabled.
*/
disabled: PropTypes.bool,
/**
* Wether to stop propagation of click events to parent elements.
*/
stopPropagation: PropTypes.bool,
/**
* Shrinks the filter button in size.
*/
small: PropTypes.bool,
/**
* Changes the filter button shape to that of a button.
*/
rectangular: PropTypes.bool,
};
FilterButton.displayName = 'FilterButton';
export default FilterButton;