forked from schrodinger/fixed-data-table-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedDataTableCellGroup.js
270 lines (225 loc) · 7.17 KB
/
FixedDataTableCellGroup.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright Schrodinger, LLC
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule FixedDataTableCellGroup
* @typechecks
*/
'use strict';
import FixedDataTableHelper from 'FixedDataTableHelper';
import React from 'React';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';
import FixedDataTableCell from 'FixedDataTableCell';
import cx from 'cx';
import FixedDataTableTranslateDOMPosition from 'FixedDataTableTranslateDOMPosition';
var DIR_SIGN = FixedDataTableHelper.DIR_SIGN;
var FixedDataTableCellGroupImpl = createReactClass({
displayName: 'FixedDataTableCellGroupImpl',
/**
* PropTypes are disabled in this component, because having them on slows
* down the FixedDataTable hugely in DEV mode. You can enable them back for
* development, but please don't commit this component with enabled propTypes.
*/
propTypes_DISABLED_FOR_PERFORMANCE: {
/**
* Array of <FixedDataTableColumn />.
*/
columns: PropTypes.array.isRequired,
isScrolling: PropTypes.bool,
left: PropTypes.number,
onColumnResize: PropTypes.func,
onColumnReorder: PropTypes.func,
onColumnReorderMove: PropTypes.func,
onColumnReorderEnd: PropTypes.func,
height: PropTypes.number.isRequired,
/**
* Height of fixedDataTableCellGroupLayout/cellGroupWrapper.
*/
cellGroupWrapperHeight: PropTypes.number,
rowHeight: PropTypes.number.isRequired,
rowIndex: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
zIndex: PropTypes.number.isRequired,
touchEnabled: PropTypes.bool
},
componentWillMount() {
this._initialRender = true;
},
componentDidMount() {
this._initialRender = false;
},
render() /*object*/ {
var props = this.props;
var columns = props.columns;
var cells = new Array(columns.length);
var contentWidth = this._getColumnsWidth(columns);
var isColumnReordering = props.isColumnReordering && columns.reduce(function (acc, column) {
return acc || props.columnReorderingData.columnKey === column.props.columnKey;
}, false);
var currentPosition = 0;
for (var i = 0, j = columns.length; i < j; i++) {
var columnProps = columns[i].props;
var recycable = columnProps.allowCellsRecycling && !isColumnReordering;
if (!recycable || (
currentPosition - props.left <= props.width &&
currentPosition - props.left + columnProps.width >= 0)) {
var key = columnProps.columnKey || 'cell_' + i;
cells[i] = this._renderCell(
props.rowIndex,
props.rowHeight,
columnProps,
currentPosition,
key,
contentWidth,
isColumnReordering
);
}
currentPosition += columnProps.width;
}
var style = {
height: props.height,
position: 'absolute',
width: contentWidth,
zIndex: props.zIndex,
};
FixedDataTableTranslateDOMPosition(style, -1 * DIR_SIGN * props.left, 0, this._initialRender);
return (
<div
className={cx('fixedDataTableCellGroupLayout/cellGroup')}
style={style}>
{cells}
</div>
);
},
_renderCell(
/*number*/ rowIndex,
/*number*/ height,
/*object*/ columnProps,
/*number*/ left,
/*string*/ key,
/*number*/ columnGroupWidth,
/*boolean*/ isColumnReordering,
) /*object*/ {
var cellIsResizable = columnProps.isResizable &&
this.props.onColumnResize;
var onColumnResize = cellIsResizable ? this.props.onColumnResize : null;
var cellIsReorderable = columnProps.isReorderable && this.props.onColumnReorder && rowIndex === -1 && columnGroupWidth !== columnProps.width;
var onColumnReorder = cellIsReorderable ? this.props.onColumnReorder : null;
var className = columnProps.cellClassName;
var pureRendering = columnProps.pureRendering || false;
return (
<FixedDataTableCell
isScrolling={this.props.isScrolling}
align={columnProps.align}
className={className}
height={height}
key={key}
maxWidth={columnProps.maxWidth}
minWidth={columnProps.minWidth}
touchEnabled={this.props.touchEnabled}
onColumnResize={onColumnResize}
onColumnReorder={onColumnReorder}
onColumnReorderMove={this.props.onColumnReorderMove}
onColumnReorderEnd={this.props.onColumnReorderEnd}
isColumnReordering={isColumnReordering}
columnReorderingData={this.props.columnReorderingData}
rowIndex={rowIndex}
columnKey={columnProps.columnKey}
width={columnProps.width}
left={left}
cell={columnProps.cell}
columnGroupWidth={columnGroupWidth}
pureRendering={pureRendering}
/>
);
},
_getColumnsWidth(/*array*/ columns) /*number*/ {
var width = 0;
for (var i = 0; i < columns.length; ++i) {
width += columns[i].props.width;
}
return width;
},
});
var FixedDataTableCellGroup = createReactClass({
displayName: 'FixedDataTableCellGroup',
/**
* PropTypes are disabled in this component, because having them on slows
* down the FixedDataTable hugely in DEV mode. You can enable them back for
* development, but please don't commit this component with enabled propTypes.
*/
propTypes_DISABLED_FOR_PERFORMANCE: {
isScrolling: PropTypes.bool,
/**
* Height of the row.
*/
height: PropTypes.number.isRequired,
offsetLeft: PropTypes.number,
left: PropTypes.number,
/**
* Z-index on which the row will be displayed. Used e.g. for keeping
* header and footer in front of other rows.
*/
zIndex: PropTypes.number.isRequired,
},
shouldComponentUpdate(/*object*/ nextProps) /*boolean*/ {
return (
!nextProps.isScrolling ||
this.props.rowIndex !== nextProps.rowIndex ||
this.props.left !== nextProps.left
);
},
getDefaultProps() /*object*/ {
return {
left: 0,
offsetLeft: 0,
};
},
render() /*object*/ {
var {offsetLeft, ...props} = this.props;
var style = {
height: props.cellGroupWrapperHeight || props.height,
width: props.width
};
if (DIR_SIGN === 1) {
style.left = offsetLeft;
} else {
style.right = offsetLeft;
}
var onColumnResize = props.onColumnResize ? this._onColumnResize : null;
return (
<div
style={style}
className={cx('fixedDataTableCellGroupLayout/cellGroupWrapper')}>
<FixedDataTableCellGroupImpl
{...props}
onColumnResize={onColumnResize}
/>
</div>
);
},
_onColumnResize(
/*number*/ left,
/*number*/ width,
/*?number*/ minWidth,
/*?number*/ maxWidth,
/*string|number*/ columnKey,
/*object*/ event
) {
this.props.onColumnResize && this.props.onColumnResize(
this.props.offsetLeft,
left - this.props.left + width,
width,
minWidth,
maxWidth,
columnKey,
event
);
},
});
module.exports = FixedDataTableCellGroup;