From 18d60047d2d441eb54a873ac7b4d014637e8b5a4 Mon Sep 17 00:00:00 2001 From: Jiaming Date: Fri, 23 Jun 2017 11:59:23 -0700 Subject: [PATCH] [add] ScrollView: support scrollToEnd method Close #541 --- src/components/ScrollView/index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/components/ScrollView/index.js b/src/components/ScrollView/index.js index 398a83a0..1349c768 100644 --- a/src/components/ScrollView/index.js +++ b/src/components/ScrollView/index.js @@ -100,6 +100,25 @@ const ScrollView = createReactClass({ }); }, + /** + * If this is a vertical ScrollView scrolls to the bottom. + * If this is a horizontal ScrollView scrolls to the right. + * + * Use `scrollToEnd({ animated: true })` for smooth animated scrolling, + * `scrollToEnd({ animated: false })` for immediate scrolling. + * If no options are passed, `animated` defaults to true. + */ + scrollToEnd(options?: { animated?: boolean }) { + // Default to true + const animated = (options && options.animated) !== false; + const { horizontal } = this.props; + const scrollResponder = this.getScrollResponder(); + const scrollResponderNode = scrollResponder.scrollResponderGetScrollableNode(); + const x = horizontal ? scrollResponderNode.scrollWidth : 0; + const y = horizontal ? 0 : scrollResponderNode.scrollHeight; + scrollResponder.scrollResponderScrollTo({ x, y, animated }); + }, + /** * Deprecated, do not use. */