UNPKG

react-native-persian-calendar-modal

Version:

Persian Calendar Modal Component for React Native

140 lines (121 loc) 3.99 kB
/** * Persian Calendar Picker Component * * Copyright 2016 Reza (github.com/rghorbani) * Licensed under the terms of the MIT license. See LICENSE file in the project root for terms. */ 'use strict'; const React = require('react'); const { PanResponder, View } = require('react-native'); export const swipeDirections = { SWIPE_UP: 'SWIPE_UP', SWIPE_DOWN: 'SWIPE_DOWN', SWIPE_LEFT: 'SWIPE_LEFT', SWIPE_RIGHT: 'SWIPE_RIGHT', }; const swipeConfig = { velocityThreshold: 0.1, directionalOffsetThreshold: 15, }; function isValidSwipe( velocity, velocityThreshold, directionalOffset, directionalOffsetThreshold, ) { // console.log(velocity,velocityThreshold,directionalOffset,directionalOffsetThreshold); return true; return ( Math.abs(velocity) > velocityThreshold && Math.abs(directionalOffset) < directionalOffsetThreshold ); } class Swiper extends React.Component { constructor(props, context) { super(props, context); this.swipeConfig = Object.assign(swipeConfig, props.config); const responderEnd = this._handlePanResponderEnd.bind(this); const shouldSetResponder = this._handleShouldSetPanResponder.bind(this); this._panResponder = PanResponder.create({ //stop JS beautify collapse onStartShouldSetPanResponder: shouldSetResponder, onMoveShouldSetPanResponder: shouldSetResponder, onPanResponderRelease: responderEnd, onPanResponderTerminate: responderEnd, }); } componentDidUpdate(prevProps) { if (prevProps.config !== this.props.config) { this.swipeConfig = Object.assign(swipeConfig, this.props.config); } } _handleShouldSetPanResponder(evt, gestureState) { // return true; // console.log('1',evt.nativeEvent.touches.length); // console.log('2',!this._gestureIsClick(gestureState)); return ( evt.nativeEvent.touches.length === 1 && !this._gestureIsClick(gestureState) ); } _gestureIsClick(gestureState) { return true; return Math.abs(gestureState.dx) < 2 ; //&& Math.abs(gestureState.dy) < 2; } _handlePanResponderEnd(evt, gestureState) { const swipeDirection = this._getSwipeDirection(gestureState); this._triggerSwipeHandlers(swipeDirection, gestureState); } _triggerSwipeHandlers(swipeDirection, gestureState) { const { onSwipe, onSwipeUp, onSwipeDown, onSwipeLeft, onSwipeRight, } = this.props; const { SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN } = swipeDirections; onSwipe && onSwipe(swipeDirection, gestureState); switch (swipeDirection) { case SWIPE_LEFT: onSwipeLeft && onSwipeLeft(gestureState); break; case SWIPE_RIGHT: onSwipeRight && onSwipeRight(gestureState); break; case SWIPE_UP: onSwipeUp && onSwipeUp(gestureState); break; case SWIPE_DOWN: onSwipeDown && onSwipeDown(gestureState); break; } } _getSwipeDirection(gestureState) { const { SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN } = swipeDirections; const { dx, dy } = gestureState; if (this._isValidHorizontalSwipe(gestureState)) { return dx > 0 ? SWIPE_RIGHT : SWIPE_LEFT; } else if (this._isValidVerticalSwipe(gestureState)) { return dy > 0 ? SWIPE_DOWN : SWIPE_UP; } return null; } _isValidHorizontalSwipe(gestureState) { const { vx, dy } = gestureState; const { velocityThreshold, directionalOffsetThreshold } = this.swipeConfig; // return true; return isValidSwipe(vx, velocityThreshold, dy, directionalOffsetThreshold); } _isValidVerticalSwipe(gestureState) { const { vy, dx } = gestureState; const { velocityThreshold, directionalOffsetThreshold } = this.swipeConfig; // return true; return isValidSwipe(vy, velocityThreshold, dx, directionalOffsetThreshold); } render() { return <View {...this.props} {...this._panResponder.panHandlers} />; } } module.exports = Swiper;