Skip to content

Commit 214bc7c

Browse files
committed
feat(table): allow row click with "onRowClick" prop
1 parent 5dba43f commit 214bc7c

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

demo/TableExamples.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function TableExamples() {
7373
{ a: 4, b: 5, c: 6 },
7474
{ a: 7, b: 8, c: 9 },
7575
]}
76+
onRowClick={console.log}
7677
/>
7778
</div>
7879

@@ -150,6 +151,7 @@ export function TableExamples() {
150151
{ a: 4, b: 5, c: 6 },
151152
{ a: 7, b: 8, c: 9 },
152153
]}
154+
onRowClick={console.info}
153155
actions={[
154156
{
155157
title: 'View details',

demo/demo.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ReactDOM.render(
1616
<ToastsContainer>
1717
<StatefulTabs
1818
vertical={true}
19-
initialTab={1}
19+
initialTab={4}
2020
tabs={[
2121
{
2222
title: 'Dialog',

src/table/Table.jsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function Table({
1414
dark,
1515
rowClass,
1616
caption,
17+
onRowClick,
1718
actions,
1819
actionLabel,
1920
columnHeaderFormat,
@@ -34,7 +35,7 @@ export function Table({
3435
<table className={tableClasses}>
3536
{caption && <caption>{caption}</caption>}
3637
<TableHead {...{ actions, actionLabel, columnHeaderFormat }} columns={normalizedColumns} />
37-
<TableBody {...{ docs, rowClass, actions }} columns={normalizedColumns} />
38+
<TableBody {...{ docs, rowClass, actions, onRowClick }} columns={normalizedColumns} />
3839
</table>
3940
</div>
4041
);
@@ -48,6 +49,7 @@ Table.defaultProps = {
4849
dark: false,
4950
actionLabel: 'Actions',
5051
rowClass: () => '',
52+
onRowClick: () => {},
5153
columnHeaderFormat: (label) => label,
5254
};
5355

@@ -61,6 +63,7 @@ Table.propTypes = {
6163
dark: PropTypes.bool,
6264
docs: PropTypes.arrayOf(PropTypes.object),
6365
hover: PropTypes.bool,
66+
onRowClick: PropTypes.func,
6467
rowClass: PropTypes.func,
6568
small: PropTypes.bool,
6669
striped: PropTypes.bool,

src/table/TableBody.jsx

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import { getColumnClass } from './table-helpers';
4-
import { safeClick } from '../utils/event-handlers';
4+
import { safeClick, stopPropagation } from '../utils/event-handlers';
55
import { getValueByPath } from '../utils/getters-setters';
66

7-
export function TableBody({ columns, docs, rowClass, actions }) {
7+
export function TableBody({ columns, docs, rowClass, actions, onRowClick }) {
88
return (
99
<tbody>
1010
{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)}>
1212
{columns.map((column, columnIndex) => (
1313
<td key={columnIndex} className={getColumnClass(column)}>
1414
{getColumnValue(doc, column, docIndex)}
@@ -21,7 +21,7 @@ export function TableBody({ columns, docs, rowClass, actions }) {
2121
<a
2222
key={actionIndex}
2323
title={action.title}
24-
{...getActionProps(action, doc)}
24+
{...getActionProps(action, doc, docIndex)}
2525
className={actionIndex > 0 ? 'ml-2' : ''}
2626
>
2727
{action.content}
@@ -40,6 +40,7 @@ TableBody.propTypes = {
4040
columns: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
4141
docs: PropTypes.arrayOf(PropTypes.object),
4242
rowClass: PropTypes.func,
43+
onRowClick: PropTypes.func,
4344
};
4445

4546
function getColumnValue(doc, column, docIndex) {
@@ -52,14 +53,15 @@ function getColumnValue(doc, column, docIndex) {
5253
return column.format(rawValue, doc, docIndex);
5354
}
5455

55-
function getActionProps(action, doc) {
56+
function getActionProps(action, doc, docIndex) {
5657
const props = {};
5758

5859
if (action.onClick) {
5960
props.href = '';
60-
props.onClick = safeClick(action.onClick, doc);
61+
props.onClick = safeClick(action.onClick, doc, docIndex);
6162
} else if (action.link) {
62-
props.href = action.link(doc);
63+
props.onClick = stopPropagation;
64+
props.href = action.link(doc, docIndex);
6365
}
6466

6567
return props;

0 commit comments

Comments
 (0)