-
Notifications
You must be signed in to change notification settings - Fork 28
/
breadcrumbs.tsx
61 lines (56 loc) · 2.05 KB
/
breadcrumbs.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React, { Children, cloneElement, ComponentProps, FC, ReactElement, ReactNode } from 'react'
import classNames from 'classnames'
import { EbayEventHandler } from '../common/event-utils/types'
type BreadcrumbProps = Omit<ComponentProps<'div'>, 'onSelect'> & {
/**
* Breadcrumbs expects `<EbayBreadcrumbItem/>` as children.
* Other elements will not work.
*
* @see Docs https://github.com/eBay/ebayui-core-react/tree/main/src/components/ebay-breadcrumb#usage
*/
children: ReactNode;
id?: string;
a11yHeadingTag?: keyof JSX.IntrinsicElements;
a11yHeadingText?: string;
onSelect?: EbayEventHandler<HTMLElement>;
}
const Breadcrumbs: FC<BreadcrumbProps> = ({
a11yHeadingText = 'Page navigation',
a11yHeadingTag = 'h2',
id = 'ebay-breadcrumb',
children: breadcrumbItems = [],
className,
onSelect = () => {},
...rest
}) => {
const headingId = `${id}-breadcrumbs-heading`
const lastItemIndex = Children.count(breadcrumbItems) - 1
const A11yHeadingTag = a11yHeadingTag
const anyLink = Children.toArray(breadcrumbItems).some((item: ReactElement) => item.props.href)
const tag = anyLink ? 'a' : 'button'
return (
<nav
{...rest}
aria-labelledby={headingId}
className={classNames('breadcrumbs', className)}
role="navigation"
>
<A11yHeadingTag id={headingId} className="clipped">{a11yHeadingText}</A11yHeadingTag>
<ul>
{Children.map(breadcrumbItems, (item: ReactElement, index) => {
const isLastItem = index === lastItemIndex
const { href, children } = item.props
const itemProps = {
tag,
isLastItem,
href,
children,
onClick: event => onSelect(event)
}
return cloneElement(item, itemProps)
})}
</ul>
</nav>
)
}
export default Breadcrumbs