-
Notifications
You must be signed in to change notification settings - Fork 3
/
godforsaken-dynamic-width-scroll-table.js
141 lines (117 loc) · 4.15 KB
/
godforsaken-dynamic-width-scroll-table.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
var Rx = require('rx');
var React = require('react');
var assign = require('object-assign');
var getColumnWidths = require('./get-column-widths');
var FixedScrollTable = React.createClass({
getInitialState: function () {
return {
visibleIndices: []
};
},
render: function () {
// render the container first,
// then render into that later with knowledge of the container
var containerStyle = {
position: 'relative',
height: this.props.containerHeight,
overflowX: 'hidden'
};
return (
<div className="fixed-scroll-element-container">
<div
ref="HeaderContainer"
className="fixed-scroll-element-header">
</div>
<div
ref="Container"
className="fixed-scroll-element"
style={containerStyle}/>
</div>
);
},
deferredRender: function (columnWidths, containerWidth) {
// this is the render for when the container has been rendered
// and we know explicitly the container width
var rows = this.state.visibleIndices.map((itemIndex, keyIndex) => {
var top = itemIndex * this.props.rowHeight;
return this.props.rowGetter(itemIndex, keyIndex, top, columnWidths, containerWidth);
});
return (
<table
style={{height: this.props.rowCount * this.props.rowHeight}}>
<tbody>
{rows}
</tbody>
</table>
);
},
componentDidUpdate: function () {
// do the actual render when the component updates itself
var containerWidth = this.containerNode.offsetWidth;
var computedColumnWidths = getColumnWidths(containerWidth, this.props.columnWidths);
var output = this.deferredRender(computedColumnWidths, containerWidth);
React.render(output, this.containerNode);
// render the header with the same constraints as the rows
React.render((
<table>
<thead>
{this.props.headerGetter(computedColumnWidths, containerWidth)}
</thead>
</table>
), this.headerContainerNode);
},
componentDidMount: function () {
// get the nodes and store them, don't want to look them up every time
this.containerNode = React.findDOMNode(this.refs.Container);
this.headerContainerNode = React.findDOMNode(this.refs.HeaderContainer);
// set up the table and the scroll observer
this.setUpTable();
// have the component rerender when the window resizes,
// as the container width might have changed
var windowResizeStream = Rx.Observable.fromEvent(window, 'resize').debounce(50);
this.windowResizeSubscription = windowResizeStream.subscribe(() => {
this.forceUpdate();
});
},
componentWillUnmount: function () {
// cleaning up time...
this.windowResizeSubscription.dispose();
this.visibleIndicesSubscription.dispose();
React.unmountComponentAtNode(this.containerNode);
React.unmountComponentAtNode(this.headerContainerNode);
},
setUpTable: function () {
var containerHeight = this.props.containerHeight;
var rowHeight = this.props.rowHeight;
var rowCount = this.props.rowCount;
var visibleRows = Math.ceil(containerHeight/ rowHeight);
var getScrollTop = () => {
return this.containerNode.scrollTop;
};
var initialScrollSubject = new Rx.ReplaySubject(1);
initialScrollSubject.onNext(getScrollTop());
var scrollTopStream = initialScrollSubject.merge(
Rx.Observable.fromEvent(this.containerNode, 'scroll').map(getScrollTop)
);
var firstVisibleRowStream = scrollTopStream.map(function (scrollTop) {
return Math.floor(scrollTop / rowHeight);
}).distinctUntilChanged();
var visibleIndicesStream = firstVisibleRowStream.map(function (firstRow) {
var visibleIndices = [];
var lastRow = firstRow + visibleRows + 1;
if (lastRow > rowCount) {
firstRow -= lastRow - rowCount;
}
for (var i = 0; i <= visibleRows; i++) {
visibleIndices.push(i + firstRow);
}
return visibleIndices;
});
this.visibleIndicesSubscription = visibleIndicesStream.subscribe((indices) => {
this.setState({
visibleIndices: indices
});
});
}
});
module.exports = FixedScrollTable;