-
Notifications
You must be signed in to change notification settings - Fork 120
/
index.tsx
156 lines (139 loc) · 4.4 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from 'react';
import { useCallback, useEffect, useRef } from 'react';
import {
prefixClaName,
classNames,
getBEMElement,
getBEMModifier,
} from 'mo/common/className';
import { IContextMenu, useContextMenu } from 'mo/components/contextMenu';
import { IMenuItemProps, Menu } from 'mo/components/menu';
import { mergeFunctions } from 'mo/common/utils';
import Tooltip from '../tooltip';
import { Icon } from '../icon';
import type { HTMLElementProps, UniqueId } from 'mo/common/types';
import { getDataAttributionsFromProps } from 'mo/common/dom';
export interface IActionBarItemProps<T = any> {
id: UniqueId;
title?: string | JSX.Element;
name?: React.ReactNode;
icon?: string | JSX.Element;
disabled?: boolean;
checked?: boolean;
data?: T;
contextMenu?: IMenuItemProps[];
onContextMenuClick?: (
e: React.MouseEvent,
item: IMenuItemProps | undefined
) => void;
onClick?(event: React.MouseEvent, item: IActionBarItemProps): void;
[key: string]: any;
}
export interface IActionBarProps<T = any> extends HTMLElementProps {
data: IActionBarItemProps<T>[];
onContextMenuClick?: (
e: React.MouseEvent,
item: IMenuItemProps | undefined
) => void;
onClick?(event: React.MouseEvent, item: IActionBarItemProps): void;
[key: string]: any;
}
const defaultActionBarClassName = prefixClaName('action-bar');
const containerClassName = getBEMElement(
defaultActionBarClassName,
'container'
);
const itemClassName = getBEMElement(defaultActionBarClassName, 'item');
const itemDisabledClassName = getBEMModifier(itemClassName, 'disabled');
const itemCheckedClassName = getBEMModifier(itemClassName, 'checked');
export function ActionBarItem(props: IActionBarItemProps) {
const {
title,
name,
contextMenu = [],
onClick,
icon,
onContextMenuClick,
...restProps
} = props;
const disabled = props.disabled ? itemDisabledClassName : null;
const checked = props.checked ? itemCheckedClassName : null;
const refItem = useRef<HTMLLIElement>(null);
const contextViewMenu = useRef<IContextMenu>();
const onClickMenuItem = useCallback(
(e: React.MouseEvent, item: IMenuItemProps | undefined) => {
onContextMenuClick?.(e, item);
contextViewMenu.current?.hide();
},
[contextMenu]
);
const renderContextMenu = () => (
<Menu onClick={onClickMenuItem} data={contextMenu} />
);
useEffect(() => {
if (contextMenu.length > 0) {
contextViewMenu.current = useContextMenu({
anchor: refItem.current,
render: renderContextMenu,
});
}
return function cleanup() {
contextViewMenu.current?.dispose();
};
});
const onClickItem = function (event) {
if (props.disabled) {
event.preventDefault();
return;
}
if (onClick) {
onClick(event, props);
}
if (contextMenu.length > -1 && contextViewMenu.current) {
contextViewMenu.current.show({
x: event.clientX,
y: event.clientY,
});
}
};
const dataProps = getDataAttributionsFromProps(restProps);
return (
<li
ref={refItem}
className={classNames(itemClassName, disabled, checked)}
onClick={onClickItem}
{...dataProps}
>
<Tooltip overlay={<span>{title}</span>}>
<Icon type={icon}>{name}</Icon>
</Tooltip>
</li>
);
}
export function ActionBar<T = any>(props: IActionBarProps<T>) {
const {
data = [],
onClick,
onContextMenuClick,
className,
style,
title,
...restProps
} = props;
const claNames = classNames(defaultActionBarClassName, className);
const items = data.map((item, index) => (
<ActionBarItem
key={item.id}
{...item}
onContextMenuClick={onContextMenuClick}
data-index={index}
onClick={mergeFunctions(onClick, item.onClick)}
/>
));
const dataProps = getDataAttributionsFromProps(restProps);
return (
<div className={claNames} style={style} title={title} {...dataProps}>
<ul className={containerClassName}>{items}</ul>
</div>
);
}