-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
117 lines (101 loc) · 2.54 KB
/
index.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React from 'react';
import PropTypes from 'prop-types';
import SmoothScrollbar from 'smooth-scrollbar';
import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll';
SmoothScrollbar.use(OverscrollPlugin);
class Scrollbar extends React.Component {
static propTypes = {
damping: PropTypes.number,
thumbMinSize: PropTypes.number,
syncCallbacks: PropTypes.bool,
renderByPixels: PropTypes.bool,
alwaysShowTracks: PropTypes.bool,
continuousScrolling: PropTypes.bool,
plugins: PropTypes.object,
onScroll: PropTypes.func,
children: PropTypes.node,
innerRef: PropTypes.func,
};
static defaultProps = {
innerRef: () => {},
};
componentDidMount() {
this.scrollbar = SmoothScrollbar.init(this.container, this.props);
this.scrollbar.addListener(this.handleScroll.bind(this));
this.props.innerRef(this.scrollbar);
}
componentWillReceiveProps(nextProps) {
Object.keys(nextProps).forEach(key => {
if (!key in this.scrollbar.options) {
return;
}
if (key === 'plugins') {
Object.keys(nextProps.plugins).forEach(pluginName => {
this.scrollbar.updatePluginOptions(
pluginName,
nextProps.plugins[pluginName]
);
});
} else {
this.scrollbar.options[key] = nextProps[key];
}
});
}
componentDidUpdate(prevProps) {
this.scrollbar && this.scrollbar.update();
}
componentWillUnmount() {
if (this.scrollbar) {
this.scrollbar.destroy();
}
this.scrollbar = null;
}
handleScroll(status) {
if (this.props.onScroll) {
this.props.onScroll(status, this.scrollbar);
}
}
render() {
const {
damping,
thumbMinSize,
syncCallbacks,
renderByPixels,
alwaysShowTracks,
continuousScrolling,
plugins,
onScroll,
children,
innerRef,
...others
} = this.props;
const count = React.Children.count(children);
if (count === 1 && typeof children.type === 'string') {
return React.cloneElement(children, {
...others,
ref: node => (this.container = node),
});
}
return React.createElement(
'div',
{
...others,
ref: node => (this.container = node),
style: {
WebkitBoxFlex: 1,
msFlex: 1,
MozFlex: 1,
flex: 1,
},
},
React.createElement(
'div',
{
className: 'scroll-content-inner',
},
children
)
);
}
}
export default Scrollbar;