| 1 |
import { Children, isValidElement, cloneElement } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Breadcrumb/breadcrumb'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { useOUIAProps, OUIAProps } from '../../helpers'; |
| 5 |
|
| 6 |
export interface BreadcrumbProps extends React.HTMLProps<HTMLElement>, OUIAProps { |
| 7 |
/** Children nodes be rendered to the BreadCrumb. Should be of type BreadCrumbItem. */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** Additional classes added to the breadcrumb nav. */ |
| 10 |
className?: string; |
| 11 |
/** Aria label added to the breadcrumb nav. */ |
| 12 |
'aria-label'?: string; |
| 13 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 14 |
ouiaId?: number | string; |
| 15 |
/** 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. */ |
| 16 |
ouiaSafe?: boolean; |
| 17 |
} |
| 18 |
|
| 19 |
export const Breadcrumb: React.FunctionComponent<BreadcrumbProps> = ({ |
| 20 |
children = null, |
| 21 |
className = '', |
| 22 |
'aria-label': ariaLabel = 'Breadcrumb', |
| 23 |
ouiaId, |
| 24 |
ouiaSafe = true, |
| 25 |
...props |
| 26 |
}: BreadcrumbProps) => { |
| 27 |
const ouiaProps = useOUIAProps(Breadcrumb.displayName, ouiaId, ouiaSafe); |
| 28 |
return ( |
| 29 |
<nav {...props} aria-label={ariaLabel} className={css(styles.breadcrumb, className)} {...ouiaProps}> |
| 30 |
<ol className={styles.breadcrumbList} role="list"> |
| 31 |
{Children.map(children, (child, index) => { |
| 32 |
const showDivider = index > 0; |
| 33 |
if (isValidElement(child)) { |
| 34 |
return cloneElement(child as React.ReactElement<any>, { showDivider }); |
| 35 |
} |
| 36 |
|
| 37 |
return child; |
| 38 |
})} |
| 39 |
</ol> |
| 40 |
</nav> |
| 41 |
); |
| 42 |
}; |
| 43 |
Breadcrumb.displayName = 'Breadcrumb'; |