-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathSimpleIconDropdown.jsx
102 lines (92 loc) · 2.81 KB
/
SimpleIconDropdown.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
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { Dropdown } from '@carbon/react';
import { validThresholdIcons } from '../DashboardEditor/editorUtils';
import { settings } from '../../constants/Settings';
import deprecate from '../../internal/deprecate';
import { CarbonIconPropType } from '../../constants/SharedPropTypes';
const { iotPrefix } = settings;
const iconProptype = PropTypes.shape({
carbonIcon: CarbonIconPropType,
name: PropTypes.string,
});
const propTypes = {
/** Array of icons to be shown */
icons: PropTypes.arrayOf(iconProptype),
/** The title of the Dropdown, defaults to 'Icon' */
titleText: PropTypes.string,
/** Required Id string */
id: PropTypes.string.isRequired,
/** True if the light theme is to be used, defaults to false */
light: PropTypes.bool,
/** Callback for when any of the Dropdown icon value changes */
onChange: PropTypes.func.isRequired,
/** Callback to translate common icons */
translateWithId: PropTypes.func,
/** The selected icon, use to set initial icon */
selectedIcon: iconProptype,
// TODO: remove deprecated 'testID' in v3
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
/** Id used if needed for testing */
testId: PropTypes.string,
};
const defaultProps = {
icons: validThresholdIcons,
light: false,
selectedIcon: undefined,
testId: undefined,
titleText: '',
translateWithId: undefined,
};
const SimpleIconDropdown = ({
icons,
id,
light,
onChange,
selectedIcon: selectedIconProp,
titleText,
// TODO: remove deprecated 'testID' in v3
testID,
testId,
translateWithId,
}) => {
const [selectedIcon, setSelectedIcon] = useState(selectedIconProp);
const renderIconItem = (item) => (
<div
title={item.name}
style={{ color: item.color || 'unset' }}
className={`${iotPrefix}--icon-dropdown__item`}
>
<div className={`${iotPrefix}--icon-dropdown__item-border`}>{item.carbonIcon}</div>
</div>
);
return (
<Dropdown
className={`${iotPrefix}--icon-dropdown`}
id={id}
itemToElement={renderIconItem}
renderSelectedItem={renderIconItem}
itemToString={(item) => item.name}
items={icons}
label=""
light={light}
onChange={({ selectedItem }) => {
setSelectedIcon(selectedItem);
onChange({ icon: selectedItem });
}}
translateWithId={translateWithId}
selectedItem={selectedIcon || icons[0]}
titleText={titleText}
type="default"
// TODO: remove deprecated 'testID' in v3
data-testid={testID || testId}
/>
);
};
SimpleIconDropdown.propTypes = propTypes;
SimpleIconDropdown.defaultProps = defaultProps;
export default SimpleIconDropdown;