Skip to content

Commit

Permalink
Fix click on table row with interactive elements (#57)
Browse files Browse the repository at this point in the history
# Fix click on table row with interactive elements


## ⚙️ Release Notes 
* Fixes click on table rows that contain interactive elements

### Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
arkadiuszbachorski authored Oct 1, 2024
1 parent 0304aef commit 9a80edf
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'

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)
}
}
: undefined
}
>
Expand Down

0 comments on commit 9a80edf

Please sign in to comment.