-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Tabs.js
291 lines (253 loc) · 7.72 KB
/
Tabs.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
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
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import PropTypes from 'prop-types';
import React from 'react';
import classNames from 'classnames';
import { ChevronDownGlyph } from '@carbon/icons-react';
import { settings } from 'carbon-components';
const { prefix } = settings;
export default class Tabs extends React.Component {
static propTypes = {
/**
* Specify the text to be read by screen-readers when visiting the <Tabs>
* component
*/
ariaLabel: PropTypes.string,
/**
* Pass in a collection of <Tab> children to be rendered depending on the
* currently selected tab
*/
children: PropTypes.node,
/**
* Provide a className that is applied to the root <nav> component for the
* <Tabs>
*/
className: PropTypes.string,
/**
* Specify whether the Tab content is hidden
hidden: PropTypes.bool,
/**
* By default, this value is "navigation". You can also provide an alternate
* role if it makes sense from the accessibility-side
*/
role: PropTypes.string.isRequired,
/**
* Provide the type of Tab
*/
type: PropTypes.oneOf(['default', 'container']),
/**
* Optionally provide an `onClick` handler that is invoked when a <Tab> is
* clicked
*/
onClick: PropTypes.func,
/**
* Optionally provide an `onKeyDown` handler that is invoked when keyed
* navigation is triggered
*/
onKeyDown: PropTypes.func,
/**
* Provide an optional handler that is called whenever the selection
* changes. This method is called with the index of the tab that was
* selected
*/
onSelectionChange: PropTypes.func,
/**
* Provide a string that represents the `href` for the triggered <Tab>
*/
triggerHref: PropTypes.string.isRequired,
/**
* Optionally provide an index for the currently selected <Tab>
*/
selected: PropTypes.number,
/**
* Provide a description that is read out when a user visits the caret icon
* for the dropdown menu of items
*/
iconDescription: PropTypes.string.isRequired,
/**
* Provide a className that is applied to the <TabContent> components
*/
tabContentClassName: PropTypes.string,
};
static defaultProps = {
iconDescription: 'show menu options',
role: 'navigation',
type: 'default',
triggerHref: '#',
selected: 0,
ariaLabel: 'listbox',
};
state = {
dropdownHidden: true,
};
static getDerivedStateFromProps({ selected }, state) {
const { prevSelected } = state;
return prevSelected === selected
? null
: {
selected,
prevSelected: selected,
};
}
getTabs() {
return React.Children.map(this.props.children, tab => tab);
}
getTabAt = (index, useFresh) => {
return (
(!useFresh && this[`tab${index}`]) ||
React.Children.toArray(this.props.children)[index]
);
};
setTabAt = (index, tabRef) => {
this[`tab${index}`] = tabRef;
};
// following functions (handle*) are Props on Tab.js, see Tab.js for parameters
handleTabClick = onSelectionChange => {
return (index, evt) => {
evt.preventDefault();
this.selectTabAt(index, onSelectionChange);
this.setState({
dropdownHidden: true,
});
};
};
handleTabKeyDown = onSelectionChange => {
return (index, evt) => {
const key = evt.key || evt.which;
if (key === 'Enter' || key === 13 || key === ' ' || key === 32) {
this.selectTabAt(index, onSelectionChange);
this.setState({
dropdownHidden: true,
});
}
};
};
handleTabAnchorFocus = onSelectionChange => {
return index => {
const tabCount = React.Children.count(this.props.children) - 1;
let tabIndex = index;
if (index < 0) {
tabIndex = tabCount;
} else if (index > tabCount) {
tabIndex = 0;
}
const tab = this.getTabAt(tabIndex);
if (tab) {
this.selectTabAt(tabIndex, onSelectionChange);
if (tab.tabAnchor) {
tab.tabAnchor.focus();
}
}
};
};
handleDropdownClick = () => {
this.setState({
dropdownHidden: !this.state.dropdownHidden,
});
};
selectTabAt = (index, onSelectionChange) => {
if (this.state.selected !== index) {
this.setState({
selected: index,
});
if (typeof onSelectionChange === 'function') {
onSelectionChange(index);
}
}
};
render() {
const {
ariaLabel,
iconDescription,
className,
triggerHref,
role,
type,
onSelectionChange,
tabContentClassName,
...other
} = this.props;
/**
* The tab panel acts like a tab panel when the screen is wider, but acts
* like a select list when the screen is narrow. In the wide case we want
* to allow the user to use the tab key to set the focus in the tab panel
* and then use the left and right arrow keys to navigate the tabs. In the
* narrow case we want to use the tab key to select different options in
* the list.
*
* We set the tab index based on the different states so the browser will treat
* the whole tab panel as a single focus component when it looks like a tab
* panel and separate components when it looks like a select list.
*/
const tabsWithProps = this.getTabs().map((tab, index) => {
const tabPanelIndex = index === this.state.selected ? 0 : -1;
const tabIndex = !this.state.dropdownHidden ? 0 : tabPanelIndex;
const newTab = React.cloneElement(tab, {
index,
selected: index === this.state.selected,
handleTabClick: this.handleTabClick(onSelectionChange),
handleTabAnchorFocus: this.handleTabAnchorFocus(onSelectionChange),
tabIndex,
ref: e => {
this.setTabAt(index, e);
},
handleTabKeyDown: this.handleTabKeyDown(onSelectionChange),
});
return newTab;
});
const tabContentWithProps = React.Children.map(tabsWithProps, tab => {
const { children, selected, renderContent: TabContent } = tab.props;
return (
<TabContent
className={tabContentClassName}
aria-hidden={!selected}
hidden={!selected}
selected={selected}>
{children}
</TabContent>
);
});
const classes = {
tabs: classNames(`${prefix}--tabs`, className, {
[`${prefix}--tabs--container`]: type === 'container',
}),
tablist: classNames(`${prefix}--tabs__nav`, {
[`${prefix}--tabs__nav--hidden`]: this.state.dropdownHidden,
}),
};
const selectedTab = this.getTabAt(this.state.selected, true);
const selectedLabel = selectedTab ? selectedTab.props.label : '';
return (
<>
<div {...other} className={classes.tabs} role={role}>
<div
role="listbox"
aria-label={ariaLabel}
tabIndex={0}
className={`${prefix}--tabs-trigger`}
onClick={this.handleDropdownClick}
onKeyPress={this.handleDropdownClick}>
<a
tabIndex={-1}
className={`${prefix}--tabs-trigger-text`}
href={triggerHref}
onClick={this.handleDropdownClick}>
{selectedLabel}
</a>
<ChevronDownGlyph aria-hidden="true">
{iconDescription && <title>{iconDescription}</title>}
</ChevronDownGlyph>
</div>
<ul role="tablist" className={classes.tablist}>
{tabsWithProps}
</ul>
</div>
{tabContentWithProps}
</>
);
}
}