-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathTabsHelpAndClose.tsx
More file actions
88 lines (82 loc) · 3.05 KB
/
Copy pathTabsHelpAndClose.tsx
File metadata and controls
88 lines (82 loc) · 3.05 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
77
78
79
80
81
82
83
84
85
86
87
88
import { createRef, useEffect, useRef, useState } from 'react';
import { Tabs, Tab, TabTitleText, Popover, TabAction } from '@patternfly/react-core';
import RhUiQuestionMarkCircleIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-question-mark-circle-icon';
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
export const TabsHelpAndClose: React.FunctionComponent = () => {
const [activeTabKey, setActiveTabKey] = useState<number>(0);
const [tabs, setTabs] = useState<string[]>(['Terminal 1', 'Terminal 2', 'Terminal 3']);
const tabComponentRef = useRef<any>(undefined);
const firstMount = useRef(true);
const onClose = (event: any, tabIndex: string | number) => {
const tabIndexNum = tabIndex as number;
let nextTabIndex = activeTabKey;
if (tabIndexNum < activeTabKey) {
// if a preceding tab is closing, keep focus on the new index of the current tab
nextTabIndex = activeTabKey - 1 > 0 ? activeTabKey - 1 : 0;
} else if (activeTabKey === tabs.length - 1) {
// if the closing tab is the last tab, focus the preceding tab
nextTabIndex = tabs.length - 2 > 0 ? tabs.length - 2 : 0;
}
setActiveTabKey(nextTabIndex);
setTabs(tabs.filter((tab, index) => index !== tabIndex));
};
const helpPopover = (header: string, popoverRef: React.RefObject<any>) => (
<Popover
headerContent={<div>{header}</div>}
bodyContent={
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id feugiat augue, nec fringilla turpis.
</div>
}
footerContent="Popover footer"
triggerRef={popoverRef}
/>
);
useEffect(() => {
if (firstMount.current) {
firstMount.current = false;
return;
} else {
const first = tabComponentRef.current.tabList.current.childNodes[activeTabKey];
first && first.firstChild.focus();
}
}, [tabs]);
return (
<Tabs
activeKey={activeTabKey}
onSelect={(event: any, tabIndex: string | number) => setActiveTabKey(tabIndex as number)}
aria-label="Tabs in the help enabled and closeable example"
role="region"
ref={tabComponentRef}
>
{tabs.map((tab, index) => {
const ref = createRef<HTMLElement>();
return (
<Tab
key={index}
eventKey={index}
aria-label={`Help action and closable content - ${tab}`}
title={<TabTitleText>{tab}</TabTitleText>}
actions={
<>
<TabAction aria-label={`Help for ${tab}`} ref={ref}>
<RhUiQuestionMarkCircleIcon />
</TabAction>
<TabAction
aria-label={`Close ${tab}`}
onClick={(e) => onClose(e, index)}
isDisabled={tabs.length === 1}
>
<RhMicronsCloseIcon />
</TabAction>
{helpPopover(`Help popover for ${tab}`, ref)}
</>
}
>
{tab}
</Tab>
);
})}
</Tabs>
);
};