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 1 commit
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
70 changes: 70 additions & 0 deletions packages/eds-data-grid-react/src/EdsDataGrid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,76 @@ RowSelection.args = {
columnResizeMode: 'onChange',
} satisfies Partial<EdsDataGridProps<Photo>>

const BasicModal = ({
yusijs marked this conversation as resolved.
Show resolved Hide resolved
text,
top,
left,
onHide,
}: {
text: string
top: number
left: number
onHide: () => void
}) => (
<div
id="modal"
style={{
position: 'absolute',
top,
left,
zIndex: 10,
backgroundColor: '#fff',
padding: '1rem',
border: '1px solid #000',
width: 'fit-content',
textAlign: 'right',
}}
>
<Typography>{text}</Typography>
<br />
<Button onClick={() => onHide()}>Close</Button>
</div>
)

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

const showModal = (
row: Row<Photo>,
event: React.MouseEvent<HTMLTableRowElement>,
) => {
event.preventDefault()
event.stopPropagation()
setModalProps({
text: `Row id: ${row.original.id} - Opening position (${event.pageX},${event.pageY})`,
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 && <BasicModal {...modalProps} />}
<EdsDataGrid {...args} onRowContextMenu={showModal} />
</>
)
}

RowContextmenuPopup.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