-
Notifications
You must be signed in to change notification settings - Fork 45
/
DropdownMenu.tsx
60 lines (55 loc) · 1.49 KB
/
DropdownMenu.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
import Menu, { MenuItem } from 'rc-menu';
import * as React from 'react';
import MentionsContext from './MentionsContext';
import type { DataDrivenOptionProps } from './Mentions';
interface DropdownMenuProps {
prefixCls?: string;
options: DataDrivenOptionProps[];
}
/**
* We only use Menu to display the candidate.
* The focus is controlled by textarea to make accessibility easy.
*/
function DropdownMenu(props: DropdownMenuProps) {
const {
notFoundContent,
activeIndex,
setActiveIndex,
selectOption,
onFocus,
onBlur,
} = React.useContext(MentionsContext);
const { prefixCls, options } = props;
const activeOption = options[activeIndex] || {};
return (
<Menu
prefixCls={`${prefixCls}-menu`}
activeKey={activeOption.key}
onSelect={({ key }) => {
const option = options.find(({ key: optionKey }) => optionKey === key);
selectOption(option);
}}
onFocus={onFocus}
onBlur={onBlur}
>
{options.map((option, index) => {
const { key, disabled, className, style, label } = option;
return (
<MenuItem
key={key}
disabled={disabled}
className={className}
style={style}
onMouseEnter={() => {
setActiveIndex(index);
}}
>
{label}
</MenuItem>
);
})}
{!options.length && <MenuItem disabled>{notFoundContent}</MenuItem>}
</Menu>
);
}
export default DropdownMenu;