Skip to content

Commit 08fd30f

Browse files
kiwiwongjiming
and
jiming
authored
fix: the style of the context menu of ActivityBarItem (#613)
* fix: fix the style of the context menu of ActivityBarItem * test: add unit test Co-authored-by: jiming <jiming@dtstack.com>
1 parent b11b4e9 commit 08fd30f

File tree

4 files changed

+74
-58
lines changed

4 files changed

+74
-58
lines changed

src/common/__tests__/dom.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { getPositionByPlacement } from '../dom';
2+
3+
describe('Test functions in dom.ts', () => {
4+
test('The getPositionByPlacement function', () => {
5+
const domRect = {
6+
x: 10,
7+
y: 10,
8+
width: 10,
9+
height: 10,
10+
top: 10,
11+
right: 10,
12+
bottom: 10,
13+
left: 10,
14+
} as DOMRect;
15+
let position;
16+
17+
position = getPositionByPlacement('top', domRect);
18+
expect(typeof position.x).toBe('number');
19+
expect(typeof position.y).toBe('number');
20+
21+
position = getPositionByPlacement('right', domRect);
22+
expect(typeof position.x).toBe('number');
23+
expect(typeof position.y).toBe('number');
24+
25+
position = getPositionByPlacement('bottom', domRect);
26+
expect(typeof position.x).toBe('number');
27+
expect(typeof position.y).toBe('number');
28+
29+
position = getPositionByPlacement('left', domRect);
30+
expect(typeof position.x).toBe('number');
31+
expect(typeof position.y).toBe('number');
32+
33+
position = getPositionByPlacement('rightBottom', domRect);
34+
expect(typeof position.x).toBe('number');
35+
expect(typeof position.y).toBe('number');
36+
});
37+
});

src/provider/__tests__/__snapshots__/molecule.test.tsx.snap

+4-9
Original file line numberDiff line numberDiff line change
@@ -734,15 +734,10 @@ dmlldzJfOV8xNjM4ODQ4MDI1NDI2MTE4Ml8zOF9bMF0TNSl1AAAAAElFTkSuQmCC"
734734
id="global.menu.settings"
735735
onClick={[Function]}
736736
>
737-
<div
738-
className="mo-drop-down mo-drop-down--rightBottom"
739-
onClick={[Function]}
740-
>
741-
<span
742-
className="mo-activityBar__label codicon codicon-settings-gear"
743-
title="Settings"
744-
/>
745-
</div>
737+
<span
738+
className="mo-activityBar__label codicon codicon-settings-gear"
739+
title="Settings"
740+
/>
746741
</li>
747742
</ul>
748743
</div>

src/workbench/activityBar/__tests__/activityBarItem.test.tsx

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
itemCheckedClassName,
99
itemDisabledClassName,
1010
labelClassName,
11+
itemClassName,
1112
} from '../base';
12-
import { defaultDropDownClassName } from 'mo/components/dropdown';
1313
import { keybindingClassName } from 'mo/components/menu/base';
1414
import { KeybindingHelper } from 'mo/services/keybinding';
1515
import { KeyCode } from 'mo/monaco';
@@ -105,9 +105,7 @@ describe('The ActivityBar Item Component', () => {
105105
<ActivityBarItem id="test" contextMenu={contexts} icon="type" />
106106
);
107107

108-
fireEvent.click(
109-
container.querySelector(`.${defaultDropDownClassName}`)!
110-
);
108+
fireEvent.click(container.querySelector(`.${itemClassName}`)!);
111109

112110
const menus = getByRole('menu');
113111
expect(menus).toBeInTheDocument();
@@ -144,9 +142,7 @@ describe('The ActivityBar Item Component', () => {
144142
const { container, getByRole } = render(
145143
<ActivityBarItem id="test" contextMenu={contexts} icon="type" />
146144
);
147-
fireEvent.click(
148-
container.querySelector(`.${defaultDropDownClassName}`)!
149-
);
145+
fireEvent.click(container.querySelector(`.${itemClassName}`)!);
150146

151147
const menus = getByRole('menu');
152148
// the first menu have keybinding
@@ -187,9 +183,7 @@ describe('The ActivityBar Item Component', () => {
187183
onContextMenuClick={mockFn}
188184
/>
189185
);
190-
fireEvent.click(
191-
container.querySelector(`.${defaultDropDownClassName}`)!
192-
);
186+
fireEvent.click(container.querySelector(`.${itemClassName}`)!);
193187

194188
const menus = getByRole('menu');
195189
fireEvent.click(menus.firstElementChild!);

src/workbench/activityBar/activityBarItem.tsx

+29-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { useRef } from 'react';
32
import { classNames } from 'mo/common/className';
43
import { IActivityBarItem } from 'mo/model/workbench/activityBar';
54
import { IMenuItemProps, Menu } from 'mo/components/menu';
@@ -12,8 +11,7 @@ import {
1211
itemCheckedClassName,
1312
itemDisabledClassName,
1413
} from './base';
15-
import { DropDown, Icon } from 'mo/components';
16-
import { DropDownRef } from 'mo/components/dropdown';
14+
import { Icon, useContextView } from 'mo/components';
1715
import { KeybindingHelper } from 'mo/services/keybinding';
1816

1917
export function ActivityBarItem(
@@ -33,29 +31,7 @@ export function ActivityBarItem(
3331
onContextMenuClick,
3432
} = props;
3533

36-
const contextMenuRef = useRef<DropDownRef>(null);
37-
38-
const onClickMenuItem = (
39-
e: React.MouseEvent,
40-
item: IMenuItemProps | undefined
41-
) => {
42-
onContextMenuClick?.(e, item);
43-
contextMenuRef.current?.dispose();
44-
};
45-
46-
const onClickItem = function (event) {
47-
if (onClick) {
48-
onClick(props.id, props);
49-
}
50-
};
51-
52-
const content = (
53-
<Icon type={icon} className={labelClassName} title={title}>
54-
{render?.() || null}
55-
</Icon>
56-
);
57-
58-
const overlay = (
34+
const renderContextMenu = () => (
5935
<Menu
6036
onClick={onClickMenuItem}
6137
role="menu"
@@ -75,7 +51,32 @@ export function ActivityBarItem(
7551
/>
7652
);
7753

78-
const hasContextMenu = contextMenu.length > 0;
54+
const contextView = useContextView({
55+
render: renderContextMenu,
56+
});
57+
58+
const onClickMenuItem = (
59+
e: React.MouseEvent,
60+
item: IMenuItemProps | undefined
61+
) => {
62+
onContextMenuClick?.(e, item);
63+
contextView?.hide();
64+
};
65+
66+
const onClickItem = function (event) {
67+
if (onClick) {
68+
onClick(props.id, props);
69+
}
70+
if (contextMenu.length > 0) {
71+
contextView.show({ x: event.pageX, y: event.pageY });
72+
}
73+
};
74+
75+
const content = (
76+
<Icon type={icon} className={labelClassName} title={title}>
77+
{render?.() || null}
78+
</Icon>
79+
);
7980

8081
return (
8182
<li
@@ -89,18 +90,7 @@ export function ActivityBarItem(
8990
)}
9091
data-id={data.id}
9192
>
92-
{hasContextMenu ? (
93-
<DropDown
94-
ref={contextMenuRef}
95-
trigger="click"
96-
placement="rightBottom"
97-
overlay={overlay}
98-
>
99-
{content}
100-
</DropDown>
101-
) : (
102-
content
103-
)}
93+
{content}
10494
{checked ? <div className={indicatorClassName}></div> : null}
10595
</li>
10696
);

0 commit comments

Comments
 (0)