forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindowScroller.example.js
177 lines (157 loc) · 4.85 KB
/
WindowScroller.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
// @flow
import clsx from 'clsx';
import Immutable from 'immutable';
import PropTypes from 'prop-types';
import * as React from 'react';
import {
ContentBox,
ContentBoxHeader,
ContentBoxParagraph,
} from '../demo/ContentBox';
import {LabeledInput, InputRow} from '../demo/LabeledInput';
import WindowScroller from './WindowScroller';
import List from '../List';
import AutoSizer from '../AutoSizer';
import styles from './WindowScroller.example.css';
type State = {
scrollToIndex: number,
showHeaderText: boolean,
};
export default class WindowScrollerExample extends React.PureComponent<
{},
State,
> {
static contextTypes = {
customElement: PropTypes.any,
isScrollingCustomElement: PropTypes.bool.isRequired,
list: PropTypes.instanceOf(Immutable.List).isRequired,
setScrollingCustomElement: PropTypes.func,
};
state = {
scrollToIndex: -1,
showHeaderText: true,
};
_windowScroller: ?WindowScroller;
render() {
const {customElement, isScrollingCustomElement, list} = this.context;
const {scrollToIndex, showHeaderText} = this.state;
return (
<ContentBox>
<ContentBoxHeader
text="WindowScroller"
sourceLink="https://github.com/bvaughn/react-virtualized/blob/master/source/WindowScroller/WindowScroller.example.js"
docsLink="https://github.com/bvaughn/react-virtualized/blob/master/docs/WindowScroller.md"
/>
{showHeaderText && (
<ContentBoxParagraph>
This component decorates <code>List</code>, <code>Table</code>, or
any other component and manages the window scroll to scroll through
the list
</ContentBoxParagraph>
)}
{showHeaderText && (
<ContentBoxParagraph>
<button onClick={this._hideHeader}>Hide header text</button>
</ContentBoxParagraph>
)}
<ContentBoxParagraph>
<label className={styles.checkboxLabel}>
<input
aria-label="Use custom element for scrolling"
className={styles.checkbox}
type="checkbox"
checked={isScrollingCustomElement}
onChange={this._onCheckboxChange}
/>
Use custom element for scrolling
</label>
</ContentBoxParagraph>
<InputRow>
<LabeledInput
label="Scroll to"
name="onScrollToRow"
placeholder="Index..."
onChange={this._onScrollToRowChange}
value={scrollToIndex || ''}
/>
</InputRow>
<WindowScroller
ref={this._setRef}
scrollElement={isScrollingCustomElement ? customElement : window}>
{({height, isScrolling, registerChild, onChildScroll, scrollTop}) => (
<div className={styles.WindowScrollerWrapper}>
<AutoSizer disableHeight>
{({width}) => (
<div ref={registerChild}>
<List
ref={el => {
window.listEl = el;
}}
autoHeight
className={styles.List}
height={height}
isScrolling={isScrolling}
onScroll={onChildScroll}
overscanRowCount={2}
rowCount={list.size}
rowHeight={30}
rowRenderer={this._rowRenderer}
scrollToIndex={scrollToIndex}
scrollTop={scrollTop}
width={width}
/>
</div>
)}
</AutoSizer>
</div>
)}
</WindowScroller>
</ContentBox>
);
}
_hideHeader = () => {
const {showHeaderText} = this.state;
this.setState(
{
showHeaderText: !showHeaderText,
},
() => {
if (this._windowScroller) {
this._windowScroller.updatePosition();
}
},
);
};
_rowRenderer = ({index, isScrolling, isVisible, key, style}) => {
const {list} = this.context;
const row = list.get(index);
const className = clsx(styles.row, {
[styles.rowScrolling]: isScrolling,
isVisible: isVisible,
});
return (
<div key={key} className={className} style={style}>
{row.name}
</div>
);
};
_setRef = windowScroller => {
this._windowScroller = windowScroller;
};
_onCheckboxChange = event => {
this.context.setScrollingCustomElement(event.target.checked);
};
_onScrollToRowChange = event => {
const {list} = this.context;
let scrollToIndex = Math.min(
list.size - 1,
parseInt(event.target.value, 10),
);
if (isNaN(scrollToIndex)) {
scrollToIndex = undefined;
}
setTimeout(() => {
this.setState({scrollToIndex});
}, 0);
};
}