forked from jwarning/react-scrollable-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (72 loc) · 2.3 KB
/
index.js
File metadata and controls
78 lines (72 loc) · 2.3 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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
export default class ScrollableList extends Component {
static propTypes = {
listItems: PropTypes.array.isRequired,
heightOfItem: PropTypes.number.isRequired,
renderListItem: PropTypes.func.isRequired,
maxItemsToRender: PropTypes.number,
className: PropTypes.string,
style: PropTypes.object
}
static defaultProps = {
listItems: [],
heightOfItem: 30,
maxItemsToRender: 50,
className: '',
renderListItem: () => <div/>
}
constructor(props) {
super(props)
this.state = { scrollPosition: 0 }
this.list = null
this.setListRef = element => {
this.list = element
}
this.updateScrollPosition = this.updateScrollPosition.bind(this)
}
componentDidMount() {
this.list.addEventListener('scroll', this.updateScrollPosition)
}
componentWillUnmount() {
this.list.removeEventListener('scroll', this.updateScrollPosition)
}
updateScrollPosition() {
const newScrollPosition = this.list.scrollTop / this.props.heightOfItem
const difference = Math.abs(this.state.scrollPosition - newScrollPosition)
if (difference >= this.props.maxItemsToRender / 5) {
this.setState({ scrollPosition: newScrollPosition })
}
}
render() {
const startPosition = this.state.scrollPosition -
this.props.maxItemsToRender >
0
? this.state.scrollPosition - this.props.maxItemsToRender
: 0
const endPosition = this.state.scrollPosition +
this.props.maxItemsToRender >=
this.props.listItems.length
? this.props.listItems.length
: this.state.scrollPosition + this.props.maxItemsToRender
return (
<div className={this.props.className ? this.props.className : "react-scrollable-list"}
ref={this.setListRef} style={this.props.style}>
<div
key="list-spacer-top"
style={{
height: startPosition * this.props.heightOfItem
}}
/>
{this.props.listItems.slice(startPosition, endPosition).map(this.props.renderListItem)}
<div
key="list-spacer-bottom"
style={{
height: this.props.listItems.length * this.props.heightOfItem -
endPosition * this.props.heightOfItem
}}
/>
</div>
)
}
}