Skip to content

Commit

Permalink
Merge pull request alexbrillant#213 from garfieldnate/patch-1
Browse files Browse the repository at this point in the history
account for cards added in onSwipe functions
  • Loading branch information
alexbrillant authored Jan 13, 2019
2 parents e1b58b3 + 5376b6d commit 1a1c18f
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Swiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,20 @@ class Swiper extends Component {
let newCardIndex = firstCardIndex + 1
let swipedAllCards = false

if (newCardIndex === this.state.cards.length) {
newCardIndex = 0
this.onSwipedCallbacks(onSwiped)

allSwipedCheck = () => newCardIndex === this.state.cards.length;

if (allSwipedCheck()) {
if (!infinite) {
swipedAllCards = true
this.props.onSwipedAll()
// onSwipeAll may have added cards
if (allSwipedCheck()) {
swipedAllCards = true
}
}
}

this.onSwipedCallbacks(onSwiped, swipedAllCards)
this.setCardIndex(newCardIndex, swipedAllCards)
}

Expand All @@ -528,9 +534,8 @@ class Swiper extends Component {
const newCardIndex =
firstCardIndex === 0 ? lastCardIndex : previousCardIndex

const swipedAllCards = false
this.onSwipedCallbacks(cb, swipedAllCards)
this.setCardIndex(newCardIndex, swipedAllCards)
this.onSwipedCallbacks(cb)
this.setCardIndex(newCardIndex, false)
}

jumpToCardIndex = newCardIndex => {
Expand All @@ -539,16 +544,13 @@ class Swiper extends Component {
}
}

onSwipedCallbacks = (swipeDirectionCallback, swipedAllCards) => {
onSwipedCallbacks = (swipeDirectionCallback) => {
const previousCardIndex = this.state.firstCardIndex
this.props.onSwiped(previousCardIndex, this.state.cards[previousCardIndex])

if (swipeDirectionCallback) {
swipeDirectionCallback(previousCardIndex, this.state.cards[previousCardIndex])
}
if (swipedAllCards) {
this.props.onSwipedAll()
}
}

setCardIndex = (newCardIndex, swipedAllCards) => {
Expand Down

0 comments on commit 1a1c18f

Please sign in to comment.