| 1 |
import { createRef, Fragment, useState } from 'react'; |
| 2 |
import { Tabs, Tab, TabContent, Button, Divider } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const TabsToggledSeparateContent: React.FunctionComponent = () => { |
| 5 |
const [activeTabKey, setActiveTabKey] = useState<string | number>(0); |
| 6 |
const [isTab2Hidden, setIsTab2Hidden] = useState<boolean>(false); |
| 7 |
// Toggle currently active tab |
| 8 |
const handleTabClick = ( |
| 9 |
event: React.MouseEvent<any> | React.KeyboardEvent | MouseEvent, |
| 10 |
tabIndex: string | number |
| 11 |
) => { |
| 12 |
setActiveTabKey(tabIndex); |
| 13 |
}; |
| 14 |
|
| 15 |
const contentRef1 = createRef<HTMLElement>(); |
| 16 |
const contentRef2 = createRef<HTMLElement>(); |
| 17 |
const contentRef3 = createRef<HTMLElement>(); |
| 18 |
|
| 19 |
return ( |
| 20 |
<Fragment> |
| 21 |
<Button onClick={() => setIsTab2Hidden(!isTab2Hidden)}>{isTab2Hidden ? 'Show' : 'Hide'} tab 2</Button> |
| 22 |
<Divider style={{ paddingTop: '1rem', paddingBottom: '1rem' }} /> |
| 23 |
<Tabs |
| 24 |
activeKey={activeTabKey} |
| 25 |
onSelect={handleTabClick} |
| 26 |
aria-label="Tabs in the toggled separate content example" |
| 27 |
role="region" |
| 28 |
> |
| 29 |
<Tab eventKey={0} title="Tab item 1" tabContentId="tab1SectionSeparateContent" tabContentRef={contentRef1} /> |
| 30 |
{!isTab2Hidden && ( |
| 31 |
<Tab eventKey={1} title="Tab item 2" tabContentId="tab2SectionSeparateContent" tabContentRef={contentRef2} /> |
| 32 |
)} |
| 33 |
<Tab eventKey={2} title="Tab item 3" tabContentId="tab3SectionSeparateContent" tabContentRef={contentRef3} /> |
| 34 |
</Tabs> |
| 35 |
<div> |
| 36 |
<TabContent |
| 37 |
eventKey={0} |
| 38 |
id="tab1SectionSeparateContent" |
| 39 |
ref={contentRef1} |
| 40 |
aria-label="This is content for the first toggled separate content tab" |
| 41 |
> |
| 42 |
Tab 1 section |
| 43 |
</TabContent> |
| 44 |
{!isTab2Hidden && ( |
| 45 |
<TabContent |
| 46 |
eventKey={1} |
| 47 |
id="tab2SectionSeparateContent" |
| 48 |
ref={contentRef2} |
| 49 |
aria-label="This is content for the second toggled separate content tab" |
| 50 |
hidden |
| 51 |
> |
| 52 |
Tab 2 section |
| 53 |
</TabContent> |
| 54 |
)} |
| 55 |
<TabContent |
| 56 |
eventKey={2} |
| 57 |
id="tab3SectionSeparateContent" |
| 58 |
ref={contentRef3} |
| 59 |
aria-label="This is content for the third toggled separate content tab" |
| 60 |
hidden |
| 61 |
> |
| 62 |
Tab 3 section |
| 63 |
</TabContent> |
| 64 |
</div> |
| 65 |
</Fragment> |
| 66 |
); |
| 67 |
}; |