This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 552
/
FixedDataTableBufferedRows.react.js
180 lines (157 loc) · 5.22 KB
/
FixedDataTableBufferedRows.react.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
/**
* Copyright (c) 2015, Facebook, Inc.
* 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 FixedDataTableBufferedRows.react
* @typechecks
*/
var React = require('React');
var FixedDataTableRowBuffer = require('FixedDataTableRowBuffer');
var FixedDataTableRow = require('FixedDataTableRow.react');
var cx = require('cx');
var emptyFunction = require('emptyFunction');
var joinClasses = require('joinClasses');
var translateDOMPositionXY = require('translateDOMPositionXY');
var {PropTypes} = React;
var FixedDataTableBufferedRows = React.createClass({
propTypes: {
isScrolling: PropTypes.bool,
defaultRowHeight: PropTypes.number.isRequired,
firstRowIndex: PropTypes.number.isRequired,
firstRowOffset: PropTypes.number.isRequired,
fixedColumns: PropTypes.array.isRequired,
height: PropTypes.number.isRequired,
offsetTop: PropTypes.number.isRequired,
onRowClick: PropTypes.func,
onRowDoubleClick: PropTypes.func,
onRowMouseDown: PropTypes.func,
onRowMouseEnter: PropTypes.func,
onRowMouseLeave: PropTypes.func,
rowClassNameGetter: PropTypes.func,
rowsCount: PropTypes.number.isRequired,
rowHeightGetter: PropTypes.func,
rowPositionGetter: PropTypes.func.isRequired,
scrollLeft: PropTypes.number.isRequired,
scrollableColumns: PropTypes.array.isRequired,
showLastRowBorder: PropTypes.bool,
width: PropTypes.number.isRequired,
},
getInitialState() /*object*/ {
this._rowBuffer =
new FixedDataTableRowBuffer(
this.props.rowsCount,
this.props.defaultRowHeight,
this.props.height,
this._getRowHeight
);
return ({
rowsToRender: this._rowBuffer.getRows(
this.props.firstRowIndex,
this.props.firstRowOffset
),
});
},
componentWillMount() {
this._staticRowArray = [];
},
componentDidMount() {
setTimeout(this._updateBuffer, 1000);
},
componentWillReceiveProps(/*object*/ nextProps) {
if (nextProps.rowsCount !== this.props.rowsCount ||
nextProps.defaultRowHeight !== this.props.defaultRowHeight ||
nextProps.height !== this.props.height) {
this._rowBuffer =
new FixedDataTableRowBuffer(
nextProps.rowsCount,
nextProps.defaultRowHeight,
nextProps.height,
this._getRowHeight
);
}
if (this.props.isScrolling && !nextProps.isScrolling) {
this._updateBuffer();
} else {
this.setState({
rowsToRender: this._rowBuffer.getRows(
nextProps.firstRowIndex,
nextProps.firstRowOffset
),
});
}
},
_updateBuffer() {
if (this.isMounted()) {
this.setState({
rowsToRender: this._rowBuffer.getRowsWithUpdatedBuffer(),
});
}
},
shouldComponentUpdate() /*boolean*/ {
// Don't add PureRenderMixin to this component please.
return true;
},
componentWillUnmount() {
this._staticRowArray.length = 0;
},
render() /*object*/ {
var props = this.props;
var rowClassNameGetter = props.rowClassNameGetter || emptyFunction;
var rowPositionGetter = props.rowPositionGetter;
var rowsToRender = this.state.rowsToRender;
this._staticRowArray.length = rowsToRender.length;
for (var i = 0; i < rowsToRender.length; ++i) {
var rowIndex = rowsToRender[i];
var currentRowHeight = this._getRowHeight(rowIndex);
var rowOffsetTop = rowPositionGetter(rowIndex);
var hasBottomBorder =
rowIndex === props.rowsCount - 1 && props.showLastRowBorder;
this._staticRowArray[i] =
<FixedDataTableRow
key={i}
isScrolling={props.isScrolling}
index={rowIndex}
width={props.width}
height={currentRowHeight}
scrollLeft={Math.round(props.scrollLeft)}
offsetTop={Math.round(rowOffsetTop)}
fixedColumns={props.fixedColumns}
scrollableColumns={props.scrollableColumns}
onClick={props.onRowClick}
onDoubleClick={props.onRowDoubleClick}
onMouseDown={props.onRowMouseDown}
onMouseEnter={props.onRowMouseEnter}
onMouseLeave={props.onRowMouseLeave}
className={joinClasses(
rowClassNameGetter(rowIndex),
cx('public/fixedDataTable/bodyRow'),
cx({
'fixedDataTableLayout/hasBottomBorder': hasBottomBorder,
'public/fixedDataTable/hasBottomBorder': hasBottomBorder,
})
)}
/>;
}
var firstRowPosition = props.rowPositionGetter(props.firstRowIndex);
var style = {
position: 'absolute',
pointerEvents: props.isScrolling ? 'none' : 'auto',
};
translateDOMPositionXY(
style,
0,
props.firstRowOffset - firstRowPosition + props.offsetTop
);
return <div style={style}>{this._staticRowArray}</div>;
},
_getRowHeight(/*number*/ index) /*number*/ {
return this.props.rowHeightGetter ?
this.props.rowHeightGetter(index) :
this.props.defaultRowHeight;
},
});
module.exports = FixedDataTableBufferedRows;