| 1 |
import { createRef, useEffect, useRef, useState } from 'react'; |
| 2 |
import { Tabs, Tab, TabTitleText, Popover, TabAction } from '@patternfly/react-core'; |
| 3 |
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon'; |
| 4 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 5 |
|
| 6 |
export const TabsHelpAndClose: React.FunctionComponent = () => { |
| 7 |
const [activeTabKey, setActiveTabKey] = useState<number>(0); |
| 8 |
const [tabs, setTabs] = useState<string[]>(['Terminal 1', 'Terminal 2', 'Terminal 3']); |
| 9 |
const tabComponentRef = useRef<any>(undefined); |
| 10 |
const firstMount = useRef(true); |
| 11 |
|
| 12 |
const onClose = (event: any, tabIndex: string | number) => { |
| 13 |
const tabIndexNum = tabIndex as number; |
| 14 |
let nextTabIndex = activeTabKey; |
| 15 |
if (tabIndexNum < activeTabKey) { |
| 16 |
// if a preceding tab is closing, keep focus on the new index of the current tab |
| 17 |
nextTabIndex = activeTabKey - 1 > 0 ? activeTabKey - 1 : 0; |
| 18 |
} else if (activeTabKey === tabs.length - 1) { |
| 19 |
// if the closing tab is the last tab, focus the preceding tab |
| 20 |
nextTabIndex = tabs.length - 2 > 0 ? tabs.length - 2 : 0; |
| 21 |
} |
| 22 |
setActiveTabKey(nextTabIndex); |
| 23 |
setTabs(tabs.filter((tab, index) => index !== tabIndex)); |
| 24 |
}; |
| 25 |
|
| 26 |
const helpPopover = (header: string, popoverRef: React.RefObject<any>) => ( |
| 27 |
<Popover |
| 28 |
headerContent={<div>{header}</div>} |
| 29 |
bodyContent={ |
| 30 |
<div> |
| 31 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id feugiat augue, nec fringilla turpis. |
| 32 |
</div> |
| 33 |
} |
| 34 |
footerContent="Popover footer" |
| 35 |
triggerRef={popoverRef} |
| 36 |
/> |
| 37 |
); |
| 38 |
|
| 39 |
useEffect(() => { |
| 40 |
if (firstMount.current) { |
| 41 |
firstMount.current = false; |
| 42 |
return; |
| 43 |
} else { |
| 44 |
const first = tabComponentRef.current.tabList.current.childNodes[activeTabKey]; |
| 45 |
first && first.firstChild.focus(); |
| 46 |
} |
| 47 |
}, [tabs]); |
| 48 |
|
| 49 |
return ( |
| 50 |
<Tabs |
| 51 |
activeKey={activeTabKey} |
| 52 |
onSelect={(event: any, tabIndex: string | number) => setActiveTabKey(tabIndex as number)} |
| 53 |
aria-label="Tabs in the help enabled and closeable example" |
| 54 |
role="region" |
| 55 |
ref={tabComponentRef} |
| 56 |
> |
| 57 |
{tabs.map((tab, index) => { |
| 58 |
const ref = createRef<HTMLElement>(); |
| 59 |
|
| 60 |
return ( |
| 61 |
<Tab |
| 62 |
key={index} |
| 63 |
eventKey={index} |
| 64 |
aria-label={`Help action and closable content - ${tab}`} |
| 65 |
title={<TabTitleText>{tab}</TabTitleText>} |
| 66 |
actions={ |
| 67 |
<> |
| 68 |
<TabAction aria-label={`Help for ${tab}`} ref={ref}> |
| 69 |
<HelpIcon /> |
| 70 |
</TabAction> |
| 71 |
<TabAction |
| 72 |
aria-label={`Close ${tab}`} |
| 73 |
onClick={(e) => onClose(e, index)} |
| 74 |
isDisabled={tabs.length === 1} |
| 75 |
> |
| 76 |
<RhMicronsCloseIcon /> |
| 77 |
</TabAction> |
| 78 |
{helpPopover(`Help popover for ${tab}`, ref)} |
| 79 |
</> |
| 80 |
} |
| 81 |
> |
| 82 |
{tab} |
| 83 |
</Tab> |
| 84 |
); |
| 85 |
})} |
| 86 |
</Tabs> |
| 87 |
); |
| 88 |
}; |