forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowScroller.example.js
165 lines (147 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
/** @flow */
import cn from 'classnames'
import Immutable from 'immutable'
import PropTypes from 'prop-types'
import React, { PureComponent } 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'
export default class WindowScrollerExample extends PureComponent {
static contextTypes = {
list: PropTypes.instanceOf(Immutable.List).isRequired,
customElement: PropTypes.any,
isScrollingCustomElement: PropTypes.bool.isRequired,
setScrollingCustomElement: PropTypes.func
}
constructor (props) {
super(props)
this.state = {
showHeaderText: true,
scrollToIndex: undefined
}
this._hideHeader = this._hideHeader.bind(this)
this._rowRenderer = this._rowRenderer.bind(this)
this._onCheckboxChange = this._onCheckboxChange.bind(this)
this._onScrollToRowChange = this._onScrollToRowChange.bind(this)
this._setRef = this._setRef.bind(this)
}
render () {
const { list, isScrollingCustomElement, customElement } = this.context
const { showHeaderText, scrollToIndex } = 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>
<div className={styles.WindowScrollerWrapper}>
<WindowScroller
ref={this._setRef}
scrollElement={isScrollingCustomElement ? customElement : null}
>
{({ height, isScrolling, scrollTop, onChildScroll }) => (
<AutoSizer disableHeight>
{({ width }) => (
<List
ref={(el) => { window.listEl = el }}
autoHeight
className={styles.List}
height={height}
isScrolling={isScrolling}
overscanRowCount={2}
rowCount={list.size}
rowHeight={30}
rowRenderer={this._rowRenderer}
scrollTop={scrollTop}
scrollToIndex={scrollToIndex}
width={width}
onScroll={onChildScroll}
/>
)}
</AutoSizer>
)}
</WindowScroller>
</div>
</ContentBox>
)
}
_hideHeader () {
const { showHeaderText } = this.state
this.setState({
showHeaderText: !showHeaderText
}, () => {
this._windowScroller.updatePosition()
})
}
_rowRenderer ({ index, isScrolling, isVisible, key, style }) {
const { list } = this.context
const row = list.get(index)
const className = cn(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)
}
}