-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAutoScroll.js
More file actions
54 lines (43 loc) · 1.62 KB
/
AutoScroll.js
File metadata and controls
54 lines (43 loc) · 1.62 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
var React = require('react')
var ReactDOM = require('react-dom')
module.exports = function AutoScroll (options) {
var property = options.property
var alwaysScroll = options.alwaysScroll || false
return function (Component) {
var displayName = Component.displayName || Component.name || 'Component'
var propTypes = {}
propTypes[property] = React.PropTypes.any
propTypes[alwaysScroll] = React.PropTypes.bool
var AutoScrollComponent = React.createClass({
componentDidMount: function componentDidMount () {
var node = this._node
node.scrollTop = node.scrollHeight
this._shouldScroll = alwaysScroll || false
},
componentDidUpdate: function componentDidUpdate (prevProps) {
if (this._shouldScroll) {
var node = this._node
node.scrollTop = node.scrollHeight
this._shouldScroll = false
}
},
componentWillUpdate: function componentWillUpdate (nextProps) {
if (this.props[property] !== nextProps[property]) {
var node = this._node
this._shouldScroll = alwaysScroll || node.scrollTop + node.offsetHeight === node.scrollHeight
}
},
displayName: 'AutoScroll(' + displayName + ')',
propTypes: propTypes,
render: function render () {
var props = Object.assign({ ref: this._setComponent }, this.props)
return React.createElement(Component, props)
},
_setComponent: function _setComponent (component) {
this._component = component
this._node = ReactDOM.findDOMNode(component)
}
})
return AutoScrollComponent
}
}