| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Accordion/accordion'; |
| 3 |
import { AccordionContext } from './AccordionContext'; |
| 4 |
|
| 5 |
export interface AccordionProps extends React.HTMLProps<HTMLDListElement> { |
| 6 |
/** Content rendered inside the Accordion */ |
| 7 |
children?: React.ReactNode; |
| 8 |
/** Additional classes added to the Accordion */ |
| 9 |
className?: string; |
| 10 |
/** Adds accessible text to the Accordion */ |
| 11 |
'aria-label'?: string; |
| 12 |
/** Heading level to use */ |
| 13 |
headingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; |
| 14 |
/** Flag to indicate whether use definition list or div */ |
| 15 |
asDefinitionList?: boolean; |
| 16 |
/** Flag to indicate the accordion had a border */ |
| 17 |
isBordered?: boolean; |
| 18 |
/** @beta Flag to prevent the accordion from automatically applying plain styling when glass theme is enabled. */ |
| 19 |
isNoPlainOnGlass?: boolean; |
| 20 |
/** @beta Flag to add plain styling to the accordion. */ |
| 21 |
isPlain?: boolean; |
| 22 |
/** Display size variant. */ |
| 23 |
displaySize?: 'default' | 'lg'; |
| 24 |
/** Sets the toggle icon position for all accordion toggles. */ |
| 25 |
togglePosition?: 'start' | 'end'; |
| 26 |
} |
| 27 |
|
| 28 |
export const Accordion: React.FunctionComponent<AccordionProps> = ({ |
| 29 |
children = null, |
| 30 |
className = '', |
| 31 |
'aria-label': ariaLabel, |
| 32 |
headingLevel = 'h3', |
| 33 |
asDefinitionList = true, |
| 34 |
isBordered = false, |
| 35 |
isNoPlainOnGlass = false, |
| 36 |
isPlain = false, |
| 37 |
displaySize = 'default', |
| 38 |
togglePosition = 'end', |
| 39 |
...props |
| 40 |
}: AccordionProps) => { |
| 41 |
const AccordionList: any = asDefinitionList ? 'dl' : 'div'; |
| 42 |
return ( |
| 43 |
<AccordionList |
| 44 |
className={css( |
| 45 |
styles.accordion, |
| 46 |
isBordered && styles.modifiers.bordered, |
| 47 |
isNoPlainOnGlass && styles.modifiers.noPlainOnGlass, |
| 48 |
isPlain && styles.modifiers.plain, |
| 49 |
togglePosition === 'start' && styles.modifiers.toggleStart, |
| 50 |
displaySize === 'lg' && styles.modifiers.displayLg, |
| 51 |
className |
| 52 |
)} |
| 53 |
aria-label={ariaLabel} |
| 54 |
{...(!asDefinitionList && ariaLabel && { role: 'region' })} |
| 55 |
{...props} |
| 56 |
> |
| 57 |
<AccordionContext.Provider |
| 58 |
value={{ |
| 59 |
ContentContainer: asDefinitionList ? 'dd' : 'div', |
| 60 |
ToggleContainer: asDefinitionList ? 'dt' : headingLevel, |
| 61 |
togglePosition |
| 62 |
}} |
| 63 |
> |
| 64 |
{children} |
| 65 |
</AccordionContext.Provider> |
| 66 |
</AccordionList> |
| 67 |
); |
| 68 |
}; |
| 69 |
Accordion.displayName = 'Accordion'; |