UNPKG

2.2 kBJavaScriptView Raw
1import React, { Component, } from 'react';
2import { Dimensions, ScrollView, } from 'react-native';
3const DEVICE_WIDTH = Dimensions.get('window').width;
4const NOOP = () => { };
5export class HScrollViewDefaultProps {
6 constructor() {
7 this.currentPage = 0;
8 this.scrollEnded = NOOP;
9 this.children = null;
10 }
11}
12export class HScrollViewDefaultStates {
13 constructor(currentPage, containerWidth) {
14 this.currentPage = this.currentPage;
15 this.containerWidth = this.containerWidth;
16 this.currentPage = currentPage;
17 this.containerWidth = containerWidth;
18 }
19}
20export default class HScrollView extends Component {
21 constructor(props) {
22 super(props);
23 this.scrollEnded = (event) => {
24 const position = event.nativeEvent.contentOffset.x;
25 const currentPage = position / DEVICE_WIDTH;
26 if (currentPage !== this.state.currentPage) {
27 this.scrollToItem(currentPage);
28 this.setState({ currentPage });
29 if (typeof this.props.scrollEnded === 'function') {
30 this.props.scrollEnded(currentPage);
31 }
32 }
33 };
34 this.scrollToItem = (itemIndex) => {
35 const scrollToX = itemIndex * DEVICE_WIDTH;
36 setTimeout(() => {
37 if (!!this.screen) {
38 this.screen.scrollTo({ y: 0, x: scrollToX, animated: true });
39 }
40 }, 0);
41 };
42 this.state = new HScrollViewDefaultStates(props.currentPage, DEVICE_WIDTH);
43 }
44 componentWillReceiveProps(nextProps) {
45 if (this.props.currentPage !== nextProps.currentPage) {
46 this.scrollToItem(nextProps.currentPage);
47 }
48 }
49 render() {
50 return (React.createElement(ScrollView, { ref: (ref) => { this.screen = ref; }, horizontal: true, scrollEnabled: false, pagingEnabled: true, removeClippedSubviews: true, showsHorizontalScrollIndicator: false, automaticallyAdjustContentInsets: false, onMomentumScrollEnd: this.scrollEnded }, this.props.children));
51 }
52}
53HScrollView.defaultProps = new HScrollViewDefaultProps();
54//# sourceMappingURL=Animate.js.map
\No newline at end of file