-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathIconDropdown.jsx
294 lines (255 loc) · 8.7 KB
/
IconDropdown.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import React, { useState, useCallback, useRef } from 'react';
import PropTypes from 'prop-types';
import { settings } from '../../constants/Settings';
import { Dropdown } from '../Dropdown';
const { iotPrefix, prefix } = settings;
const itemShape = PropTypes.shape({
id: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired,
footer: PropTypes.node,
});
const propTypes = {
id: PropTypes.string.isRequired,
dropdownId: PropTypes.string.isRequired,
columnCount: PropTypes.number,
selectedItem: itemShape,
items: PropTypes.arrayOf(itemShape).isRequired,
// Dropdown specific props
/**
* 'aria-label' of the ListBox component.
*/
ariaLabel: PropTypes.string,
/**
* Specify the direction of the dropdown. Can be either top or bottom.
*/
direction: PropTypes.oneOf(['top', 'bottom']),
/**
* Disable the control
*/
disabled: PropTypes.bool,
/**
* Provide helper text that is used alongside the control label for
* additional help
*/
helperText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/**
* Specify if the currently selected value is invalid.
*/
invalid: PropTypes.bool,
/**
* Message which is displayed if the value is invalid.
*/
invalidText: PropTypes.string,
/**
* Generic `label` that will be used as the textual representation of what
* this field is for
*/
label: PropTypes.node.isRequired,
/**
* `true` to use the light version.
*/
light: PropTypes.bool,
/**
* `onChange` is a utility for this controlled component to communicate to a
* consuming component what kind of internal state changes are occurring.
*/
onChange: PropTypes.func,
translateWithId: PropTypes.func,
/**
* Provide the title text that will be read by a screen reader when
* visiting this control
*/
titleText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/**
* The dropdown type, `default` or `inline`
*/
type: PropTypes.oneOf(['default', 'inline']),
testId: PropTypes.string,
};
/* istanbul ignore next, ignore the default onChange */
const defaultPropTypes = {
columnCount: 4,
selectedItem: null,
disabled: false,
type: 'default',
light: false,
titleText: '',
helperText: '',
direction: 'bottom',
ariaLabel: '',
invalid: false,
invalidText: '',
onChange: () => {},
translateWithId: undefined,
testId: 'icon-dropdown',
};
const defaultItemSize = 48;
const defaultHelperPadding = 4;
const IconDropdown = ({
id,
columnCount,
selectedItem: controlledSelectedItem,
items,
dropdownId,
disabled,
direction,
onChange,
translateWithId,
testId,
...other
}) => {
const [isOpen, setIsOpen] = useState(false);
const [highlightedIndex, setHighlightedIndex] = useState(-1);
const [internalSelectedItem, setInternalSelectedItem] = useState(controlledSelectedItem);
const [height, setHeight] = useState(0);
const [width, setWidth] = useState(columnCount * defaultItemSize);
const [topTranslate, setTopTranslate] = useState(0);
const [bottomTranslate, setBottomTranslate] = useState(0);
const selectedItem =
controlledSelectedItem !== null ? controlledSelectedItem : internalSelectedItem;
const dropdownRef = useRef(null);
const highlightedItem =
highlightedIndex >= 0 && highlightedIndex < items.length ? items[highlightedIndex] : null;
// const hasFooter = highlightedItem || selectedItem;
const handleClick = useCallback(() => {
// Takes measurements of the dropdown and text that renders beneath this - used to position the footer
// const dropdown = document.getElementById(id);
const helperText = dropdownRef.current.getElementsByClassName(
`${prefix}--form__helper-text`
)[0];
const validationText = dropdownRef.current.getElementsByClassName(
`${prefix}--form-requirement`
)[0];
const menuOption = dropdownRef.current.getElementsByClassName(
`${prefix}--list-box__menu-item`
)[0];
const dropdownLabel = dropdownRef.current.getElementsByClassName(
`${prefix}--list-box__field`
)[0];
const helperTextHeight =
helperText !== undefined ? helperText.clientHeight + defaultHelperPadding : 0;
const validationTextHeight =
validationText !== undefined ? validationText.clientHeight + defaultHelperPadding : 0;
const labelHeight = dropdownLabel !== undefined ? dropdownLabel.clientHeight : 0;
setWidth(columnCount * (menuOption?.offsetWidth ?? defaultItemSize));
setHeight(Math.ceil(items.length / columnCount) * defaultItemSize);
setTopTranslate(helperTextHeight + validationTextHeight + labelHeight + 1); // Add one for the border width
setBottomTranslate(helperTextHeight + validationTextHeight);
}, [columnCount, dropdownRef, items.length]);
const Footer = () => {
const selectedFooter = highlightedItem !== null ? highlightedItem : selectedItem;
return (
<div
data-testid={`${testId}-footer`}
className={`${iotPrefix}--icon-dropdown__footer`}
style={{
width: `${width}px`,
transform: `translateY(-${direction === 'top' ? topTranslate : bottomTranslate}px)`,
paddingTop: direction === 'bottom' ? `${height}px` : 0,
paddingBottom: direction === 'top' ? `${height}px` : 0,
bottom: direction === 'top' ? 0 : 'initial',
}}
>
{selectedFooter && (
<div className={`${iotPrefix}--icon-dropdown__footer-content`}>
{selectedFooter.footer}
</div>
)}
</div>
);
};
const itemToElement = (item) => {
const index = items.findIndex((element) => element.id === item.id);
return (
<>
{/* {
// only display this button when the dropdown is open, bc if it's shown when closed it's
// rendered in a different place and causes a <button> within <button> warning.
isOpen ? (
<Button
className={classnames(
`${iotPrefix}--icon-dropdown__image-button`,
{
[`${iotPrefix}--icon-dropdown__image-button--leading`]: index % columnCount === 0,
},
{
[`${iotPrefix}--icon-dropdown__image-button--trailing`]:
(index + 1) % columnCount === 0,
},
{
[`${iotPrefix}--icon-dropdown__image-button--bottom`]:
index + columnCount >= items.length && !hasFooter && direction === 'bottom',
},
{
[`${iotPrefix}--icon-dropdown__image-button--top`]:
index < columnCount && hasFooter && direction === 'top',
}
)}
renderIcon={item?.icon}
kind="ghost"
hasIconOnly
disabled={disabled}
selected={item.id === selectedItem?.id}
testId={`dropdown-button__${item?.id}`}
iconDescription={item.text}
title={item?.text}
/>
) : null
} */}
<div
className={`${iotPrefix}--icon-dropdown__selected-icon-label`}
onMouseEnter={() => {
if (index !== highlightedIndex) {
setHighlightedIndex(index);
}
}}
>
{React.createElement(item.icon)}
<div className={`${iotPrefix}--icon-dropdown__selected-icon-label__content`}>
{item.text}
</div>
</div>
</>
);
};
return (
<div id={id} ref={dropdownRef}>
{isOpen && direction === 'top' && <Footer />}
<Dropdown
id={dropdownId}
direction={direction}
style={{ width: `${width}px` }}
items={items}
className={`${iotPrefix}--icon-dropdown__selection-buttons`}
selectedItem={selectedItem}
onChange={({ selectedItem: newSelected }) => {
setInternalSelectedItem(newSelected);
onChange(newSelected);
}}
translateWithId={translateWithId}
renderSelectedItem={itemToElement}
downshiftProps={{
isOpen,
onStateChange: (change) => {
handleClick();
if (change.isOpen !== undefined) {
setIsOpen(change.isOpen);
}
if (change.highlightedIndex !== undefined) {
setHighlightedIndex(change.highlightedIndex);
}
},
}}
data-testid={testId}
{...other}
itemToElement={itemToElement}
itemToString={(item) => item.text}
/>
{isOpen && direction === 'bottom' && <Footer />}
</div>
);
};
IconDropdown.propTypes = propTypes;
IconDropdown.defaultProps = defaultPropTypes;
export default IconDropdown;