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

BottomSheet.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * Function to render a bottom sheet header element, if necessary.
  40. */
  41. renderHeader: ?Function
  42. };
  43. /**
  44. * A component emulating Android's BottomSheet.
  45. */
  46. class BottomSheet extends PureComponent<Props> {
  47. panResponder: Object;
  48. /**
  49. * Instantiates a new component.
  50. *
  51. * @inheritdoc
  52. */
  53. constructor(props: Props) {
  54. super(props);
  55. this.panResponder = PanResponder.create({
  56. onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  57. onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  58. onPanResponderRelease: this._onGestureEnd.bind(this)
  59. });
  60. }
  61. /**
  62. * Implements React's {@link Component#render()}.
  63. *
  64. * @inheritdoc
  65. * @returns {ReactElement}
  66. */
  67. render() {
  68. const { _styles, renderHeader } = this.props;
  69. return (
  70. <SlidingView
  71. accessibilityRole = 'menu'
  72. accessibilityViewIsModal = { true }
  73. onHide = { this.props.onCancel }
  74. position = 'bottom'
  75. show = { true }>
  76. <View
  77. pointerEvents = 'box-none'
  78. style = { styles.sheetContainer }>
  79. <View
  80. pointerEvents = 'box-none'
  81. style = { styles.sheetAreaCover } />
  82. { renderHeader && renderHeader() }
  83. <SafeAreaView
  84. style = { [
  85. styles.sheetItemContainer,
  86. _styles.sheet
  87. ] }
  88. { ...this.panResponder.panHandlers }>
  89. <ScrollView
  90. bounces = { false }
  91. showsVerticalScrollIndicator = { false }
  92. style = { styles.scrollView } >
  93. { this.props.children }
  94. </ScrollView>
  95. </SafeAreaView>
  96. </View>
  97. </SlidingView>
  98. );
  99. }
  100. /**
  101. * Callback to handle a gesture end event.
  102. *
  103. * @param {Object} evt - The native gesture event.
  104. * @param {Object} gestureState - The gesture state.
  105. * @returns {void}
  106. */
  107. _onGestureEnd(evt, gestureState) {
  108. const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
  109. && Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
  110. if (verticalSwipe) {
  111. const direction = gestureState.vy > 0 ? 'down' : 'up';
  112. const { onCancel, onSwipe } = this.props;
  113. let isSwipeHandled = false;
  114. if (onSwipe) {
  115. isSwipeHandled = onSwipe(direction);
  116. }
  117. if (direction === 'down' && !isSwipeHandled) {
  118. // Swipe down is a special gesture that can be used to close the
  119. // BottomSheet, so if the swipe is not handled by the parent
  120. // component, we consider it as a request to close.
  121. onCancel && onCancel();
  122. }
  123. }
  124. }
  125. /**
  126. * Returns true if the pan responder should activate, false otherwise.
  127. *
  128. * @param {Object} evt - The native gesture event.
  129. * @param {Object} gestureState - The gesture state.
  130. * @returns {boolean}
  131. */
  132. _onShouldSetResponder({ nativeEvent }, gestureState) {
  133. return nativeEvent.touches.length === 1
  134. && Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
  135. && Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
  136. }
  137. }
  138. /**
  139. * Maps part of the Redux state to the props of this component.
  140. *
  141. * @param {Object} state - The Redux state.
  142. * @returns {{
  143. * _styles: StyleType
  144. * }}
  145. */
  146. function _mapStateToProps(state) {
  147. return {
  148. _styles: ColorSchemeRegistry.get(state, 'BottomSheet')
  149. };
  150. }
  151. export default connect(_mapStateToProps)(BottomSheet);