Skip to content

Commit dc3c2c5

Browse files
committed
Use React composition instead of arrays of data
1 parent d2d7ab0 commit dc3c2c5

File tree

2 files changed

+78
-73
lines changed

2 files changed

+78
-73
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
3+
import {
4+
css,
5+
Icon,
6+
MenuItem,
7+
palette,
8+
spacing,
9+
type GlyphName,
10+
} from '@mongodb-js/compass-components';
11+
12+
const styles = css({
13+
display: 'flex',
14+
alignItems: 'center',
15+
gap: spacing[2],
16+
});
17+
18+
type OptionMenuItemProps = {
19+
label: string;
20+
icon: GlyphName;
21+
onClick: () => void;
22+
setMenuOpen: (open: boolean) => void;
23+
};
24+
25+
export function OptionMenuItem({
26+
label,
27+
onClick,
28+
setMenuOpen,
29+
icon,
30+
}: OptionMenuItemProps) {
31+
return (
32+
<MenuItem
33+
data-text={label}
34+
onClick={() => {
35+
setMenuOpen(false);
36+
onClick();
37+
}}
38+
>
39+
<div className={styles}>
40+
<Icon color={palette.gray.dark2} glyph={icon} size="small" />
41+
{label}
42+
</div>
43+
</MenuItem>
44+
);
45+
}

packages/compass-aggregations/src/components/stage-toolbar/option-menu.tsx

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import React, { useMemo, useState } from 'react';
1+
import React, { useState } from 'react';
22
import { connect } from 'react-redux';
3-
import {
4-
Menu,
5-
MenuItem,
6-
css,
7-
IconButton,
8-
Icon,
9-
palette,
10-
spacing,
11-
} from '@mongodb-js/compass-components';
3+
import { Menu, IconButton, Icon } from '@mongodb-js/compass-components';
124
import {
135
addStage,
146
removeStage,
@@ -17,12 +9,7 @@ import {
179
} from '../../modules/pipeline-builder/stage-editor';
1810
import type { StoreStage } from '../../modules/pipeline-builder/stage-editor';
1911
import type { RootState } from '../../modules';
20-
21-
const menuItemStyles = css({
22-
display: 'flex',
23-
alignItems: 'center',
24-
gap: spacing[2],
25-
});
12+
import { OptionMenuItem } from './option-menu-item';
2613

2714
export const OptionMenu = ({
2815
index,
@@ -39,51 +26,6 @@ export const OptionMenu = ({
3926
}) => {
4027
const [menuOpen, setMenuOpen] = useState(false);
4128

42-
const menuItems = useMemo(() => {
43-
return [
44-
{
45-
label: 'Add stage after',
46-
onClick: () => {
47-
onAddStageClick(index);
48-
setMenuOpen(false);
49-
},
50-
icon: 'PlusWithCircle',
51-
},
52-
{
53-
label: 'Add stage before',
54-
onClick: () => {
55-
onAddStageClick(index - 1);
56-
setMenuOpen(false);
57-
},
58-
icon: 'PlusWithCircle',
59-
},
60-
{
61-
label: 'Delete stage',
62-
onClick: () => {
63-
onDeleteStageClick(index);
64-
setMenuOpen(false);
65-
},
66-
icon: 'Trash',
67-
},
68-
{
69-
label: 'Expand documents',
70-
onClick: () => {
71-
onExpand(index);
72-
setMenuOpen(false);
73-
},
74-
icon: 'ChevronDown',
75-
},
76-
{
77-
label: 'Collapse documents',
78-
onClick: () => {
79-
onCollapse(index);
80-
setMenuOpen(false);
81-
},
82-
icon: 'ChevronUp',
83-
},
84-
];
85-
}, [index, onAddStageClick, onDeleteStageClick, onExpand, onCollapse]);
86-
8729
return (
8830
<Menu
8931
open={menuOpen}
@@ -105,18 +47,36 @@ export const OptionMenu = ({
10547
);
10648
}}
10749
>
108-
{menuItems.map((item) => (
109-
<MenuItem
110-
key={item.label}
111-
data-text={item.label}
112-
onClick={item.onClick}
113-
>
114-
<div className={menuItemStyles}>
115-
<Icon color={palette.gray.dark2} glyph={item.icon} size="small" />
116-
{item.label}
117-
</div>
118-
</MenuItem>
119-
))}
50+
<OptionMenuItem
51+
label="Add stage after"
52+
icon="PlusWithCircle"
53+
onClick={() => onAddStageClick(index)}
54+
setMenuOpen={setMenuOpen}
55+
/>
56+
<OptionMenuItem
57+
label="Add stage before"
58+
icon="PlusWithCircle"
59+
onClick={() => onAddStageClick(index - 1)}
60+
setMenuOpen={setMenuOpen}
61+
/>
62+
<OptionMenuItem
63+
label="Delete stage"
64+
icon="Trash"
65+
onClick={() => onDeleteStageClick(index)}
66+
setMenuOpen={setMenuOpen}
67+
/>
68+
<OptionMenuItem
69+
label="Expand documents"
70+
icon="ChevronDown"
71+
onClick={() => onExpand(index)}
72+
setMenuOpen={setMenuOpen}
73+
/>
74+
<OptionMenuItem
75+
label="Collapse documents"
76+
icon="ChevronUp"
77+
onClick={() => onCollapse(index)}
78+
setMenuOpen={setMenuOpen}
79+
/>
12080
</Menu>
12181
);
12282
};

0 commit comments

Comments
 (0)