| 1 |
import { forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
|
| 5 |
export interface MenuGroupProps extends Omit<React.HTMLProps<HTMLElement>, 'label'> { |
| 6 |
/** Items within group */ |
| 7 |
children?: React.ReactNode; |
| 8 |
/** Additional classes added to the MenuGroup */ |
| 9 |
className?: string; |
| 10 |
/** Group label */ |
| 11 |
label?: React.ReactNode; |
| 12 |
/** ID for title label */ |
| 13 |
titleId?: string; |
| 14 |
/** @hide Forwarded ref */ |
| 15 |
innerRef?: React.Ref<any>; |
| 16 |
/** Group label heading level. Default is h1. */ |
| 17 |
labelHeadingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; |
| 18 |
} |
| 19 |
|
| 20 |
const MenuGroupBase: React.FunctionComponent<MenuGroupProps> = ({ |
| 21 |
children, |
| 22 |
className = '', |
| 23 |
label, |
| 24 |
titleId, |
| 25 |
innerRef, |
| 26 |
labelHeadingLevel: HeadingLevel = 'h1', |
| 27 |
...props |
| 28 |
}: MenuGroupProps) => { |
| 29 |
const Wrapper = typeof label === 'function' ? label : HeadingLevel; |
| 30 |
const hasLabel = label !== undefined && label !== null && label !== ''; |
| 31 |
|
| 32 |
return ( |
| 33 |
<section {...props} className={css(styles.menuGroup, className)} ref={innerRef}> |
| 34 |
<> |
| 35 |
{hasLabel && ( |
| 36 |
<> |
| 37 |
{['function', 'string'].includes(typeof label) ? ( |
| 38 |
<Wrapper className={css(styles.menuGroupTitle)} id={titleId}> |
| 39 |
{label} |
| 40 |
</Wrapper> |
| 41 |
) : ( |
| 42 |
label |
| 43 |
)} |
| 44 |
</> |
| 45 |
)} |
| 46 |
{children} |
| 47 |
</> |
| 48 |
</section> |
| 49 |
); |
| 50 |
}; |
| 51 |
|
| 52 |
export const MenuGroup = forwardRef((props: MenuGroupProps, ref: React.Ref<HTMLElement>) => ( |
| 53 |
<MenuGroupBase {...props} innerRef={ref} /> |
| 54 |
)); |
| 55 |
MenuGroup.displayName = 'MenuGroup'; |