From 5058acfaf930332668e18d68e2854a922c01b99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20L=C3=A5stad?= Date: Mon, 21 Oct 2024 13:51:45 +0200 Subject: [PATCH] EDS Data Grid Feature: onRowContextMenu (#3658) * Feature: onRowContextMenu added * onRowContextMenu - updated storybook with menu --- .../src/EdsDataGrid.stories.tsx | 101 ++++++++++++++++++ .../eds-data-grid-react/src/EdsDataGrid.tsx | 11 ++ .../src/EdsDataGridProps.ts | 10 ++ .../src/components/TableRow.tsx | 8 +- .../src/tests/EdsDataGrid.test.tsx | 21 ++++ 5 files changed, 150 insertions(+), 1 deletion(-) diff --git a/packages/eds-data-grid-react/src/EdsDataGrid.stories.tsx b/packages/eds-data-grid-react/src/EdsDataGrid.stories.tsx index 4d55213c1a..9eb0654445 100644 --- a/packages/eds-data-grid-react/src/EdsDataGrid.stories.tsx +++ b/packages/eds-data-grid-react/src/EdsDataGrid.stories.tsx @@ -2,6 +2,8 @@ import { Button, Checkbox, Divider, + Icon, + Menu, Pagination, Paper, TextField, @@ -35,6 +37,7 @@ import { } from './stories/columns' import { data, summaryData } from './stories/data' import { Virtualizer } from './types' +import { external_link } from '@equinor/eds-icons' const meta: Meta> = { title: 'EDS Data grid', @@ -286,6 +289,104 @@ RowSelection.args = { columnResizeMode: 'onChange', } satisfies Partial> +const SimpleMenu = ({ + text, + top, + left, + onHide, +}: { + text: string + top: number + left: number + onHide: () => void +}): JSX.Element => { + const [anchorEl, setAnchorEl] = useState(null) + return ( +
+ + + + + EDS homepage + + + + + + Equinor.com + + + + + +
+ ) +} + +export const RowContextMenu: StoryFn> = (args) => { + const [isOpen, setIsOpen] = useState(false) + const [menuProps, setMenuProps] = useState<{ + text: string + top: number + left: number + onHide: () => void + } | null>(null) + + const showMenu = ( + row: Row, + event: React.MouseEvent, + ) => { + event.preventDefault() + event.stopPropagation() + setMenuProps({ + text: `Menu for row id: ${row.original.id}`, + top: event.pageY, + left: event.pageX, + onHide: () => setIsOpen(false), + }) + setIsOpen(true) + } + + return ( + <> + Right click a row to open a basic popup. + +
+ {isOpen && } + + + ) +} + +RowContextMenu.args = {} satisfies Partial> + export const Paging: StoryFn> = (args) => { return } diff --git a/packages/eds-data-grid-react/src/EdsDataGrid.tsx b/packages/eds-data-grid-react/src/EdsDataGrid.tsx index 534e3122a4..0be4aebc56 100644 --- a/packages/eds-data-grid-react/src/EdsDataGrid.tsx +++ b/packages/eds-data-grid-react/src/EdsDataGrid.tsx @@ -91,6 +91,7 @@ export function EdsDataGrid({ setExpansionState, getSubRows, defaultColumn, + onRowContextMenu, onRowClick, onCellClick, enableFooter, @@ -459,6 +460,11 @@ export function EdsDataGrid({ onRowContextMenu(row, event) + : undefined + } onClick={ onRowClick ? (event) => onRowClick(row, event) @@ -491,6 +497,11 @@ export function EdsDataGrid({ onRowContextMenu(row, event) + : undefined + } onClick={ onRowClick ? (event) => onRowClick(row, event) : undefined } diff --git a/packages/eds-data-grid-react/src/EdsDataGridProps.ts b/packages/eds-data-grid-react/src/EdsDataGridProps.ts index b527790ee3..71ec1e02af 100644 --- a/packages/eds-data-grid-react/src/EdsDataGridProps.ts +++ b/packages/eds-data-grid-react/src/EdsDataGridProps.ts @@ -210,6 +210,16 @@ type FilterProps = { } type HandlersProps = { + /** + * + * @param row the current row + * @param event The right-click event + * @returns + */ + onRowContextMenu?: ( + row: Row, + event: MouseEvent, + ) => unknown /** * Row click handler. * diff --git a/packages/eds-data-grid-react/src/components/TableRow.tsx b/packages/eds-data-grid-react/src/components/TableRow.tsx index 2afb5b8782..5427f3fcd3 100644 --- a/packages/eds-data-grid-react/src/components/TableRow.tsx +++ b/packages/eds-data-grid-react/src/components/TableRow.tsx @@ -11,7 +11,12 @@ type Props = { onCellClick?: EdsDataGridProps['onCellClick'] } & HTMLAttributes -export function TableRow({ row, onCellClick, onClick }: Props) { +export function TableRow({ + row, + onCellClick, + onClick, + onContextMenu, +}: Props) { const { rowClass, rowStyle } = useTableContext() return ( @@ -21,6 +26,7 @@ export function TableRow({ row, onCellClick, onClick }: Props) { }} className={`${row.getIsSelected() ? 'selected' : ''} ${rowClass?.(row)}`} onClick={onClick} + onContextMenu={onContextMenu} > {row.getVisibleCells().map((cell) => ( { await userEvent.click(screen.getAllByRole('row')[1]) expect(spy).toHaveBeenCalledTimes(1) }) + it('right-click should call onRowSelectionChange if enableRowSelection is set', async () => { + const spy = jest.fn() + render( + + row.getCanSelect() ? row.toggleSelected() : null + } + columns={columns} + rows={data} + />, + ) + + await userEvent.pointer({ + keys: '[MouseRight]', + target: screen.getAllByRole('row')[1], + }) + + expect(spy).toHaveBeenCalledTimes(1) + }) }) describe('Paging', () => {