-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathTab.jsx
More file actions
76 lines (69 loc) · 1.62 KB
/
Tab.jsx
File metadata and controls
76 lines (69 loc) · 1.62 KB
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
import React, { useEffect, useRef } from 'react';
import cx from 'clsx';
const DEFAULT_CLASS = 'react-tabs__tab';
/*
Left for TS migration
const propTypes = {
children: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
PropTypes.string,
]),
className: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
PropTypes.object,
]),
disabled: PropTypes.bool,
disabledClassName: PropTypes.string,
focus: PropTypes.bool, // private
id: PropTypes.string, // private
selected: PropTypes.bool, // private
selectedClassName: PropTypes.string,
tabIndex: PropTypes.string,
tabRef: PropTypes.func, // private
};*/
const Tab = ({
children,
className = DEFAULT_CLASS,
disabled,
disabledClassName = `${DEFAULT_CLASS}--disabled`,
focus = false,
id = null,
selected = false,
selectedClassName = `${DEFAULT_CLASS}--selected`,
tabIndex,
tabRef,
...attributes
}) => {
let nodeRef = useRef();
useEffect(() => {
if (selected && focus) {
nodeRef.current.focus();
}
}, [selected, focus]);
return (
<li
{...attributes}
className={cx(className, {
[selectedClassName]: selected,
[disabledClassName]: disabled,
})}
ref={(node) => {
nodeRef.current = node;
if (tabRef) tabRef(node);
}}
role="tab"
id={`tab${id}`}
aria-selected={selected ? 'true' : 'false'}
aria-disabled={disabled ? 'true' : 'false'}
aria-controls={`panel${id}`}
tabIndex={tabIndex || (selected ? '0' : null)}
data-rttab
>
{children}
</li>
);
};
Tab.tabsRole = 'Tab';
export default Tab;