|
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'; |
3 | 3 | import { ResultRow } from '@lightdash/common';
|
4 |
| -import React from 'react'; |
| 4 | +import { Cell } from '@tanstack/react-table'; |
| 5 | +import { cloneElement, FC, isValidElement } from 'react'; |
5 | 6 | import { CellContextMenuProps, TableColumn } from '../common/Table/types';
|
6 | 7 | import { useUnderlyingDataContext } from '../UnderlyingData/UnderlyingDataProvider';
|
7 | 8 |
|
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, |
9 | 34 | children,
|
10 | 35 | cell,
|
| 36 | + onOpen, |
| 37 | + onClose, |
11 | 38 | }) => {
|
12 | 39 | const meta = cell.column.columnDef.meta as TableColumn['meta'];
|
13 | 40 | const item = meta?.item;
|
14 |
| - const { viewData } = useUnderlyingDataContext(); |
15 | 41 |
|
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}</>; |
35 | 44 | }
|
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 | + ); |
37 | 69 | };
|
| 70 | + |
| 71 | +export default CellContextMenu; |
0 commit comments