Skip to content

Commit 2bc0e64

Browse files
committed
2.0.0
1 parent 2d8f9d3 commit 2bc0e64

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-tiny-virtual-list",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "A tiny but mighty list virtualization component, with zero dependencies 💪",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ const STYLE_INNER = {position: 'relative', overflow: 'hidden', width: '100%', mi
66
const STYLE_ITEM = {position: 'absolute', left: 0, width: '100%'};
77
const DIRECTION_VERTICAL = 'vertical';
88
const DIRECTION_HORIZONTAL = 'horizontal';
9+
910
const scrollProp = {
1011
[DIRECTION_VERTICAL]: 'scrollTop',
1112
[DIRECTION_HORIZONTAL]: 'scrollLeft',
1213
};
14+
const sizeProp = {
15+
[DIRECTION_VERTICAL]: 'height',
16+
[DIRECTION_HORIZONTAL]: 'width',
17+
};
18+
const positionProp = {
19+
[DIRECTION_VERTICAL]: 'top',
20+
[DIRECTION_HORIZONTAL]: 'left',
21+
};
1322

1423
export default class VirtualList extends PureComponent {
1524
static defaultProps = {
@@ -98,7 +107,7 @@ export default class VirtualList extends PureComponent {
98107

99108
if (typeof itemSize === 'function') { return itemSize(index); }
100109

101-
return (Array.isArray(itemSize)) ? itemSize[index] : itemSize;
110+
return Array.isArray(itemSize) ? itemSize[index] : itemSize;
102111
}
103112

104113
setOffset(offset) {
@@ -116,20 +125,20 @@ export default class VirtualList extends PureComponent {
116125
}
117126

118127
getOffsetForIndex(index, scrollToAlignment = this.props.scrollToAlignment) {
119-
const {height} = this.props;
128+
const {scrollDirection} = this.props;
120129

121130
return this.sizeAndPositionManager.getUpdatedOffsetForIndex({
122131
align: scrollToAlignment,
123-
containerSize: height,
132+
containerSize: this.props[sizeProp[scrollDirection]],
124133
currentOffset: this.state && this.state.offset || 0,
125134
targetIndex: index,
126135
});
127136
}
128137

129138
getRowsForOffset(offset) {
130-
const {height, overscanCount, itemCount} = this.props;
139+
const {itemCount, overscanCount, scrollDirection} = this.props;
131140
let {start, stop} = this.sizeAndPositionManager.getVisibleRange({
132-
containerSize: height,
141+
containerSize: this.props[sizeProp[scrollDirection]],
133142
offset,
134143
});
135144

@@ -145,12 +154,13 @@ export default class VirtualList extends PureComponent {
145154
const style = this._styleCache[index];
146155
if (style) { return style; }
147156

157+
const {scrollDirection} = this.props;
148158
const {size, offset} = this.sizeAndPositionManager.getSizeAndPositionForIndex(index);
149159

150160
return this._styleCache[index] = {
151161
...STYLE_ITEM,
152-
height: size,
153-
top: offset,
162+
[sizeProp[scrollDirection]]: size,
163+
[positionProp[scrollDirection]]: offset,
154164
};
155165
}
156166
cache = {};
@@ -183,7 +193,7 @@ export default class VirtualList extends PureComponent {
183193

184194
return (
185195
<div ref={this._getRef} {...props} onScroll={this.handleScroll} style={{...STYLE_WRAPPER, ...style, height, width}}>
186-
<div style={{...STYLE_INNER, height: this.sizeAndPositionManager.getTotalSize()}}>
196+
<div style={{...STYLE_INNER, [sizeProp[scrollDirection]]: this.sizeAndPositionManager.getTotalSize()}}>
187197
{rows}
188198
</div>
189199
</div>

0 commit comments

Comments
 (0)