-
Notifications
You must be signed in to change notification settings - Fork 129
/
dropdown.js
186 lines (174 loc) · 4.17 KB
/
dropdown.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
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
import React, { useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import DropdownItem from './components/item';
import DropdownDivider from './components/divider';
import Button from '../button';
import Element from '../element';
const Dropdown = ({
className,
children,
value,
color,
align,
right,
up,
hoverable,
label,
onChange,
closeOnSelect,
icon,
domRef,
disabled,
...props
}) => {
const ref = useRef(domRef);
const [isOpen, setIsOpen] = useState(false);
const close = (evt) => {
// IDK yet how to test using the ref in enzime
// istanbul ignore if
if (
hoverable ||
(evt && ref && ref.current && ref.current.contains(evt.target))
) {
return;
}
if (ref.current) {
setIsOpen(false);
}
};
const onSelect = (selectedValue) => {
return () => {
if (onChange) {
onChange(selectedValue);
}
if (closeOnSelect) {
close();
}
};
};
useEffect(() => {
window.addEventListener('click', close);
return () => {
window.removeEventListener('click', close);
};
}, []);
let current = label;
const childrenArray = React.Children.map(children, (child, i) => {
if (
child.type === DropdownItem &&
((i === 0 && !label) || child.props.value === value)
) {
current = child.props.children;
}
return React.cloneElement(
child,
child.type === DropdownItem
? {
active: child.props.value === value,
onClick: onSelect(child.props.value),
}
: {},
);
});
return (
<Element
{...props}
domRef={ref}
className={classnames('dropdown', className, {
'is-active': isOpen,
'is-up': up,
'is-right': right || align === 'right',
'is-hoverable': hoverable,
})}
>
<div
className="dropdown-trigger"
role="presentation"
onClick={() => {
if (disabled) {
return;
}
setIsOpen((open) => {
return !open;
});
}}
>
<Button disabled={disabled} color={color}>
<span>{current}</span>
{icon}
</Button>
</div>
<div className="dropdown-menu" id="dropdown-menu" role="menu">
<div className="dropdown-content">{childrenArray}</div>
</div>
</Element>
);
};
Dropdown.Item = DropdownItem;
Dropdown.Divider = DropdownDivider;
Dropdown.propTypes = {
/**
* The value of the currently selected dropdown item. If this value match
* with the value passed to a Dropdown.item it will be used as label if the label prop its empty
*/
value: PropTypes.any,
/**
* Called when a dropdown item is selected.
*/
onChange: PropTypes.func,
/**
* The color of the dropdown button.
*/
color: PropTypes.oneOfType([
PropTypes.oneOf([
'primary',
'link',
'info',
'success',
'warning',
'danger',
'dark',
'text',
]),
PropTypes.string,
]),
disabled: PropTypes.bool,
/**
* Whether the dropdown should align to the right side.
*/
right: PropTypes.bool,
/**
* Whether the dropdown menu should appear above the dropdown button
* instead of below.
*/
up: PropTypes.bool,
/**
* Whether the dropdown menu can be activated when the cursor
* hovers above the button without clicking.
*/
hoverable: PropTypes.bool,
/**
* A string, or a react component that displays the label of the dropdown
* button. if not set it will be use the selected Dropdown.Item or the first one
* if there is no selected
*/
label: PropTypes.node,
/**
* Whether the dropdown menu should be closed when a dropdown item is selected.
*/
closeOnSelect: PropTypes.bool,
/**
* A react component that draws the icon of the dropdown button.
* Usually it is an arrow (or a chevron) pointing downwards (or upwards).
*/
icon: PropTypes.node,
/**
* Additional CSS classes to pass to `<Dropdown />`.
* They will sit alongside pre-applied bulma classes.
*/
};
Dropdown.defaultProps = {
closeOnSelect: true,
};
export default Dropdown;