@ -1,40 +1,15 @@
// @flow
import React , { PureComponent , type Node } from 'react' ;
import { PanResponder , SafeAreaView , ScrollView , View } from 'react-native' ;
import { SafeAreaView , ScrollView , View } from 'react-native' ;
import { ColorSchemeRegistry } from '../../../color-scheme' ;
import { SlidingView } from '../../../react' ;
import { connect } from '../../../redux' ;
import { StyleType } from '../../../styles' ;
import { bottomSheetStyles as styles } from './styles' ;
/ * *
* Minimal distance that needs to be moved by the finger to consider it a swipe .
* /
const GESTURE _DISTANCE _THRESHOLD = 5 ;
/ * *
* The minimal speed needed to be achieved by the finger to consider it as a swipe .
* /
const GESTURE _SPEED _THRESHOLD = 0.2 ;
/ * *
* The type of { @ code BottomSheet } ' s React { @ code Component } prop types .
* /
type Props = {
/ * *
* The height of the screen .
* /
_height : number ,
/ * *
* The color - schemed stylesheet of the feature .
* /
_styles : StyleType ,
/ * *
* Whether to add padding to scroll view .
* /
@ -51,11 +26,6 @@ type Props = {
* /
onCancel : ? Function ,
/ * *
* Callback to be attached to the custom swipe event of the BottomSheet .
* /
onSwipe ? : Function ,
/ * *
* Function to render a bottom sheet header element , if necessary .
* /
@ -81,8 +51,6 @@ type Props = {
* A component emulating Android ' s BottomSheet .
* /
class BottomSheet extends PureComponent < Props > {
panResponder : Object ;
/ * *
* Default values for { @ code BottomSheet } component ' s properties .
*
@ -93,21 +61,6 @@ class BottomSheet extends PureComponent<Props> {
showSlidingView : true
} ;
/ * *
* Instantiates a new component .
*
* @ inheritdoc
* /
constructor ( props : Props ) {
super ( props ) ;
this . panResponder = PanResponder . create ( {
onStartShouldSetPanResponder : this . _onShouldSetResponder . bind ( this ) ,
onMoveShouldSetPanResponder : this . _onShouldSetResponder . bind ( this ) ,
onPanResponderRelease : this . _onGestureEnd . bind ( this )
} ) ;
}
/ * *
* Implements React ' s { @ link Component # render ( ) } .
*
@ -116,8 +69,6 @@ class BottomSheet extends PureComponent<Props> {
* /
render ( ) {
const {
_height ,
_styles ,
addScrollViewPadding ,
renderHeader ,
renderFooter ,
@ -143,20 +94,16 @@ class BottomSheet extends PureComponent<Props> {
style = { [
styles . sheetItemContainer ,
renderHeader
? _styles . sheetHeader
: _styles . sheet ,
renderFooter && _styles . sheetFooter ,
style ,
{
maxHeight : _height - 100
}
] }
{ ... this . panResponder . panHandlers } >
? styles . sheetHeader
: styles . sheet ,
renderFooter && styles . sheetFooter ,
style
] } >
< ScrollView
bounces = { false }
showsVerticalScrollIndicator = { false }
style = { [
renderFooter && _ styles. sheet ,
renderFooter && styles . sheet ,
addScrollViewPadding && styles . scrollView
] } >
{ this . props . children }
@ -167,63 +114,6 @@ class BottomSheet extends PureComponent<Props> {
< / S l i d i n g V i e w >
) ;
}
/ * *
* Callback to handle a gesture end event .
*
* @ param { Object } evt - The native gesture event .
* @ param { Object } gestureState - The gesture state .
* @ returns { void }
* /
_onGestureEnd ( evt , gestureState ) {
const verticalSwipe = Math . abs ( gestureState . vy ) > Math . abs ( gestureState . vx )
&& Math . abs ( gestureState . vy ) > GESTURE _SPEED _THRESHOLD ;
if ( verticalSwipe ) {
const direction = gestureState . vy > 0 ? 'down' : 'up' ;
const { onCancel , onSwipe } = this . props ;
let isSwipeHandled = false ;
if ( onSwipe ) {
isSwipeHandled = onSwipe ( direction ) ;
}
if ( direction === 'down' && ! isSwipeHandled ) {
// Swipe down is a special gesture that can be used to close the
// BottomSheet, so if the swipe is not handled by the parent
// component, we consider it as a request to close.
onCancel && onCancel ( ) ;
}
}
}
/ * *
* Returns true if the pan responder should activate , false otherwise .
*
* @ param { Object } evt - The native gesture event .
* @ param { Object } gestureState - The gesture state .
* @ returns { boolean }
* /
_onShouldSetResponder ( { nativeEvent } , gestureState ) {
return nativeEvent . touches . length === 1
&& Math . abs ( gestureState . dx ) > GESTURE _DISTANCE _THRESHOLD
&& Math . abs ( gestureState . dy ) > GESTURE _DISTANCE _THRESHOLD ;
}
}
/ * *
* Maps part of the Redux state to the props of this component .
*
* @ param { Object } state - The Redux state .
* @ returns { {
* _styles : StyleType
* } }
* /
function _mapStateToProps ( state ) {
return {
_styles : ColorSchemeRegistry . get ( state , 'BottomSheet' ) ,
_height : state [ 'features/base/responsive-ui' ] . clientHeight
} ;
}
export default connect ( _mapStateToProps ) ( BottomSheet ) ;
export default BottomSheet ;