-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathHeader.jsx
170 lines (158 loc) · 5.2 KB
/
Header.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
import { Header as CarbonHeader, HeaderMenuButton, HeaderName, SkipToContent } from '@carbon/react';
import PropTypes from 'prop-types';
import React from 'react';
import { Switcher } from '@carbon/react/icons';
import { settings } from '../../constants/Settings';
import HeaderActionGroup from './HeaderActionGroup';
import { HeaderActionItemPropTypes, HeaderPanelPropTypes } from './HeaderPropTypes';
import { APP_SWITCHER } from './headerConstants';
const { prefix: carbonPrefix, iotPrefix } = settings;
const propTypes = {
/** Add a prefix other than IBM */
prefix: PropTypes.string,
/** Name to follow the IBM prefix up top, left */
appName: PropTypes.string.isRequired,
/** Short name to follow the IBM prefix at top, left on smaller breakpoints */
// eslint-disable-next-line react/require-default-props, uses appName is none provided
shortAppName: PropTypes.string,
/** Optional prop that provides additional app information */
subtitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
/** Add a class name to Header */
className: PropTypes.string,
/** Provide ID for the skip to content functionality */
skipto: PropTypes.string,
/** href optional url to file if you click on title */
url: PropTypes.string,
/** Object of action items */
actionItems: PropTypes.arrayOf(PropTypes.shape(HeaderActionItemPropTypes)).isRequired,
/** Bit to flip that tells header to render the nav toggle button */
hasSideNav: PropTypes.bool,
onClickSideNavExpand: PropTypes.func,
/** Main app switcher Header panel props */
headerPanel: PropTypes.shape(HeaderPanelPropTypes),
/** App switcher label */
appSwitcherLabel: PropTypes.string,
i18n: PropTypes.shape({
mainHeader: PropTypes.string,
openMenu: PropTypes.string,
closeMenu: PropTypes.string,
}),
testId: PropTypes.string,
/** Returns true, if the icon should be shown. (actionItem) => {} */
isActionItemVisible: PropTypes.func,
/** allows setting aria-label on side-nav menu button correctly */
isSideNavExpanded: PropTypes.bool,
/** Make sure the Close icon is always displayed in the HeaderActionPanel action item when the panel is expanded */
showCloseIconWhenPanelExpanded: PropTypes.bool,
/** Optional callback when user clicks on header name */
handleHeaderNameClick: PropTypes.func,
};
// istanbul ignore next
const defaultProps = {
onClickSideNavExpand: null,
hasSideNav: true,
prefix: 'IBM',
className: 'main-header',
skipto: '#main-content',
headerPanel: null,
subtitle: null,
url: '#',
appSwitcherLabel: APP_SWITCHER,
i18n: {
mainHeader: 'main header',
openMenu: 'Open menu',
closeMenu: 'Close menu',
},
testId: 'header',
isActionItemVisible: () => true,
isSideNavExpanded: false,
showCloseIconWhenPanelExpanded: false,
handleHeaderNameClick: () => null,
};
/**
* UI header with multiple side panels functionality and dropdowns
*/
const Header = ({
appName,
shortAppName,
subtitle,
className,
actionItems: actionItemsProp,
prefix,
skipto,
hasSideNav,
onClickSideNavExpand,
headerPanel,
url,
appSwitcherLabel,
i18n,
testId,
isActionItemVisible,
isSideNavExpanded,
showCloseIconWhenPanelExpanded,
handleHeaderNameClick,
}) => {
const mergedI18n = { ...defaultProps.i18n, ...i18n };
const theShortAppName = shortAppName || appName;
const actionItems = !headerPanel
? actionItemsProp
: [
...actionItemsProp,
{
id: 'app-switcher',
label: appSwitcherLabel,
hasHeaderPanel: true,
btnContent: (
<Switcher
size={20}
fill="white"
className={`${carbonPrefix}--header__menu-item ${carbonPrefix}--header__menu-title`}
/>
),
childContent: [
{
metaData: {
className: `${carbonPrefix}--app-switcher ${headerPanel.className}`,
element: 'a',
},
content: <headerPanel.content />,
},
],
},
];
return (
<CarbonHeader data-testid={testId} className={className} aria-label={mergedI18n.mainHeader}>
<SkipToContent href={skipto} />
{hasSideNav && (
<HeaderMenuButton
data-testid={`${testId}-menu-button`}
aria-label={isSideNavExpanded ? mergedI18n.closeMenu : mergedI18n.openMenu}
onClick={onClickSideNavExpand}
isActive={isSideNavExpanded}
/>
)}
<HeaderName
data-testid={`${testId}-name`}
href={url}
onClick={handleHeaderNameClick}
prefix={prefix}
>
<span>{appName}</span>
{theShortAppName ? (
<span className={`${iotPrefix}--header__short-name`}>{theShortAppName}</span>
) : null}
{subtitle ? <div className={`${iotPrefix}--header__subtitle`}>{subtitle}</div> : null}
</HeaderName>
<HeaderActionGroup
actionItems={actionItems}
i18n={mergedI18n}
testId={`${testId}-action-group`}
isActionItemVisible={isActionItemVisible}
showCloseIconWhenPanelExpanded={showCloseIconWhenPanelExpanded}
/>
</CarbonHeader>
);
};
Header.propTypes = propTypes;
Header.defaultProps = defaultProps;
export default Header;