-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
79 lines (65 loc) · 2.47 KB
/
index.jsx
File metadata and controls
79 lines (65 loc) · 2.47 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
import React from 'react'
import { verticalFilled } from 'patchkit-vertical-filled'
// SimpleInfinite mimics the interface of react-infinite
// the main difference is, it doesnt try to do fancy performance improvements, like hiding offscreen elements
// this lets it tolerate elements with heights that aren't precomputed (which react-infinite cant do)
class SimpleInfinite extends React.Component {
constructor(props) {
super(props)
this.scrollingTo = false
this.onScroll = () => {
// stop checking if out of more content
if (!this.hasLoadableContent())
return
// dont check if already loading
if (this.props.isInfiniteLoading)
return
let el = this.refs.container
if (el.offsetHeight + el.scrollTop + this.props.infiniteLoadBeginBottomOffset >= el.scrollHeight) {
// hit bottom
this.props.onInfiniteLoad(this.scrollingTo)
}
}
}
hasLoadableContent() {
// this is how react-infinite signals there's no more content -- when this var is falsey
return !!this.props.infiniteLoadBeginBottomOffset
}
getScrollTop() {
const el = this.refs && this.refs.container
if (!el) return 0
console.log('a')
return el.scrollTop
}
// check if a location is in scroll-view
isPointVisible(left, top) {
const el = this.refs && this.refs.container
if (!el) return
// TODO left
if (el.scrollTop > top || el.scrollTop + this.props.containerHeight < top)
return false
return true
}
// scrolls & loads until a destination is reached
scrollTo(top, lastTop) {
const el = this.refs && this.refs.container
if (!el) return
// stop when:
// - we reached the destination, or
// - we're out of loadable content, and attempting to scroll further has not moved us
if (el.scrollTop == top || (!this.hasLoadableContent() && el.scrollTop === lastTop)) {
this.scrollingTo = false
return // we're done
}
el.scrollTop = top
if (el.scrollTop !== top) { // didnt get all the way there?
this.scrollingTo = top
setTimeout(() => this.scrollTo(top, el.scrollTop), 1) // try again in 1ms, after more loading has had a chance to occur
} else
this.scrollingTo = false // done
}
render() {
return <div id={this.props.id} className="vertical-filled" ref="container" onScroll={this.onScroll} style={{height: this.props.height, overflow: 'auto'}}>{this.props.children}</div>
}
}
export default verticalFilled(SimpleInfinite)