Skip to content

Commit 5caba61

Browse files
committed
Removed cell-wrapper <div> in Collection
1 parent abf4974 commit 5caba61

File tree

5 files changed

+77
-65
lines changed

5 files changed

+77
-65
lines changed

docs/Collection.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Unlike `Grid`, which renders checkerboard data, `Collection` can render arbitrar
1313
| className | String | | Optional custom CSS class name to attach to root Collection element. |
1414
| cellCount | Number || Number of cells in collection. |
1515
| cellGroupRenderer | Function || Responsible for rendering a group of cells given their indices.: `({ cellSizeAndPositionGetter:Function, indices: Array<number>, cellRenderer: Function }): Array<PropTypes.node>` |
16-
| cellRenderer | Function || Responsible for rendering a cell given an row and column index: `({ index: number, isScrolling: boolean }): PropTypes.element` |
16+
| cellRenderer | Function || Responsible for rendering a cell given an row and column index: `({ index: number, isScrolling: boolean, key: string, style: object }): PropTypes.element` |
1717
| cellSizeAndPositionGetter | Function || Callback responsible for returning size and offset/position information for a given cell (index): `({ index: number }): { height: number, width: number, x: number, y: number }` |
1818
| height | Number || Height of Collection; this property determines the number of visible (vs virtualized) rows. |
1919
| horizontalOverscanSize | Number | | Enables the `Collection` to horiontally "overscan" its content similar to how `Grid` does. This can reduce flicker around the edges when a user scrolls quickly. This property defaults to `0`; |
@@ -46,14 +46,13 @@ The Collection component supports the following static class names
4646
|:---|:---|
4747
| ReactVirtualized__Collection | Main (outer) element |
4848
| ReactVirtualized__Collection__innerScrollContainer | Inner scrollable area |
49-
| ReactVirtualized__Collection__cell | Individual cell |
5049

5150
### Examples
5251

5352
Below is a very basic `Collection` example. It displays an array of objects with fixed row and column sizes.
5453
[See here](../source/Collection/Collection.example.js) for a more full-featured example.
5554

56-
```javascript
55+
```jsx
5756
import React from 'react';
5857
import ReactDOM from 'react-dom';
5958
import { Collection } from 'react-virtualized';
@@ -65,20 +64,34 @@ const list = [
6564
// And so on...
6665
];
6766

