| 1 |
import { useContext } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Accordion/accordion'; |
| 4 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 5 |
import { AccordionContext, AccordionItemContext } from './AccordionContext'; |
| 6 |
|
| 7 |
export interface AccordionToggleProps extends React.DetailedHTMLProps< |
| 8 |
React.ButtonHTMLAttributes<HTMLButtonElement>, |
| 9 |
HTMLButtonElement |
| 10 |
> { |
| 11 |
/** Content rendered inside the Accordion toggle */ |
| 12 |
children?: React.ReactNode; |
| 13 |
/** Additional classes added to the Accordion Toggle */ |
| 14 |
className?: string; |
| 15 |
/** Identify the Accordion toggle number */ |
| 16 |
id: string; |
| 17 |
/** Container to override the default for toggle */ |
| 18 |
component?: React.ElementType; |
| 19 |
} |
| 20 |
|
| 21 |
export const AccordionToggle: React.FunctionComponent<AccordionToggleProps> = ({ |
| 22 |
className = '', |
| 23 |
id, |
| 24 |
children = null, |
| 25 |
component, |
| 26 |
...props |
| 27 |
}: AccordionToggleProps) => { |
| 28 |
const renderToggleIcon = () => ( |
| 29 |
<span className={css(styles.accordionToggleIcon)}> |
| 30 |
<RhMicronsCaretDownIcon /> |
| 31 |
</span> |
| 32 |
); |
| 33 |
|
| 34 |
const { isExpanded } = useContext(AccordionItemContext); |
| 35 |
|
| 36 |
return ( |
| 37 |
<AccordionContext.Consumer> |
| 38 |
{({ ToggleContainer, togglePosition }) => { |
| 39 |
const Container = component || ToggleContainer; |
| 40 |
const isToggleStartPositioned = togglePosition === 'start'; |
| 41 |
|
| 42 |
return ( |
| 43 |
<Container> |
| 44 |
<button |
| 45 |
id={id} |
| 46 |
className={css(styles.accordionToggle, isExpanded && styles.modifiers.expanded, className)} |
| 47 |
aria-expanded={isExpanded} |
| 48 |
type="button" |
| 49 |
{...props} |
| 50 |
> |
| 51 |
{isToggleStartPositioned && renderToggleIcon()} |
| 52 |
<span className={css(styles.accordionToggleText)}>{children}</span> |
| 53 |
{!isToggleStartPositioned && renderToggleIcon()} |
| 54 |
</button> |
| 55 |
</Container> |
| 56 |
); |
| 57 |
}} |
| 58 |
</AccordionContext.Consumer> |
| 59 |
); |
| 60 |
}; |
| 61 |
AccordionToggle.displayName = 'AccordionToggle'; |