Skip to content

Commit b86af79

Browse files
committed
refactoring scrollTo to accept animation options
1 parent 9a05e71 commit b86af79

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

Libraries/Components/ScrollView/ScrollView.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,44 @@ var ScrollView = React.createClass({
210210
return React.findNodeHandle(this.refs[INNERVIEW]);
211211
},
212212

213-
scrollTo: function(destY?: number, destX?: number) {
213+
scrollTo: function(destY?: number, destX?: number, options?: { animated?: bool, duration?: number }) {
214214
if (Platform.OS === 'android') {
215+
// TODO: update to allow for custom duration which is already enabled on iOS
215216
RCTUIManager.dispatchViewManagerCommand(
216217
React.findNodeHandle(this),
217218
RCTUIManager.RCTScrollView.Commands.scrollTo,
218219
[destX || 0, destY || 0]
219220
);
220221
} else {
222+
var animated = options && options.animated || !options;
223+
224+
if (!animated) {
225+
// scroll with no animation
226+
RCTUIManager.scrollWithoutAnimationTo(
227+
React.findNodeHandle(this),
228+
destX || 0,
229+
destY || 0
230+
);
231+
return;
232+
}
233+
234+
// scroll using RCTAnimation with custom duration if a duration was passed
235+
if (animated && !isNaN(options.duration)) {
236+
invariant(
237+
options.duration >= 0,
238+
'Duration passed to ScrollView \'scrollTo\' ' +
239+
'should be greater than or equal to 0.'
240+
);
241+
RCTUIManager.scrollWithCustomDurationTo(
242+
React.findNodeHandle(this),
243+
destX || 0,
244+
destY || 0,
245+
options.duration
246+
);
247+
return;
248+
}
249+
250+
// Default scroll behavior
221251
RCTUIManager.scrollTo(
222252
React.findNodeHandle(this),
223253
destX || 0,
@@ -227,20 +257,7 @@ var ScrollView = React.createClass({
227257
},
228258

229259
scrollWithoutAnimationTo: function(destY?: number, destX?: number) {
230-
RCTUIManager.scrollWithoutAnimationTo(
231-
React.findNodeHandle(this),
232-
destX || 0,
233-
destY || 0
234-
);
235-
},
236-
237-
scrollWithCustomDurationTo: function(destY?: number, destX?: number, duration?: number) {
238-
RCTUIManager.scrollWithCustomDurationTo(
239-
React.findNodeHandle(this),
240-
destX || 0,
241-
destY || 0,
242-
duration || 0
243-
);
260+
this.scrollTo(destY, destX, { animated: false });
244261
},
245262

246263
render: function() {

0 commit comments

Comments
 (0)