Skip to content

Commit

Permalink
Fix click on table row with interactive elements
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszbachorski committed Oct 1, 2024
1 parent 0304aef commit 0675a7f
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ import type { DataTableViewProps } from './DataTable'

export interface DataTableTableViewSpecificProps<Data> {
onRowClick?: (data: Data, event: MouseEvent) => void
/**
* Determines whether event is valid row click. Some table rows include interactive elements
* isRowClicked allows excluding clicks that were bubbled up
* */
isRowClicked?: (event: MouseEvent) => boolean
}

const isRowClickedDefault = (event: MouseEvent) =>
(event.target as HTMLElement).tagName === 'TD'

Check warning on line 33 in packages/design-system/src/components/DataTable/DataTableTableView.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/DataTable/DataTableTableView.tsx#L33

Added line #L33 was not covered by tests

interface DataTableTableViewProps<Data>
extends DataTableViewProps<Data>,
DataTableTableViewSpecificProps<Data> {}
Expand All @@ -32,6 +40,7 @@ export const DataTableTableView = <Data,>({
table,
entityName,
onRowClick,
isRowClicked = isRowClickedDefault,
}: DataTableTableViewProps<Data>) => {
const rows = table.getRowModel().rows
return (
Expand Down Expand Up @@ -74,7 +83,11 @@ export const DataTableTableView = <Data,>({
data-state={row.getIsSelected() && 'selected'}
onClick={
onRowClick ?
(event) => onRowClick(row.original, event)
(event) => {
if (isRowClicked(event)) {
onRowClick(row.original, event)
}
}

Check warning on line 90 in packages/design-system/src/components/DataTable/DataTableTableView.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/DataTable/DataTableTableView.tsx#L86-L90

Added lines #L86 - L90 were not covered by tests
: undefined
}
>
Expand Down

0 comments on commit 0675a7f

Please sign in to comment.