From b6fe4c08585dcd13beba17d124ef7669c88fcb33 Mon Sep 17 00:00:00 2001 From: ksairi Date: Tue, 10 Jul 2018 13:45:34 +0200 Subject: [PATCH 1/2] fixes bug #36. pageWidth is calculated using view's onLayout method --- index.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index fc9d939..768087e 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,8 @@ import { import styles from "./styles"; -const { width } = Dimensions.get("window"); +var { width } = Dimensions.get("window"); +var defaultPageWidth = width - 80; export default class Carousel extends Component { @@ -38,7 +39,7 @@ export default class Carousel extends Component { initialPage: 0, pageStyle: null, containerStyle: null, - pageWidth: width - 80, + pageWidth: defaultPageWidth, sneak: 20, noItemsText: "Sorry, there are currently \n no items available", transitionDelay: 0, @@ -58,6 +59,8 @@ export default class Carousel extends Component { this._resetScrollPosition = this._resetScrollPosition.bind(this); this._handleScrollEnd = this._handleScrollEnd.bind(this); + this._onLayout = this._onLayout.bind(this); + this._getPageWidth = this._getPageWidth.bind(this); } componentWillMount() { @@ -94,9 +97,7 @@ export default class Carousel extends Component { } _getPageOffset() { - const { - pageWidth, - } = this.props; + const pageWidth = this._getPageWidth(); const { gap, @@ -136,7 +137,8 @@ export default class Carousel extends Component { } _calculateGap(props) { - const { sneak, pageWidth } = props; + const { sneak } = props; + const pageWidth = this._getPageWidth(); if (pageWidth > width) { throw new Error("invalid pageWidth"); } @@ -183,9 +185,21 @@ export default class Carousel extends Component { this.props.onPageChange(position, currentElement); } } + + _onLayout(event) { + width = event.nativeEvent.layout.width; + this._calculateGap(this.props); + } + + _getPageWidth() { + return this.props.pageWidth == defaultPageWidth + ? defaultPageWidth + : this.props.pageWidth; + } render() { - const { sneak, pageWidth } = this.props; + const pageWidth = this._getPageWidth(); + const { sneak } = this.props; const { gap } = this.state; const computedStyles = StyleSheet.create({ scrollView: { @@ -232,7 +246,7 @@ export default class Carousel extends Component { } return ( - + Date: Thu, 21 May 2020 14:04:34 -0300 Subject: [PATCH 2/2] added prop horizontal --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 768087e..b9e8d34 100644 --- a/index.js +++ b/index.js @@ -44,7 +44,8 @@ export default class Carousel extends Component { noItemsText: "Sorry, there are currently \n no items available", transitionDelay: 0, currentPage: 0, - swipeThreshold: 0.5 + swipeThreshold: 0.5, + horizontal: true }; constructor(props) { @@ -139,9 +140,9 @@ export default class Carousel extends Component { _calculateGap(props) { const { sneak } = props; const pageWidth = this._getPageWidth(); - if (pageWidth > width) { - throw new Error("invalid pageWidth"); - } + // if (pageWidth > width) { + // throw new Error("invalid pageWidth"); + // } /* ------------ | | @@ -253,7 +254,7 @@ export default class Carousel extends Component { contentContainerStyle={ [ computedStyles.scrollView ] } style={{ flexDirection: (I18nManager && I18nManager.isRTL) ? 'row-reverse' : 'row' }} decelerationRate={ 0.9 } - horizontal + horizontal = {this.props.horizontal} onScrollEndDrag={ this._handleScrollEnd } ref={ c => this.scrollView = c } showsHorizontalScrollIndicator={ false }