-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Tab.js
96 lines (87 loc) · 2.07 KB
/
Tab.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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import cx from 'clsx';
const DEFAULT_CLASS = 'react-tabs__tab';
export default class Tab extends Component {
static defaultProps = {
className: DEFAULT_CLASS,
disabledClassName: `${DEFAULT_CLASS}--disabled`,
focus: false,
id: null,
panelId: null,
selected: false,
selectedClassName: `${DEFAULT_CLASS}--selected`,
};
static propTypes = {
children: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
PropTypes.string,
]),
className: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
PropTypes.object,
]),
disabled: PropTypes.bool,
tabIndex: PropTypes.string,
disabledClassName: PropTypes.string,
focus: PropTypes.bool, // private
id: PropTypes.string, // private
panelId: PropTypes.string, // private
selected: PropTypes.bool, // private
selectedClassName: PropTypes.string,
tabRef: PropTypes.func, // private
};
componentDidMount() {
this.checkFocus();
}
componentDidUpdate() {
this.checkFocus();
}
checkFocus() {
const { selected, focus } = this.props;
if (selected && focus) {
this.node.focus();
}
}
render() {
const {
children,
className,
disabled,
disabledClassName,
focus, // unused
id,
panelId,
selected,
selectedClassName,
tabIndex,
tabRef,
...attributes
} = this.props;
return (
<li
{...attributes}
className={cx(className, {
[selectedClassName]: selected,
[disabledClassName]: disabled,
})}
ref={(node) => {
this.node = node;
if (tabRef) tabRef(node);
}}
role="tab"
id={id}
aria-selected={selected ? 'true' : 'false'}
aria-disabled={disabled ? 'true' : 'false'}
aria-controls={panelId}
tabIndex={tabIndex || (selected ? '0' : null)}
data-rttab
>
{children}
</li>
);
}
}
Tab.tabsRole = 'Tab';