-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
UncontrolledTabs.js
392 lines (334 loc) · 10.5 KB
/
UncontrolledTabs.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import PropTypes from 'prop-types';
import React, { cloneElement, Component } from 'react';
import cx from 'clsx';
import uuid from '../helpers/uuid';
import { childrenPropType } from '../helpers/propTypes';
import { getPanelsCount, getTabsCount } from '../helpers/count';
import { deepMap } from '../helpers/childrenDeepMap';
import { isTabList, isTabPanel, isTab } from '../helpers/elementTypes';
function isNode(node) {
return node && 'getAttribute' in node;
}
// Determine if a node from event.target is a Tab element
function isTabNode(node) {
return isNode(node) && node.getAttribute('data-rttab');
}
// Determine if a tab node is disabled
function isTabDisabled(node) {
return isNode(node) && node.getAttribute('aria-disabled') === 'true';
}
let canUseActiveElement;
function determineCanUseActiveElement(environment) {
const env =
environment || (typeof window !== 'undefined' ? window : undefined);
try {
canUseActiveElement = !!(
typeof env !== 'undefined' &&
env.document &&
env.document.activeElement
);
} catch (e) {
// Work around for IE bug when accessing document.activeElement in an iframe
// Refer to the following resources:
// http://stackoverflow.com/a/10982960/369687
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12733599
canUseActiveElement = false;
}
}
export default class UncontrolledTabs extends Component {
static defaultProps = {
className: 'react-tabs',
focus: false,
};
static propTypes = {
children: childrenPropType,
direction: PropTypes.oneOf(['rtl', 'ltr']),
className: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
PropTypes.object,
]),
disabledTabClassName: PropTypes.string,
disableUpDownKeys: PropTypes.bool,
domRef: PropTypes.func,
focus: PropTypes.bool,
forceRenderTabPanel: PropTypes.bool,
onSelect: PropTypes.func.isRequired,
selectedIndex: PropTypes.number.isRequired,
selectedTabClassName: PropTypes.string,
selectedTabPanelClassName: PropTypes.string,
environment: PropTypes.object,
};
tabNodes = [];
setSelected(index, event) {
// Check index boundary
if (index < 0 || index >= this.getTabsCount()) return;
const { onSelect, selectedIndex } = this.props;
// Call change event handler
onSelect(index, selectedIndex, event);
}
getNextTab(index) {
const count = this.getTabsCount();
// Look for non-disabled tab from index to the last tab on the right
for (let i = index + 1; i < count; i++) {
if (!isTabDisabled(this.getTab(i))) {
return i;
}
}
// If no tab found, continue searching from first on left to index
for (let i = 0; i < index; i++) {
if (!isTabDisabled(this.getTab(i))) {
return i;
}
}
// No tabs are disabled, return index
return index;
}
getPrevTab(index) {
let i = index;
// Look for non-disabled tab from index to first tab on the left
while (i--) {
if (!isTabDisabled(this.getTab(i))) {
return i;
}
}
// If no tab found, continue searching from last tab on right to index
i = this.getTabsCount();
while (i-- > index) {
if (!isTabDisabled(this.getTab(i))) {
return i;
}
}
// No tabs are disabled, return index
return index;
}
getFirstTab() {
const count = this.getTabsCount();
// Look for non disabled tab from the first tab
for (let i = 0; i < count; i++) {
if (!isTabDisabled(this.getTab(i))) {
return i;
}
}
return null;
}
getLastTab() {
let i = this.getTabsCount();
// Look for non disabled tab from the last tab
while (i--) {
if (!isTabDisabled(this.getTab(i))) {
return i;
}
}
return null;
}
getTabsCount() {
const { children } = this.props;
return getTabsCount(children);
}
getPanelsCount() {
const { children } = this.props;
return getPanelsCount(children);
}
getTab(index) {
return this.tabNodes[`tabs-${index}`];
}
getChildren() {
let index = 0;
const {
children,
disabledTabClassName,
focus,
forceRenderTabPanel,
selectedIndex,
selectedTabClassName,
selectedTabPanelClassName,
environment,
} = this.props;
this.tabIds = this.tabIds || [];
this.panelIds = this.panelIds || [];
let diff = this.tabIds.length - this.getTabsCount();
// Add ids if new tabs have been added
// Don't bother removing ids, just keep them in case they are added again
// This is more efficient, and keeps the uuid counter under control
while (diff++ < 0) {
this.tabIds.push(uuid());
this.panelIds.push(uuid());
}
// Map children to dynamically setup refs
return deepMap(children, (child) => {
let result = child;
// Clone TabList and Tab components to have refs
if (isTabList(child)) {
let listIndex = 0;
// Figure out if the current focus in the DOM is set on a Tab
// If it is we should keep the focus on the next selected tab
let wasTabFocused = false;
if (canUseActiveElement == null) {
determineCanUseActiveElement(environment);
}
if (canUseActiveElement) {
wasTabFocused = React.Children.toArray(child.props.children)
.filter(isTab)
.some((tab, i) => {
const env =
environment ||
(typeof window !== 'undefined' ? window : undefined);
return env && env.document.activeElement === this.getTab(i);
});
}
result = cloneElement(child, {
children: deepMap(child.props.children, (tab) => {
const key = `tabs-${listIndex}`;
const selected = selectedIndex === listIndex;
const props = {
tabRef: (node) => {
this.tabNodes[key] = node;
},
id: this.tabIds[listIndex],
panelId: this.panelIds[listIndex],
selected,
focus: selected && (focus || wasTabFocused),
};
if (selectedTabClassName)
props.selectedClassName = selectedTabClassName;
if (disabledTabClassName)
props.disabledClassName = disabledTabClassName;
listIndex++;
return cloneElement(tab, props);
}),
});
} else if (isTabPanel(child)) {
const props = {
id: this.panelIds[index],
tabId: this.tabIds[index],
selected: selectedIndex === index,
};
if (forceRenderTabPanel) props.forceRender = forceRenderTabPanel;
if (selectedTabPanelClassName)
props.selectedClassName = selectedTabPanelClassName;
index++;
result = cloneElement(child, props);
}
return result;
});
}
handleKeyDown = (e) => {
const { direction, disableUpDownKeys } = this.props;
if (this.isTabFromContainer(e.target)) {
let { selectedIndex: index } = this.props;
let preventDefault = false;
let useSelectedIndex = false;
if (e.keyCode === 32 || e.keyCode === 13) {
preventDefault = true;
useSelectedIndex = false;
this.handleClick(e);
}
if (e.keyCode === 37 || (!disableUpDownKeys && e.keyCode === 38)) {
// Select next tab to the left, validate if up arrow is not disabled
if (direction === 'rtl') {
index = this.getNextTab(index);
} else {
index = this.getPrevTab(index);
}
preventDefault = true;
useSelectedIndex = true;
} else if (e.keyCode === 39 || (!disableUpDownKeys && e.keyCode === 40)) {
// Select next tab to the right, validate if down arrow is not disabled
if (direction === 'rtl') {
index = this.getPrevTab(index);
} else {
index = this.getNextTab(index);
}
preventDefault = true;
useSelectedIndex = true;
} else if (e.keyCode === 35) {
// Select last tab (End key)
index = this.getLastTab();
preventDefault = true;
useSelectedIndex = true;
} else if (e.keyCode === 36) {
// Select first tab (Home key)
index = this.getFirstTab();
preventDefault = true;
useSelectedIndex = true;
}
// This prevents scrollbars from moving around
if (preventDefault) {
e.preventDefault();
}
// Only use the selected index in the state if we're not using the tabbed index
if (useSelectedIndex) {
this.setSelected(index, e);
}
}
};
handleClick = (e) => {
let node = e.target;
do {
if (this.isTabFromContainer(node)) {
if (isTabDisabled(node)) {
return;
}
const index = [].slice
.call(node.parentNode.children)
.filter(isTabNode)
.indexOf(node);
this.setSelected(index, e);
return;
}
} while ((node = node.parentNode) != null);
};
/**
* Determine if a node from event.target is a Tab element for the current Tabs container.
* If the clicked element is not a Tab, it returns false.
* If it finds another Tabs container between the Tab and `this`, it returns false.
*/
isTabFromContainer(node) {
// return immediately if the clicked element is not a Tab.
if (!isTabNode(node)) {
return false;
}
// Check if the first occurrence of a Tabs container is `this` one.
let nodeAncestor = node.parentElement;
do {
if (nodeAncestor === this.node) return true;
if (nodeAncestor.getAttribute('data-rttabs')) break;
nodeAncestor = nodeAncestor.parentElement;
} while (nodeAncestor);
return false;
}
render() {
// Delete all known props, so they don't get added to DOM
const {
children, // unused
className,
disabledTabClassName, // unused
domRef,
focus, // unused
forceRenderTabPanel, // unused
onSelect, // unused
selectedIndex, // unused
selectedTabClassName, // unused
selectedTabPanelClassName, // unused
environment, // unused
disableUpDownKeys, // unused
...attributes
} = this.props;
return (
<div
{...attributes}
className={cx(className)}
onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
ref={(node) => {
this.node = node;
if (domRef) domRef(node);
}}
data-rttabs
>
{this.getChildren()}
</div>
);
}
}