forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.example.js
More file actions
196 lines (164 loc) · 5.54 KB
/
Collection.example.js
File metadata and controls
196 lines (164 loc) · 5.54 KB
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
/** @flow */
import React, { Component, PropTypes } from 'react'
import Immutable from 'immutable'
import { ContentBox, ContentBoxHeader, ContentBoxParagraph } from '../demo/ContentBox'
import { LabeledInput, InputRow } from '../demo/LabeledInput'
import AutoSizer from '../AutoSizer'
import Collection from './Collection'
import shallowCompare from 'react-addons-shallow-compare'
import styles from './Collection.example.css'
// Defines a pattern of sizes and positions for a range of 10 rotating cells
// These cells cover an area of 600 (wide) x 400 (tall)
const GUTTER_SIZE = 3
const CELL_WIDTH = 75
export default class CollectionExample extends Component {
static propTypes = {
list: PropTypes.instanceOf(Immutable.List).isRequired
}
constructor (props, context) {
super(props, context)
this.state = {
cellCount: props.list.size,
columnCount: this._getColumnCount(props.list.size),
height: 300,
scrollToCell: undefined,
showScrollingPlaceholder: false
}
this._columnYMap = []
this._cellRenderer = this._cellRenderer.bind(this)
this._cellSizeAndPositionGetter = this._cellSizeAndPositionGetter.bind(this)
this._noContentRenderer = this._noContentRenderer.bind(this)
this._onCellCountChange = this._onCellCountChange.bind(this)
this._onHeightChange = this._onHeightChange.bind(this)
this._onScrollToCellChange = this._onScrollToCellChange.bind(this)
}
render () {
const { cellCount, height, scrollToCell, showScrollingPlaceholder } = this.state
return (
<ContentBox {...this.props}>
<ContentBoxHeader
text='Collection'
sourceLink='https://github.com/bvaughn/react-virtualized/blob/master/source/Collection/Collection.example.js'
docsLink='https://github.com/bvaughn/react-virtualized/blob/master/docs/Collection.md'
/>
<ContentBoxParagraph>
Renders scattered or non-linear data.
Unlike <code>Grid</code>, which renders checkerboard data, <code>Collection</code> can render arbitrarily positioned- even overlapping- data.
</ContentBoxParagraph>
<ContentBoxParagraph>
<label className={styles.checkboxLabel}>
<input
aria-label='Show placeholder while scrolling?'
checked={showScrollingPlaceholder}
className={styles.checkbox}
type='checkbox'
onChange={event => this.setState({ showScrollingPlaceholder: event.target.checked })}
/>
Show placeholder while scrolling?
</label>
</ContentBoxParagraph>
<InputRow>
<LabeledInput
label='Num cells'
name='cellCount'
onChange={this._onCellCountChange}
value={cellCount}
/>
<LabeledInput
label='Scroll to cell'
name='onScrollToCell'
placeholder='Index...'
onChange={this._onScrollToCellChange}
value={scrollToCell || ''}
/>
<LabeledInput
label='Height'
name='height'
onChange={this._onHeightChange}
value={height}
/>
</InputRow>
<AutoSizer disableHeight>
{({ width }) => (
<Collection
cellCount={cellCount}
cellRenderer={this._cellRenderer}
cellSizeAndPositionGetter={this._cellSizeAndPositionGetter}
className={styles.collection}
height={height}
noContentRenderer={this._noContentRenderer}
scrollToCell={scrollToCell}
width={width}
/>
)}
</AutoSizer>
</ContentBox>
)
}
shouldComponentUpdate (nextProps, nextState) {
return shallowCompare(this, nextProps, nextState)
}
_cellRenderer ({ index, isScrolling }) {
const { list } = this.props
const { showScrollingPlaceholder } = this.state
const datum = list.get(index % list.size)
return (
<div
className={styles.cell}
style={{
backgroundColor: datum.color
}}
>
{showScrollingPlaceholder && isScrolling ? '...' : index}
</div>
)
}
_cellSizeAndPositionGetter ({ index }) {
const { list } = this.props
const { columnCount } = this.state
const columnPosition = index % (columnCount || 1)
const datum = list.get(index % list.size)
// Poor man's Masonry layout; columns won't all line up equally with the bottom.
const height = datum.size
const width = CELL_WIDTH
const x = columnPosition * (GUTTER_SIZE + width)
const y = this._columnYMap[columnPosition] || 0
this._columnYMap[columnPosition] = y + height + GUTTER_SIZE
return {
height,
width,
x,
y
}
}
_getColumnCount (cellCount) {
return Math.round(Math.sqrt(cellCount))
}
_noContentRenderer () {
return (
<div className={styles.noCells}>
No cells
</div>
)
}
_onCellCountChange (event) {
const cellCount = parseInt(event.target.value, 10) || 0
this._columnYMap = []
this.setState({
cellCount,
columnCount: this._getColumnCount(cellCount)
})
}
_onHeightChange (event) {
const height = parseInt(event.target.value, 10) || 0
this.setState({ height })
}
_onScrollToCellChange (event) {
const { cellCount } = this.state
let scrollToCell = Math.min(cellCount - 1, parseInt(event.target.value, 10))
if (isNaN(scrollToCell)) {
scrollToCell = undefined
}
this.setState({ scrollToCell })
}
}