| 1 |
import { forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/TabContent/tab-content'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 5 |
import { TabsContextConsumer, TabsContextProps } from './TabsContext'; |
| 6 |
|
| 7 |
export interface TabContentProps extends Omit<React.HTMLProps<HTMLElement>, 'ref'>, OUIAProps { |
| 8 |
/** content rendered inside the tab content area if used outside Tabs component */ |
| 9 |
children?: any; |
| 10 |
/** Child to show in the content area */ |
| 11 |
child?: React.ReactElement<any>; |
| 12 |
/** class of tab content area if used outside Tabs component */ |
| 13 |
className?: string; |
| 14 |
/** Identifies the active Tab */ |
| 15 |
activeKey?: number | string; |
| 16 |
/** uniquely identifies the controlling Tab if used outside Tabs component */ |
| 17 |
eventKey?: number | string; |
| 18 |
/** @hide Callback for the section ref */ |
| 19 |
innerRef?: React.Ref<any>; |
| 20 |
/** id passed from parent to identify the content section */ |
| 21 |
id: string; |
| 22 |
/** title of controlling Tab if used outside Tabs component */ |
| 23 |
'aria-label'?: string; |
| 24 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 25 |
ouiaId?: number | string; |
| 26 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 27 |
ouiaSafe?: boolean; |
| 28 |
} |
| 29 |
|
| 30 |
const variantStyle = { |
| 31 |
default: '', |
| 32 |
secondary: styles.modifiers.secondary |
| 33 |
}; |
| 34 |
|
| 35 |
const TabContentBase: React.FunctionComponent<TabContentProps> = ({ |
| 36 |
id, |
| 37 |
activeKey, |
| 38 |
'aria-label': ariaLabel, |
| 39 |
child, |
| 40 |
children, |
| 41 |
className, |
| 42 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 43 |
eventKey, |
| 44 |
innerRef, |
| 45 |
ouiaId, |
| 46 |
ouiaSafe, |
| 47 |
...props |
| 48 |
}: TabContentProps) => { |
| 49 |
if (children || child) { |
| 50 |
let labelledBy: string; |
| 51 |
if (ariaLabel) { |
| 52 |
labelledBy = null; |
| 53 |
} else { |
| 54 |
labelledBy = children ? `${id}` : `pf-tab-${child.props.eventKey}-${id}`; |
| 55 |
} |
| 56 |
|
| 57 |
return ( |
| 58 |
<TabsContextConsumer> |
| 59 |
{({ variant }: TabsContextProps) => { |
| 60 |
const variantClass = variantStyle[variant]; |
| 61 |
|
| 62 |
return ( |
| 63 |
<section |
| 64 |
ref={innerRef} |
| 65 |
hidden={children ? null : child.props.eventKey !== activeKey} |
| 66 |
className={ |
| 67 |
children |
| 68 |
? css(styles.tabContent, className, variantClass) |
| 69 |
: css(styles.tabContent, child.props.className, variantClass) |
| 70 |
} |
| 71 |
id={children ? id : `pf-tab-section-${child.props.eventKey}-${id}`} |
| 72 |
aria-label={ariaLabel} |
| 73 |
aria-labelledby={labelledBy} |
| 74 |
role="tabpanel" |
| 75 |
tabIndex={0} |
| 76 |
{...getOUIAProps('TabContent', ouiaId, ouiaSafe)} |
| 77 |
{...props} |
| 78 |
> |
| 79 |
{children || child.props.children} |
| 80 |
</section> |
| 81 |
); |
| 82 |
}} |
| 83 |
</TabsContextConsumer> |
| 84 |
); |
| 85 |
} |
| 86 |
return null; |
| 87 |
}; |
| 88 |
|
| 89 |
export const TabContent = forwardRef((props: TabContentProps, ref: React.Ref<HTMLElement>) => ( |
| 90 |
<TabContentBase {...props} innerRef={ref} /> |
| 91 |
)); |