-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDropdownSubMenuItem.tsx
More file actions
40 lines (35 loc) · 1.23 KB
/
DropdownSubMenuItem.tsx
File metadata and controls
40 lines (35 loc) · 1.23 KB
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
/** @jsxImportSource @emotion/react */
import { useRef } from 'react';
import * as Style from '../style';
import { DROPDOWN_MENU_OPEN_CLASS_NAME, NOT_DISABLED_DROPDOWN_SUB_ITEM_QUERY } from '../constants';
import { keyboardHandler } from '../utils/keyboardHandler';
import { selectItem } from '../utils/selectItem';
import type { DropdownSubMenuItemProps } from '../types';
export const SubMenuItem = (props: DropdownSubMenuItemProps) => {
const menuItemRef = useRef<HTMLLIElement>(null);
const { children, onClick, disabled, ...otherProps } = props;
const onKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
if (!menuItemRef.current) return;
keyboardHandler({
event,
parentScope: DROPDOWN_MENU_OPEN_CLASS_NAME,
selectorOfList: NOT_DISABLED_DROPDOWN_SUB_ITEM_QUERY
});
};
return (
<li
ref={menuItemRef}
role="menuitem"
tabIndex={disabled ? -1 : 0}
className={`sub_item ${disabled ? 'disabled' : ''}`}
onClick={e => selectItem(e, onClick)}
aria-disabled={!!disabled}
css={{ ...Style.createDropdownItem(!!disabled) }}
onKeyDown={e => onKeyDown(e)}
{...otherProps}
>
{children}
</li>
);
};
SubMenuItem.displayName = 'Dropdown.SubMenuItem';