Skip to content

Commit 67e13e0

Browse files
authored
fix: table viz context menu (lightdash#3004)
1 parent 3f56991 commit 67e13e0

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed
Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,71 @@
1-
import { Menu, MenuItem } from '@blueprintjs/core';
2-
import { ContextMenu2 } from '@blueprintjs/popover2';
1+
import { Menu, Position } from '@blueprintjs/core';
2+
import { MenuItem2, Popover2 } from '@blueprintjs/popover2';
33
import { ResultRow } from '@lightdash/common';
4-
import React from 'react';
4+
import { Cell } from '@tanstack/react-table';
5+
import { cloneElement, FC, isValidElement } from 'react';
56
import { CellContextMenuProps, TableColumn } from '../common/Table/types';
67
import { useUnderlyingDataContext } from '../UnderlyingData/UnderlyingDataProvider';
78

8-
export const CellContextMenu: React.FC<CellContextMenuProps> = ({
9+
interface ContextMenuProps {
10+
cell: Cell<ResultRow>;
11+
meta: TableColumn['meta'];
12+
}
13+
14+
const ContextMenu: FC<ContextMenuProps> = ({ meta, cell }) => {
15+
const { viewData } = useUnderlyingDataContext();
16+
17+
const value: ResultRow[0]['value'] = cell.getValue()?.value || {};
18+
19+
return (
20+
<Menu>
21+
<MenuItem2
22+
text="View underlying data"
23+
icon="layers"
24+
onClick={() => {
25+
viewData(value, meta, cell.row.original || {});
26+
}}
27+
/>
28+
</Menu>
29+
);
30+
};
31+
32+
const CellContextMenu: FC<CellContextMenuProps> = ({
33+
boundaryElement,
934
children,
1035
cell,
36+
onOpen,
37+
onClose,
1138
}) => {
1239
const meta = cell.column.columnDef.meta as TableColumn['meta'];
1340
const item = meta?.item;
14-
const { viewData } = useUnderlyingDataContext();
1541

16-
if (item) {
17-
const value: ResultRow[0]['value'] = cell.getValue()?.value || {};
18-
return (
19-
<ContextMenu2
20-
content={
21-
<Menu>
22-
<MenuItem
23-
text={`View underlying data`}
24-
icon={'layers'}
25-
onClick={(e) => {
26-
viewData(value, meta, cell.row.original || {});
27-
}}
28-
/>
29-
</Menu>
30-
}
31-
>
32-
{children}
33-
</ContextMenu2>
34-
);
42+
if (!item || !boundaryElement) {
43+
return <>{children}</>;
3544
}
36-
return <>{children}</>;
45+
46+
return (
47+
<Popover2
48+
minimal
49+
lazy
50+
position={Position.BOTTOM_RIGHT}
51+
boundary={boundaryElement}
52+
content={<ContextMenu cell={cell} meta={meta} />}
53+
renderTarget={({ ref, ...targetProps }) => {
54+
if (isValidElement(children)) {
55+
return cloneElement(children, {
56+
ref,
57+
...targetProps,
58+
});
59+
} else {
60+
throw new Error(
61+
'CellContextMenu children must be a valid React element',
62+
);
63+
}
64+
}}
65+
onOpening={onOpen}
66+
onClosing={onClose}
67+
/>
68+
);
3769
};
70+
71+
export default CellContextMenu;

packages/frontend/src/components/SimpleTable/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { FC } from 'react';
1+
import { FC } from 'react';
22
import Table from '../common/Table';
33
import { useVisualizationContext } from '../LightdashVisualization/VisualizationProvider';
44
import { LoadingChart } from '../SimpleChart';
5-
import { CellContextMenu } from './CellContextMenu';
5+
import CellContextMenu from './CellContextMenu';
66
import { TableWrapper } from './SimpleTable.styles';
77

88
const SimpleTable: FC = () => {

0 commit comments

Comments
 (0)