| 1 |
import { useContext, useEffect, useRef, useState } from 'react'; |
| 2 |
|
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Accordion/accordion'; |
| 5 |
import { AccordionContext, AccordionItemContext } from './AccordionContext'; |
| 6 |
import { AccordionExpandableContentBody, AccordionExpandableContentBodyProps } from './AccordionExpandableContentBody'; |
| 7 |
|
| 8 |
export interface AccordionContentProps extends React.HTMLProps<HTMLDivElement> { |
| 9 |
/** Content rendered inside the Accordion */ |
| 10 |
children?: React.ReactNode; |
| 11 |
/** Additional classes added to the Accordion content */ |
| 12 |
className?: string; |
| 13 |
/** Identify the AccordionContent item */ |
| 14 |
id?: string; |
| 15 |
/** Flag to indicate Accordion content is fixed */ |
| 16 |
isFixed?: boolean; |
| 17 |
/** Adds accessible text to the Accordion content */ |
| 18 |
'aria-label'?: string; |
| 19 |
/** Id of the controlling accordion toggle to label the content. */ |
| 20 |
'aria-labelledby'?: string; |
| 21 |
/** Component to use as content container */ |
| 22 |
component?: React.ElementType; |
| 23 |
/** Flag indicating content is custom. Expanded content Body wrapper will be removed from children. This allows multiple bodies to be rendered as content. */ |
| 24 |
isCustomContent?: React.ReactNode; |
| 25 |
/** Props passed to the AccordionExpandableContentBody **/ |
| 26 |
contentBodyProps?: AccordionExpandableContentBodyProps; |
| 27 |
} |
| 28 |
|
| 29 |
export const AccordionContent: React.FunctionComponent<AccordionContentProps> = ({ |
| 30 |
className = '', |
| 31 |
children = null, |
| 32 |
id = '', |
| 33 |
isFixed = false, |
| 34 |
isCustomContent = false, |
| 35 |
'aria-label': ariaLabel = '', |
| 36 |
'aria-labelledby': ariaLabelledby, |
| 37 |
component, |
| 38 |
contentBodyProps, |
| 39 |
...props |
| 40 |
}: AccordionContentProps) => { |
| 41 |
const [hasScrollbar, setHasScrollbar] = useState(false); |
| 42 |
const containerRef = useRef(null); |
| 43 |
const { isExpanded } = useContext(AccordionItemContext); |
| 44 |
|
| 45 |
useEffect(() => { |
| 46 |
if (containerRef?.current && isFixed && isExpanded) { |
| 47 |
const { offsetHeight, scrollHeight } = containerRef.current; |
| 48 |
|
| 49 |
setHasScrollbar(offsetHeight < scrollHeight); |
| 50 |
} else if (!isFixed) { |
| 51 |
setHasScrollbar(false); |
| 52 |
} |
| 53 |
}, [containerRef, isFixed, isExpanded]); |
| 54 |
|
| 55 |
return ( |
| 56 |
<AccordionContext.Consumer> |
| 57 |
{({ ContentContainer }) => { |
| 58 |
const Container = component || ContentContainer; |
| 59 |
return ( |
| 60 |
<Container |
| 61 |
ref={containerRef} |
| 62 |
id={id} |
| 63 |
className={css(styles.accordionExpandableContent, isFixed && styles.modifiers.fixed, className)} |
| 64 |
hidden={!isExpanded} |
| 65 |
{...(ariaLabel && { 'aria-label': ariaLabel })} |
| 66 |
{...(ariaLabelledby && { 'aria-labelledby': ariaLabelledby })} |
| 67 |
{...(hasScrollbar && { tabIndex: 0 })} |
| 68 |
{...(hasScrollbar && Container === 'div' && { role: 'region' })} |
| 69 |
{...props} |
| 70 |
> |
| 71 |
{isCustomContent ? ( |
| 72 |
children |
| 73 |
) : ( |
| 74 |
<AccordionExpandableContentBody {...contentBodyProps}>{children}</AccordionExpandableContentBody> |
| 75 |
)} |
| 76 |
</Container> |
| 77 |
); |
| 78 |
}} |
| 79 |
</AccordionContext.Consumer> |
| 80 |
); |
| 81 |
}; |
| 82 |
AccordionContent.displayName = 'AccordionContent'; |