-
Notifications
You must be signed in to change notification settings - Fork 32
/
menu.tsx
58 lines (53 loc) · 2 KB
/
menu.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
import React, {
Children, cloneElement,
ComponentProps, FC, ReactElement
} from 'react'
import classNames from 'classnames'
import { EbayFakeMenuItemProps } from './index'
import { EbayKeyboardEventHandler, EbayMouseEventHandler } from '../common/event-utils/types'
type SpanProps = Omit<ComponentProps<'div'>, 'onKeyDown' | 'onSelect'>
type Props = SpanProps & {
itemMatchesUrl?: boolean;
onKeyDown?: EbayKeyboardEventHandler<HTMLElement, { index: number }>;
onSelect?: EbayMouseEventHandler<HTMLAnchorElement, { index: number }>;
}
const EbayFakeMenu: FC<Props> = ({
className,
itemMatchesUrl = true,
onKeyDown = () => {},
onSelect = () => {},
children,
...rest
}) => {
const childrenArray = Children.toArray(children)
const defaultAriaCurrent = itemMatchesUrl === false ? 'true' : 'page'
return (
<div {...rest} className={classNames(className, 'fake-menu')}>
<ul className="fake-menu__items" tabIndex={-1}>
{childrenArray.map((child: ReactElement, i) => {
const {
current,
onClick = () => {},
...itemRest
}: EbayFakeMenuItemProps = child.props
return (
<li key={i}>
{cloneElement(child, {
...itemRest,
'aria-current': current ? defaultAriaCurrent : undefined,
onClick: e => {
onSelect(e, { index: i })
onClick(e)
},
onKeyDown: e => {
onKeyDown(e, { index: i })
}
} as EbayFakeMenuItemProps)}
</li>
)
})}
</ul>
</div>
)
}
export default EbayFakeMenu