67+
function cellRenderer ({ index, key, style }) {
68+
return (
69+
<div
70+
key={key}
71+
style={style}
72+
>
73+
{list[index].name}
74+
</div>
75+
)
76+
}
77+
78+
function cellSizeAndPositionGetter ({ index }) {
79+
const datum = list[index]
80+
81+
return {
82+
height: datum.height,
83+
width: datum.width,
84+
x: datum.x,
85+
y: datum.y
86+
}
87+
}
88+
6889
// Render your grid
6990
ReactDOM.render(
7091
<Collection
7192
cellCount={list.length}
72-
cellRenderer={({ index }) => list[index].name}
73-
cellSizeAndPositionGetter={({ index }) => {
74-
const datum = list[index]
75-
return {
76-
height: datum.height,
77-
width: datum.width,
78-
x: datum.x,
79-
y: datum.y
80-
}
81-
}}
93+
cellRenderer={cellRenderer}
94+
cellSizeAndPositionGetter={cellSizeAndPositionGetter}
8295
height={300}
8396
width={300}
8497
/>,

source/Collection/Collection.example.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,20 @@ export default class CollectionExample extends Component {
130130
return shallowCompare(this, nextProps, nextState)
131131
}
132132

133-
_cellRenderer ({ index, isScrolling }) {
133+
_cellRenderer ({ index, isScrolling, key, style }) {
134134
const { list } = this.props
135135
const { showScrollingPlaceholder } = this.state
136136

137137
const datum = list.get(index % list.size)
138138

139+
// Customize style
140+
style.backgroundColor = datum.color
141+
139142
return (
140143
<div
141144
className={styles.cell}
142-
style={{
143-
backgroundColor: datum.color
144-
}}
145+
key={key}
146+
style={style}
145147
>
146148
{showScrollingPlaceholder && isScrolling ? '...' : index}
147149
</div>

source/Collection/Collection.js

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class Collection extends Component {
3131

3232
/**
3333
* Responsible for rendering a cell given an row and column index.
34-
* Should implement the following interface: ({ index: number }): PropTypes.element
34+
* Should implement the following interface: ({ index: number, key: string, style: object }): PropTypes.element
3535
*/
3636
cellRenderer: PropTypes.func.isRequired,
3737

@@ -210,46 +210,32 @@ function defaultCellGroupRenderer ({
210210
.map((index) => {
211211
const cellMetadata = cellSizeAndPositionGetter({ index })
212212

213+
let cellRendererProps = {
214+
index,
215+
isScrolling,
216+
key: index,
217+
style: {
218+
height: cellMetadata.height,
219+
left: cellMetadata.x,
220+
position: 'absolute',
221+
top: cellMetadata.y,
222+
width: cellMetadata.width
223+
}
224+
}
225+
213226
// Avoid re-creating cells while scrolling.
214227
// This can lead to the same cell being created many times and can cause performance issues for "heavy" cells.
215228
// If a scroll is in progress- cache and reuse cells.
216229
// This cache will be thrown away once scrolling complets.
217-
let renderedCell
218-
219230
if (isScrolling) {
220231
if (!(index in cellCache)) {
221-
cellCache[index] = cellRenderer({
222-
index,
223-
isScrolling
224-
})
232+
cellCache[index] = cellRenderer(cellRendererProps)
225233
}
226234

227-
renderedCell = cellCache[index]
235+
return cellCache[index]
228236
} else {
229-
renderedCell = cellRenderer({
230-
index,
231-
isScrolling
232-
})
237+
return cellRenderer(cellRendererProps)
233238
}
234-
235-
if (renderedCell == null || renderedCell === false) {
236-
return null
237-
}
238-
239-
return (
240-
<div
241-
className='ReactVirtualized__Collection__cell'
242-
key={index}
243-
style={{
244-
height: cellMetadata.height,
245-
left: cellMetadata.x,
246-
top: cellMetadata.y,
247-
width: cellMetadata.width
248-
}}
249-
>
250-
{renderedCell}
251-
</div>
252-
)
253239
})
254240
.filter((renderedCell) => !!renderedCell)
255241
}

source/Collection/Collection.test.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import Collection from './Collection'
1111
import { CELLS, SECTION_SIZE } from './TestData'
1212

1313
describe('Collection', () => {
14-
function defaultCellRenderer ({ index }) {
14+
function defaultCellRenderer ({ index, key, style }) {
1515
return (
16-
<div className='cell'>
16+
<div
17+
className='cell'
18+
key={key}
19+
style={style}
20+
>
1721
cell:{index}
1822
</div>
1923
)
@@ -70,12 +74,16 @@ describe('Collection', () => {
7074

7175
// Small performance tweak added in 5.5.6
7276
it('should not render/parent cells that are null or false', () => {
73-
function cellRenderer ({ index }) {
77+
function cellRenderer ({ index, key, style }) {
7478
if (index > 2) {
7579
return null
7680
} else {
7781
return (
78-
<div className='cell'>
82+
<div
83+
className='cell'
84+
key={key}
85+
style={style}
86+
>
7987
{index}
8088
</div>
8189
)
@@ -511,7 +519,14 @@ describe('Collection', () => {
511519
it('should use a custom :cellGroupRenderer if specified', () => {
512520
let cellGroupRendererCalled = 0
513521
let cellGroupRendererParams
514-
const cellRenderer = ({ index }) => index
522+
const cellRenderer = ({ index, key, style }) => (
523+
<div
524+
key={key}
525+
style={style}
526+
>
527+
{index}
528+
</div>
529+
)
515530
findDOMNode(render(getMarkup({
516531
cellRenderer,
517532
cellGroupRenderer: (params) => {
@@ -532,9 +547,9 @@ describe('Collection', () => {
532547

533548
it('should pass the cellRenderer an :isScrolling flag when scrolling is in progress', () => {
534549
const cellRendererCalls = []
535-
function cellRenderer ({ index, isScrolling }) {
550+
function cellRenderer ({ index, isScrolling, key, style }) {
536551
cellRendererCalls.push(isScrolling)
537-
return defaultCellRenderer({ index })
552+
return defaultCellRenderer({ index, key, style })
538553
}
539554
const collection = render(getMarkup({
540555
cellRenderer
@@ -601,9 +616,9 @@ describe('Collection', () => {
601616
describe('cell caching', () => {
602617
it('should not cache cells if the Grid is not scrolling', () => {
603618
const cellRendererCalls = []
604-
function cellRenderer ({ isScrolling, index }) {
619+
function cellRenderer ({ isScrolling, index, key, style }) {
605620
cellRendererCalls.push({ isScrolling, index })
606-
return defaultCellRenderer({ index })
621+
return defaultCellRenderer({ index, key, style })
607622
}
608623

609624
const props = {
@@ -628,9 +643,9 @@ describe('Collection', () => {
628643

629644
it('should cache a cell once it has been rendered while scrolling', () => {
630645
const cellRendererCalls = []
631-
function cellRenderer ({ isScrolling, index }) {
646+
function cellRenderer ({ isScrolling, index, key, style }) {
632647
cellRendererCalls.push({ isScrolling, index })
633-
return defaultCellRenderer({ index })
648+
return defaultCellRenderer({ index, key, style })
634649
}
635650

636651
const props = {
@@ -670,9 +685,9 @@ describe('Collection', () => {
670685

671686
it('should clear cache once :isScrolling is false', async (done) => {
672687
const cellRendererCalls = []
673-
function cellRenderer ({ isScrolling, index }) {
688+
function cellRenderer ({ isScrolling, index, key, style }) {
674689
cellRendererCalls.push({ isScrolling, index })
675-
return defaultCellRenderer({ isScrolling, index })
690+
return defaultCellRenderer({ isScrolling, index, key, style })
676691
}
677692

678693
const props = {

source/styles.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
overflow: hidden;
1717
}
1818

19-
.ReactVirtualized__Collection__cell {
20-
position: absolute;
21-
}
22-
2319
/* Grid default theme */
2420

2521
.ReactVirtualized__Grid {

0 commit comments

Comments
 (0)