-
Notifications
You must be signed in to change notification settings - Fork 782
/
TableRow.js
138 lines (126 loc) · 3.86 KB
/
TableRow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint no-nested-ternary: 0 */
import classSet from 'classnames';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Utils from './util';
class TableRow extends Component {
constructor(props) {
super(props);
this.clickNum = 0;
}
rowClick = e => {
const rowIndex = this.props.index + 1;
const cellIndex = e.target.cellIndex;
if (this.props.onRowClick) this.props.onRowClick(rowIndex, cellIndex, e);
const {
selectRow, unselectableRow, isSelected, onSelectRow, onExpandRow, dbClickToEdit
} = this.props;
if (selectRow) {
if (selectRow.clickToSelect && !unselectableRow) {
onSelectRow(rowIndex, !isSelected, e);
} else if (selectRow.clickToSelectAndEditCell && !unselectableRow) {
this.clickNum++;
/** if clickToSelectAndEditCell is enabled,
* there should be a delay to prevent a selection changed when
* user dblick to edit cell on same row but different cell
**/
setTimeout(() => {
if (this.clickNum === 1) {
onSelectRow(rowIndex, !isSelected, e);
onExpandRow(e, rowIndex, cellIndex);
}
this.clickNum = 0;
}, 200);
} else {
if (dbClickToEdit) {
this.expandRow(e, rowIndex, cellIndex);
}
}
}
}
expandRow = (event, rowIndex, cellIndex) => {
this.clickNum++;
setTimeout(() => {
if (this.clickNum === 1) {
this.props.onExpandRow(event, rowIndex, cellIndex);
}
this.clickNum = 0;
}, 200);
}
rowDoubleClick = e => {
if (e.target.tagName !== 'INPUT' &&
e.target.tagName !== 'SELECT' &&
e.target.tagName !== 'TEXTAREA') {
if (this.props.onRowDoubleClick) {
this.props.onRowDoubleClick(this.props.index, e);
}
}
}
rowMouseOut = e => {
const rowIndex = this.props.index;
if (this.props.onRowMouseOut) {
this.props.onRowMouseOut(rowIndex, e);
}
}
rowMouseOver = e => {
const rowIndex = this.props.index;
if (this.props.onRowMouseOver) {
this.props.onRowMouseOver(rowIndex, e);
}
}
render() {
this.clickNum = 0;
const { selectRow, row, isSelected, className, index, hidden } = this.props;
let { style } = this.props;
let backgroundColor = null;
let selectRowClass = null;
if (selectRow) {
backgroundColor = Utils.isFunction(selectRow.bgColor) ?
selectRow.bgColor(row, isSelected) : ( isSelected ? selectRow.bgColor : null);
selectRowClass = Utils.isFunction(selectRow.className) ?
selectRow.className(row, isSelected) : ( isSelected ? selectRow.className : null);
}
if (Utils.isFunction(style)) {
style = style(row, index);
} else {
style = { ...style } || {};
}
// the bgcolor of row selection always overwrite the bgcolor defined by global.
if (style && backgroundColor && isSelected) {
style.backgroundColor = backgroundColor;
}
const trCss = {
style: { ...style },
className: classSet(selectRowClass, className)
};
return (
<tr { ...trCss }
onMouseOver={ this.rowMouseOver }
onMouseOut={ this.rowMouseOut }
onClick={ this.rowClick }
hidden={ hidden }
onDoubleClick={ this.rowDoubleClick }>{ this.props.children }</tr>
);
}
}
TableRow.propTypes = {
index: PropTypes.number,
row: PropTypes.any,
style: PropTypes.any,
isSelected: PropTypes.bool,
enableCellEdit: PropTypes.bool,
onRowClick: PropTypes.func,
onRowDoubleClick: PropTypes.func,
onSelectRow: PropTypes.func,
onExpandRow: PropTypes.func,
onRowMouseOut: PropTypes.func,
onRowMouseOver: PropTypes.func,
unselectableRow: PropTypes.bool,
hidden: PropTypes.bool
};
TableRow.defaultProps = {
onRowClick: undefined,
onRowDoubleClick: undefined,
hidden: false
};
export default TableRow;