-
Notifications
You must be signed in to change notification settings - Fork 120
/
menuItem.tsx
94 lines (88 loc) · 2.28 KB
/
menuItem.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React from 'react';
import { classNames } from 'mo/common/className';
import type { HTMLElementProps, UniqueId } from 'mo/common/types';
import {
checkClassName,
disabledClassName,
defaultMenuItemClassName,
keybindingClassName,
labelClassName,
menuContentClassName,
} from './base';
import { Icon } from '../icon';
export interface IMenuItemProps extends HTMLElementProps {
id: UniqueId;
/**
* The name of icon
*/
icon?: string | JSX.Element;
type?: 'divider';
/**
* Item Name
*/
name?: string;
disabled?: boolean;
/**
* The description of keybinding
* example: ⇧⌘P
*/
keybinding?: string;
/**
* Custom render
*/
render?: (data: IMenuItemProps) => React.ReactNode;
onClick?: (e: React.MouseEvent, item: IMenuItemProps) => void;
sortIndex?: number;
[key: string]: any;
}
export function MenuItem(
props: React.PropsWithChildren<Omit<IMenuItemProps, 'id'>>
) {
const {
icon,
disabled = false,
className,
onClick,
keybinding,
render,
children,
name,
title,
id,
sortIndex,
...restProps
} = props;
const events = {
onClick: function (e: React.MouseEvent) {
onClick?.(e, props);
},
};
return (
<li
className={classNames(
defaultMenuItemClassName,
className,
disabled ? disabledClassName : null
)}
id={id?.toString()}
data-sort={sortIndex}
// prevent render JSX title in HTMLElement
{...(typeof title === 'string' ? { title } : {})}
{...events}
{...restProps}
>
<div className={menuContentClassName}>
<Icon type={icon} className={checkClassName} />
<span
className={labelClassName}
title={typeof name === 'string' ? name : ''}
>
{render ? render(props) : children}
</span>
{keybinding ? (
<span className={keybindingClassName}>{keybinding}</span>
) : null}
</div>
</li>
);
}