Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EDS Data Grid Feature: onRowContextMenu #3658

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions packages/eds-data-grid-react/src/EdsDataGrid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
Button,
Checkbox,
Divider,
Icon,
Menu,
Pagination,
Paper,
TextField,
Expand Down Expand Up @@ -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<typeof EdsDataGrid<Photo>> = {
title: 'EDS Data grid',
Expand Down Expand Up @@ -286,6 +289,104 @@ RowSelection.args = {
columnResizeMode: 'onChange',
} satisfies Partial<EdsDataGridProps<Photo>>

const SimpleMenu = ({
text,
top,
left,
onHide,
}: {
text: string
top: number
left: number
onHide: () => void
}): JSX.Element => {
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)
return (
<div
id="anchor"
ref={setAnchorEl}
style={{
position: 'absolute',
top: top,
left: left,
}}
>
<Menu
open
id="menu-as"
aria-labelledby="anchor-as"
onClose={onHide}
anchorEl={anchorEl}
placement="right-start"
>
<Menu.Section title={text}>
<Menu.Item
as="a"
onClick={onHide}
href="https://eds.equinor.com/"
target="_blank"
style={{ justifyContent: 'space-between' }}
>
<Typography group="navigation" variant="menu_title" as="span">
EDS homepage
</Typography>
<Icon data={external_link} size={16} />
</Menu.Item>
<Menu.Item
as="a"
onClick={onHide}
href="https://equinor.com/"
target="_blank"
style={{ justifyContent: 'space-between' }}
>
<Typography group="navigation" variant="menu_title" as="span">
Equinor.com
</Typography>
<Icon data={external_link} size={16} />
</Menu.Item>
</Menu.Section>
</Menu>
</div>
)
}

export const RowContextMenu: StoryFn<EdsDataGridProps<Photo>> = (args) => {
const [isOpen, setIsOpen] = useState(false)
const [menuProps, setMenuProps] = useState<{
text: string
top: number
left: number
onHide: () => void
} | null>(null)

const showMenu = (
row: Row<Photo>,
event: React.MouseEvent<HTMLTableRowElement>,
) => {
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 (
<>
<Typography>Right click a row to open a basic popup.</Typography>
<Divider />
<br />
{isOpen && <SimpleMenu {...menuProps} />}
<EdsDataGrid {...args} onRowContextMenu={showMenu} />
</>
)
}

RowContextMenu.args = {} satisfies Partial<EdsDataGridProps<Photo>>

export const Paging: StoryFn<EdsDataGridProps<Photo>> = (args) => {
return <EdsDataGrid {...args} />
}
Expand Down
11 changes: 11 additions & 0 deletions packages/eds-data-grid-react/src/EdsDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function EdsDataGrid<T>({
setExpansionState,
getSubRows,
defaultColumn,
onRowContextMenu,
onRowClick,
onCellClick,
enableFooter,
Expand Down Expand Up @@ -459,6 +460,11 @@ export function EdsDataGrid<T>({
<TableRow
key={virtualItem.index}
row={row}
onContextMenu={
onRowContextMenu
? (event) => onRowContextMenu(row, event)
: undefined
}
onClick={
onRowClick
? (event) => onRowClick(row, event)
Expand Down Expand Up @@ -491,6 +497,11 @@ export function EdsDataGrid<T>({
<TableRow
key={row.id}
row={row}
onContextMenu={
onRowContextMenu
? (event) => onRowContextMenu(row, event)
: undefined
}
onClick={
onRowClick ? (event) => onRowClick(row, event) : undefined
}
Expand Down
10 changes: 10 additions & 0 deletions packages/eds-data-grid-react/src/EdsDataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ type FilterProps = {
}

type HandlersProps<T> = {
/**
*
* @param row the current row
* @param event The right-click event
* @returns
*/
onRowContextMenu?: (
row: Row<T>,
event: MouseEvent<HTMLTableRowElement>,
) => unknown
/**
* Row click handler.
*
Expand Down
8 changes: 7 additions & 1 deletion packages/eds-data-grid-react/src/components/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ type Props<T> = {
onCellClick?: EdsDataGridProps<T>['onCellClick']
} & HTMLAttributes<HTMLTableRowElement>

export function TableRow<T>({ row, onCellClick, onClick }: Props<T>) {
export function TableRow<T>({
row,
onCellClick,
onClick,
onContextMenu,
}: Props<T>) {
const { rowClass, rowStyle } = useTableContext()

return (
Expand All @@ -21,6 +26,7 @@ export function TableRow<T>({ row, onCellClick, onClick }: Props<T>) {
}}
className={`${row.getIsSelected() ? 'selected' : ''} ${rowClass?.(row)}`}
onClick={onClick}
onContextMenu={onContextMenu}
>
{row.getVisibleCells().map((cell) => (
<TableBodyCell
Expand Down
21 changes: 21 additions & 0 deletions packages/eds-data-grid-react/src/tests/EdsDataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,27 @@ describe('EdsDataGrid', () => {
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(
<EdsDataGrid
enableRowSelection
onRowSelectionChange={spy}
onRowContextMenu={(row) =>
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', () => {
Expand Down
Loading