You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BottomSheet.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { type Node, PureComponent } from 'react';
  2. import { SafeAreaView, ScrollView, View } from 'react-native';
  3. import { SlidingView } from '../../../react';
  4. import { connect } from '../../../redux';
  5. import { hideSheet } from '../../actions';
  6. import { bottomSheetStyles as styles } from './styles';
  7. /**
  8. * The type of {@code BottomSheet}'s React {@code Component} prop types.
  9. */
  10. type Props = {
  11. /**
  12. * Whether to add padding to scroll view.
  13. */
  14. addScrollViewPadding?: boolean,
  15. /**
  16. * The children to be displayed within this component.
  17. */
  18. children: Node,
  19. /**
  20. * Redux Dispatch function.
  21. */
  22. dispatch: Function,
  23. /**
  24. * Handler for the cancel event, which happens when the user dismisses
  25. * the sheet.
  26. */
  27. onCancel: ?Function,
  28. /**
  29. * Function to render a bottom sheet header element, if necessary.
  30. */
  31. renderHeader: ?Function,
  32. /**
  33. * Function to render a bottom sheet footer element, if necessary.
  34. */
  35. renderFooter: ?Function,
  36. /**
  37. * Whether to show sliding view or not.
  38. */
  39. showSlidingView?: boolean,
  40. /**
  41. * The component's external style.
  42. */
  43. style: Object
  44. };
  45. /**
  46. * A component emulating Android's BottomSheet.
  47. */
  48. class BottomSheet extends PureComponent<Props> {
  49. /**
  50. * Default values for {@code BottomSheet} component's properties.
  51. *
  52. * @static
  53. */
  54. static defaultProps = {
  55. addScrollViewPadding: true,
  56. showSlidingView: true
  57. };
  58. /**
  59. * Initializes a new instance.
  60. *
  61. * @param {Props} props - The React {@code Component} props to initialize
  62. * the new instance with.
  63. */
  64. constructor(props: Props) {
  65. super(props);
  66. this._onCancel = this._onCancel.bind(this);
  67. }
  68. /**
  69. * Handles the cancel event, when the user dismissed the sheet. By default we close it.
  70. *
  71. * @returns {void}
  72. */
  73. _onCancel() {
  74. if (this.props.onCancel) {
  75. this.props.onCancel();
  76. } else {
  77. this.props.dispatch(hideSheet());
  78. }
  79. }
  80. /**
  81. * Implements React's {@link Component#render()}.
  82. *
  83. * @inheritdoc
  84. * @returns {ReactElement}
  85. */
  86. render() {
  87. const {
  88. addScrollViewPadding,
  89. renderHeader,
  90. renderFooter,
  91. showSlidingView,
  92. style
  93. } = this.props;
  94. return (
  95. <SlidingView
  96. accessibilityRole = 'menu'
  97. accessibilityViewIsModal = { true }
  98. onHide = { this._onCancel }
  99. position = 'bottom'
  100. show = { showSlidingView }>
  101. <View
  102. pointerEvents = 'box-none'
  103. style = { styles.sheetContainer }>
  104. <View
  105. pointerEvents = 'box-none'
  106. style = { styles.sheetAreaCover } />
  107. { renderHeader && renderHeader() }
  108. <SafeAreaView
  109. style = { [
  110. styles.sheetItemContainer,
  111. renderHeader
  112. ? styles.sheetHeader
  113. : styles.sheet,
  114. renderFooter && styles.sheetFooter,
  115. style
  116. ] }>
  117. <ScrollView
  118. bounces = { false }
  119. showsVerticalScrollIndicator = { false }
  120. style = { [
  121. renderFooter && styles.sheet,
  122. addScrollViewPadding && styles.scrollView
  123. ] } >
  124. { this.props.children }
  125. </ScrollView>
  126. { renderFooter && renderFooter() }
  127. </SafeAreaView>
  128. </View>
  129. </SlidingView>
  130. );
  131. }
  132. }
  133. export default connect()(BottomSheet);