-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Tab.js
91 lines (86 loc) · 1.96 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
import PropTypes from 'prop-types';
import React, { useEffect, useRef } from 'react';
import cx from 'clsx';
const DEFAULT_CLASS = 'react-tabs__tab';
const DEFAULT_PROPS = {
className: DEFAULT_CLASS,
disabledClassName: `${DEFAULT_CLASS}--disabled`,
focus: false,
id: null,
panelId: null,
selected: false,
selectedClassName: `${DEFAULT_CLASS}--selected`,
};
const 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,
};
const Tab = (props) => {
let nodeRef = useRef();
const checkFocus = () => {
const { selected, focus } = props;
if (selected && focus) {
nodeRef.current.focus();
}
};
useEffect(() => {
checkFocus();
});
const {
children,
className,
disabled,
disabledClassName,
focus, // unused
id,
panelId,
selected,
selectedClassName,
tabIndex,
tabRef,
...attributes
} = props;
return (
<li
{...attributes}
className={cx(className, {
[selectedClassName]: selected,
[disabledClassName]: disabled,
})}
ref={(node) => {
nodeRef.current = 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.propTypes = propTypes;
Tab.tabsRole = 'Tab';
Tab.defaultProps = DEFAULT_PROPS;
export default Tab;