您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BottomSheet.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // @flow
  2. import React, { PureComponent, type Node } from 'react';
  3. import { PanResponder, SafeAreaView, ScrollView, View } from 'react-native';
  4. import { ColorSchemeRegistry } from '../../../color-scheme';
  5. import { SlidingView } from '../../../react';
  6. import { connect } from '../../../redux';
  7. import { StyleType } from '../../../styles';
  8. import { bottomSheetStyles as styles } from './styles';
  9. /**
  10. * Minimal distance that needs to be moved by the finger to consider it a swipe.
  11. */
  12. const GESTURE_DISTANCE_THRESHOLD = 5;
  13. /**
  14. * The minimal speed needed to be achieved by the finger to consider it as a swipe.
  15. */
  16. const GESTURE_SPEED_THRESHOLD = 0.2;
  17. /**
  18. * The type of {@code BottomSheet}'s React {@code Component} prop types.
  19. */
  20. type Props = {
  21. /**
  22. * The color-schemed stylesheet of the feature.
  23. */
  24. _styles: StyleType,
  25. /**
  26. * The children to be displayed within this component.
  27. */
  28. children: Node,
  29. /**
  30. * Handler for the cancel event, which happens when the user dismisses
  31. * the sheet.
  32. */
  33. onCancel: ?Function,
  34. /**
  35. * Callback to be attached to the custom swipe event of the BottomSheet.
  36. */
  37. onSwipe?: Function
  38. };
  39. /**
  40. * A component emulating Android's BottomSheet.
  41. */
  42. class BottomSheet extends PureComponent<Props> {
  43. panResponder: Object;
  44. /**
  45. * Instantiates a new component.
  46. *
  47. * @inheritdoc
  48. */
  49. constructor(props: Props) {
  50. super(props);
  51. this.panResponder = PanResponder.create({
  52. onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  53. onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  54. onPanResponderRelease: this._onGestureEnd.bind(this)
  55. });
  56. }
  57. /**
  58. * Implements React's {@link Component#render()}.
  59. *
  60. * @inheritdoc
  61. * @returns {ReactElement}
  62. */
  63. render() {
  64. const { _styles } = this.props;
  65. return (
  66. <SlidingView
  67. onHide = { this.props.onCancel }
  68. position = 'bottom'
  69. show = { true }>
  70. <View
  71. pointerEvents = 'box-none'
  72. style = { styles.sheetContainer }>
  73. <View
  74. pointerEvents = 'box-none'
  75. style = { styles.sheetAreaCover } />
  76. <SafeAreaView
  77. style = { [
  78. styles.sheetItemContainer,
  79. _styles.sheet
  80. ] }
  81. { ...this.panResponder.panHandlers }>
  82. <ScrollView
  83. bounces = { false }
  84. showsVerticalScrollIndicator = { false }
  85. style = { styles.scrollView } >
  86. { this.props.children }
  87. </ScrollView>
  88. </SafeAreaView>
  89. </View>
  90. </SlidingView>
  91. );
  92. }
  93. /**
  94. * Callback to handle a gesture end event.
  95. *
  96. * @param {Object} evt - The native gesture event.
  97. * @param {Object} gestureState - The gesture state.
  98. * @returns {void}
  99. */
  100. _onGestureEnd(evt, gestureState) {
  101. const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
  102. && Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
  103. if (verticalSwipe) {
  104. const direction = gestureState.vy > 0 ? 'down' : 'up';
  105. const { onCancel, onSwipe } = this.props;
  106. let isSwipeHandled = false;
  107. if (onSwipe) {
  108. isSwipeHandled = onSwipe(direction);
  109. }
  110. if (direction === 'down' && !isSwipeHandled) {
  111. // Swipe down is a special gesture that can be used to close the
  112. // BottomSheet, so if the swipe is not handled by the parent
  113. // component, we consider it as a request to close.
  114. onCancel && onCancel();
  115. }
  116. }
  117. }
  118. /**
  119. * Returns true if the pan responder should activate, false otherwise.
  120. *
  121. * @param {Object} evt - The native gesture event.
  122. * @param {Object} gestureState - The gesture state.
  123. * @returns {boolean}
  124. */
  125. _onShouldSetResponder({ nativeEvent }, gestureState) {
  126. return nativeEvent.touches.length === 1
  127. && Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
  128. && Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
  129. }
  130. }
  131. /**
  132. * Maps part of the Redux state to the props of this component.
  133. *
  134. * @param {Object} state - The Redux state.
  135. * @returns {{
  136. * _styles: StyleType
  137. * }}
  138. */
  139. function _mapStateToProps(state) {
  140. return {
  141. _styles: ColorSchemeRegistry.get(state, 'BottomSheet')
  142. };
  143. }
  144. export default connect(_mapStateToProps)(BottomSheet);