Skip to content

Commit ccc480d

Browse files
committed
feat: add onSync callback to resolve goldenyz#87
1 parent b208ee5 commit ccc480d

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

example/example.js

+47-18
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ function logEvent(type) {
1010
console.log(`event '${type}' triggered!`);
1111
}
1212

13+
const debounce = (fn, ms = 0) => {
14+
let timeoutId;
15+
return function wrapper(...args) {
16+
clearTimeout(timeoutId);
17+
timeoutId = setTimeout(() => fn.apply(this, args), ms);
18+
};
19+
};
20+
1321
class Example extends Component {
1422
constructor(props) {
1523
super(props);
1624

1725
this.state = {
1826
className: undefined,
1927
onXReachEnd: null,
28+
items: Array.from(new Array(100).keys()),
2029
};
2130
}
2231

@@ -33,28 +42,48 @@ class Example extends Component {
3342
logEvent('onYReachEnd');
3443
}
3544

45+
handleTrigger = () => {
46+
this.setState({
47+
items: Array.from(new Array(100).keys()),
48+
});
49+
}
50+
51+
handleSync = debounce((ps) => {
52+
ps.update();
53+
console.log('debounce sync ps container in 1000ms');
54+
}, 1000)
55+
3656
render() {
3757
const { className, onXReachEnd } = this.state;
3858

3959
return (
40-
<div className="example">
41-
<ScrollBar
42-
className={className}
43-
onScrollY={() => logEvent('onScrollY')}
44-
onScrollX={() => logEvent('onScrollX')}
45-
onScrollUp={() => logEvent('onScrollUp')}
46-
onScrollDown={() => logEvent('onScrollDown')}
47-
onScrollLeft={() => logEvent('onScrollLeft')}
48-
onScrollRight={() => logEvent('onScrollRight')}
49-
onYReachStart={() => logEvent('onYReachStart')}
50-
onYReachEnd={this.handleYReachEnd}
51-
onXReachStart={() => logEvent('onXReachStart')}
52-
onXReachEnd={onXReachEnd}
53-
component="div"
54-
>
55-
<div className="content" />
56-
</ScrollBar>
57-
</div>
60+
<React.Fragment>
61+
62+
<div className="example">
63+
<ScrollBar
64+
className={className}
65+
onScrollY={() => logEvent('onScrollY')}
66+
onScrollX={() => logEvent('onScrollX')}
67+
onScrollUp={() => logEvent('onScrollUp')}
68+
onScrollDown={() => logEvent('onScrollDown')}
69+
onScrollLeft={() => logEvent('onScrollLeft')}
70+
onScrollRight={() => logEvent('onScrollRight')}
71+
onYReachStart={() => logEvent('onYReachStart')}
72+
onYReachEnd={this.handleYReachEnd}
73+
onXReachStart={() => logEvent('onXReachStart')}
74+
onXReachEnd={onXReachEnd}
75+
component="div"
76+
>
77+
<div className="content" />
78+
</ScrollBar>
79+
</div>
80+
<div className="example">
81+
<button onClick={this.handleTrigger}>Trigger</button>
82+
<ScrollBar onSync={this.handleSync}>
83+
{this.state.items.map(e => (<div key={e}>{e}</div>))}
84+
</ScrollBar>
85+
</div>
86+
</React.Fragment>
5887
);
5988
}
6089
}

src/scrollbar.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export default class ScrollBar extends Component {
3737

3838
componentDidUpdate(prevProps) {
3939
this._updateEventHook(prevProps);
40-
this._ps.update();
40+
41+
if (typeof this.props.onSync === 'function') this.props.onSync(this._ps);
42+
4143
if (prevProps.className !== this.props.className) {
4244
this._updateClassName();
4345
}
@@ -89,10 +91,6 @@ export default class ScrollBar extends Component {
8991
}
9092
}
9193

92-
updateScroll() {
93-
this._ps.update();
94-
}
95-
9694
handleRef(ref) {
9795
this._container = ref;
9896
this.props.containerRef(ref);
@@ -116,6 +114,7 @@ export default class ScrollBar extends Component {
116114
onXReachStart,
117115
onXReachEnd,
118116
component,
117+
onSync,
119118
children,
120119
...remainProps
121120
} = this.props;
@@ -145,6 +144,7 @@ ScrollBar.defaultProps = {
145144
onYReachEnd: undefined,
146145
onXReachStart: undefined,
147146
onXReachEnd: undefined,
147+
onSync: ps => ps.update(),
148148
component: 'div',
149149
};
150150

@@ -165,5 +165,6 @@ ScrollBar.propTypes = {
165165
onYReachEnd: PropTypes.func,
166166
onXReachStart: PropTypes.func,
167167
onXReachEnd: PropTypes.func,
168+
onSync: PropTypes.func,
168169
component: PropTypes.string,
169170
};

0 commit comments

Comments
 (0)