Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BottomSheet.tsx 4.0KB

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