This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathToolbarList.jsx
57 lines (47 loc) · 1.59 KB
/
ToolbarList.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
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { DropdownButton } from 'patternfly-react';
import { ToolbarListItem } from './ToolbarListItem';
import { isEnabled } from './utility';
import CountContext from './ToolbarContext';
const toolbarListTitle = props => (
<React.Fragment>
{ props.icon && <i className={props.icon} style={{ color: props.color }} /> }
{ props.text }
</React.Fragment>
);
toolbarListTitle.propTypes = {
icon: PropTypes.string,
color: PropTypes.string,
text: PropTypes.string,
};
export const ToolbarList = (props) => {
const count = useContext(CountContext);
// Filter out invisible items.
const visibleItems = props.items.filter(i => !i.hidden);
// Do not render at all if no child is visible.
if (visibleItems.length === 0) {
return null;
}
// Calculate item's enable state based on item's initial enable state, onwhen and count.
// Toolbar is disabled if no item is enabled.
let isToolbarEnabled = false;
const enabledItems = visibleItems.map((i) => {
const enabled = i.onwhen ? isEnabled(i.onwhen, count) : i.enabled;
isToolbarEnabled = isToolbarEnabled || enabled;
return {
...i,
enabled,
};
});
return (
<DropdownButton id={props.id} disabled={!isToolbarEnabled} title={toolbarListTitle(props)}>
{ enabledItems.map(item => <ToolbarListItem key={item.id} {...item} onClick={props.onClick} />) }
</DropdownButton>
);
};
ToolbarList.propTypes = {
id: PropTypes.string.isRequired,
items: PropTypes.arrayOf(PropTypes.any),
onClick: PropTypes.func.isRequired,
};