forked from bvaughn/react-virtualized
/
ScrollSync.example.js
275 lines (253 loc) · 9 KB
/
ScrollSync.example.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
271
272
273
274
275
/** @flow */
import * as React from 'react';
import {
ContentBox,
ContentBoxHeader,
ContentBoxParagraph,
} from '../demo/ContentBox';
import AutoSizer from '../AutoSizer';
import Grid from '../Grid';
import ScrollSync from './ScrollSync';
import clsx from 'clsx';
import styles from './ScrollSync.example.css';
import scrollbarSize from 'dom-helpers/scrollbarSize';
const LEFT_COLOR_FROM = hexToRgb('#471061');
const LEFT_COLOR_TO = hexToRgb('#BC3959');
const TOP_COLOR_FROM = hexToRgb('#000000');
const TOP_COLOR_TO = hexToRgb('#333333');
export default class GridExample extends React.PureComponent {
constructor(props, context) {
super(props, context);
this.state = {
columnWidth: 75,
columnCount: 50,
height: 300,
overscanColumnCount: 0,
overscanRowCount: 5,
rowHeight: 40,
rowCount: 100,
};
this._renderBodyCell = this._renderBodyCell.bind(this);
this._renderHeaderCell = this._renderHeaderCell.bind(this);
this._renderLeftSideCell = this._renderLeftSideCell.bind(this);
}
render() {
const {
columnCount,
columnWidth,
height,
overscanColumnCount,
overscanRowCount,
rowHeight,
rowCount,
} = this.state;
return (
<ContentBox>
<ContentBoxHeader
text="ScrollSync"
sourceLink="https://github.com/bvaughn/react-virtualized/blob/master/source/ScrollSync/ScrollSync.example.js"
docsLink="https://github.com/bvaughn/react-virtualized/blob/master/docs/ScrollSync.md"
/>
<ContentBoxParagraph>
High order component that simplifies the process of synchronizing
scrolling between two or more virtualized components.
</ContentBoxParagraph>
<ContentBoxParagraph>
This example shows two <code>Grid</code>
s and one <code>List</code> configured to mimic a spreadsheet with a
fixed header and first column. It also shows how a scroll callback can
be used to control UI properties such as background color.
</ContentBoxParagraph>
<ScrollSync>
{({
clientHeight,
clientWidth,
onScroll,
scrollHeight,
scrollLeft,
scrollTop,
scrollWidth,
}) => {
const x = scrollLeft / (scrollWidth - clientWidth);
const y = scrollTop / (scrollHeight - clientHeight);
const leftBackgroundColor = mixColors(
LEFT_COLOR_FROM,
LEFT_COLOR_TO,
y,
);
const leftColor = '#ffffff';
const topBackgroundColor = mixColors(
TOP_COLOR_FROM,
TOP_COLOR_TO,
x,
);
const topColor = '#ffffff';
const middleBackgroundColor = mixColors(
leftBackgroundColor,
topBackgroundColor,
0.5,
);
const middleColor = '#ffffff';
return (
<div className={styles.GridRow}>
<div
className={styles.LeftSideGridContainer}
style={{
position: 'absolute',
left: 0,
top: 0,
color: leftColor,
backgroundColor: `rgb(${topBackgroundColor.r},${
topBackgroundColor.g
},${topBackgroundColor.b})`,
}}>
<Grid
cellRenderer={this._renderLeftHeaderCell}
className={styles.HeaderGrid}
width={columnWidth}
height={rowHeight}
rowHeight={rowHeight}
columnWidth={columnWidth}
rowCount={1}
columnCount={1}
/>
</div>
<div
className={styles.LeftSideGridContainer}
style={{
position: 'absolute',
left: 0,
top: rowHeight,
color: leftColor,
backgroundColor: `rgb(${leftBackgroundColor.r},${
leftBackgroundColor.g
},${leftBackgroundColor.b})`,
}}>
<Grid
overscanColumnCount={overscanColumnCount}
overscanRowCount={overscanRowCount}
cellRenderer={this._renderLeftSideCell}
columnWidth={columnWidth}
columnCount={1}
className={styles.LeftSideGrid}
height={height - scrollbarSize()}
rowHeight={rowHeight}
rowCount={rowCount}
scrollTop={scrollTop}
width={columnWidth}
/>
</div>
<div className={styles.GridColumn}>
<AutoSizer disableHeight>
{({width}) => (
<div>
<div
style={{
backgroundColor: `rgb(${topBackgroundColor.r},${
topBackgroundColor.g
},${topBackgroundColor.b})`,
color: topColor,
height: rowHeight,
width: width - scrollbarSize(),
}}>
<Grid
className={styles.HeaderGrid}
columnWidth={columnWidth}
columnCount={columnCount}
height={rowHeight}
overscanColumnCount={overscanColumnCount}
cellRenderer={this._renderHeaderCell}
rowHeight={rowHeight}
rowCount={1}
scrollLeft={scrollLeft}
width={width - scrollbarSize()}
/>
</div>
<div
style={{
backgroundColor: `rgb(${middleBackgroundColor.r},${
middleBackgroundColor.g
},${middleBackgroundColor.b})`,
color: middleColor,
height,
width,
}}>
<Grid
className={styles.BodyGrid}
columnWidth={columnWidth}
columnCount={columnCount}
height={height}
onScroll={onScroll}
overscanColumnCount={overscanColumnCount}
overscanRowCount={overscanRowCount}
cellRenderer={this._renderBodyCell}
rowHeight={rowHeight}
rowCount={rowCount}
width={width}
/>
</div>
</div>
)}
</AutoSizer>
</div>
</div>
);
}}
</ScrollSync>
</ContentBox>
);
}
_renderBodyCell({columnIndex, key, rowIndex, style}) {
if (columnIndex < 1) {
return;
}
return this._renderLeftSideCell({columnIndex, key, rowIndex, style});
}
_renderHeaderCell({columnIndex, key, rowIndex, style}) {
if (columnIndex < 1) {
return;
}
return this._renderLeftHeaderCell({columnIndex, key, rowIndex, style});
}
_renderLeftHeaderCell({columnIndex, key, style}) {
return (
<div className={styles.headerCell} key={key} style={style}>
{`C${columnIndex}`}
</div>
);
}
_renderLeftSideCell({columnIndex, key, rowIndex, style}) {
const rowClass =
rowIndex % 2 === 0
? columnIndex % 2 === 0 ? styles.evenRow : styles.oddRow
: columnIndex % 2 !== 0 ? styles.evenRow : styles.oddRow;
const classNames = clsx(rowClass, styles.cell);
return (
<div className={classNames} key={key} style={style}>
{`R${rowIndex}, C${columnIndex}`}
</div>
);
}
}
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}
/**
* Ported from sass implementation in C
* https://github.com/sass/libsass/blob/0e6b4a2850092356aa3ece07c6b249f0221caced/functions.cpp#L209
*/
function mixColors(color1, color2, amount) {
const weight1 = amount;
const weight2 = 1 - amount;
const r = Math.round(weight1 * color1.r + weight2 * color2.r);
const g = Math.round(weight1 * color1.g + weight2 * color2.g);
const b = Math.round(weight1 * color1.b + weight2 * color2.b);
return {r, g, b};
}