1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { getColumnClass } from './table-helpers' ;
4
- import { safeClick } from '../utils/event-handlers' ;
4
+ import { safeClick , stopPropagation } from '../utils/event-handlers' ;
5
5
import { getValueByPath } from '../utils/getters-setters' ;
6
6
7
- export function TableBody ( { columns, docs, rowClass, actions } ) {
7
+ export function TableBody ( { columns, docs, rowClass, actions, onRowClick } ) {
8
8
return (
9
9
< tbody >
10
10
{ docs . map ( ( doc , docIndex ) => (
11
- < tr key = { docIndex } className = { rowClass ( doc ) } >
11
+ < tr key = { docIndex } className = { rowClass ( doc ) } role = "button" onClick = { safeClick ( onRowClick , doc , docIndex ) } >
12
12
{ columns . map ( ( column , columnIndex ) => (
13
13
< td key = { columnIndex } className = { getColumnClass ( column ) } >
14
14
{ getColumnValue ( doc , column , docIndex ) }
@@ -21,7 +21,7 @@ export function TableBody({ columns, docs, rowClass, actions }) {
21
21
< a
22
22
key = { actionIndex }
23
23
title = { action . title }
24
- { ...getActionProps ( action , doc ) }
24
+ { ...getActionProps ( action , doc , docIndex ) }
25
25
className = { actionIndex > 0 ? 'ml-2' : '' }
26
26
>
27
27
{ action . content }
@@ -40,6 +40,7 @@ TableBody.propTypes = {
40
40
columns : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) ) ,
41
41
docs : PropTypes . arrayOf ( PropTypes . object ) ,
42
42
rowClass : PropTypes . func ,
43
+ onRowClick : PropTypes . func ,
43
44
} ;
44
45
45
46
function getColumnValue ( doc , column , docIndex ) {
@@ -52,14 +53,15 @@ function getColumnValue(doc, column, docIndex) {
52
53
return column . format ( rawValue , doc , docIndex ) ;
53
54
}
54
55
55
- function getActionProps ( action , doc ) {
56
+ function getActionProps ( action , doc , docIndex ) {
56
57
const props = { } ;
57
58
58
59
if ( action . onClick ) {
59
60
props . href = '' ;
60
- props . onClick = safeClick ( action . onClick , doc ) ;
61
+ props . onClick = safeClick ( action . onClick , doc , docIndex ) ;
61
62
} else if ( action . link ) {
62
- props . href = action . link ( doc ) ;
63
+ props . onClick = stopPropagation ;
64
+ props . href = action . link ( doc , docIndex ) ;
63
65
}
64
66
65
67
return props ;
0 commit comments