Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel swipe #60

Open
ruben2807 opened this issue Jun 20, 2019 · 5 comments
Open

Cancel swipe #60

ruben2807 opened this issue Jun 20, 2019 · 5 comments

Comments

@ruben2807
Copy link

How can I do to prevent the user from moving around the different views? and manage the current changes Index for my own.
Basically what I want is to cancel the swipe.

Can someone help me?

@rubycon
Copy link

rubycon commented Jun 20, 2019

Use the onIndexChange callback to check if the index is in your allowed range. If not, set the index prop back to the previous allowed index.

@ruben2807
Copy link
Author

Hi rubycon!
Yes, that could help me. The problem is that this event is executed when the transition and the slide change are carried out. I would like to remove the user the power to move the slide.

@rubycon
Copy link

rubycon commented Jun 20, 2019

Yes, the event is fired after the change but if you update the index in response, SideSwipe won't start rendering the transition. It might look a bit hacky but works great and render nice on Android and iOS for me:

<SideSwipe
  // ...
  index={this.state.index}
  onIndexChange={index => {
    const prevIndex = this.state.index;
    // update your index state in any case, to keep the value coherent
    // with the internal state of SideSwipe. This won't trigger another onIndexChange.
    this.setState({index}, () => {
      // then correct the index if needed.
      if(isNotAllowed(index))
        this.setState({index: prevIndex});
    })
  }}
/>

If you want to deny the move you can use the shouldCapture callback and test if the user is swiping left or right of the current index. Maybe something like (not tested):

shouldCapture={gestureState => {
  if(gestureState.dx > 0 && isNotAllowed(this.index + 1))
    return false;

  if(gestureState.dx < 0 && isNotAllowed(this.index - 1))
    return false;

  return true;
}}

@ruben2807
Copy link
Author

Thank you very much, with that I can work perfectly.
Excuse my bad English, greetings from Costa Rica.

@andrewsadowski
Copy link

andrewsadowski commented Jul 22, 2019

I'm having a similar issue; where I want to make sure that the user cannot interact with the side-swipe component, but returning false within shouldCapture is not disabling this currently. I recently updated to RN V-59.1 and ReactNativeNavigation V-2.22.3, and this behavior started. Are there any other props on Sideswipe you would recommend to block a users gestures?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants