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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @flow
  2. import React, { Component, type Node } from 'react';
  3. import { TouchableWithoutFeedback, View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { ColorSchemeRegistry } from '../../../color-scheme';
  6. import { Modal } from '../../../react';
  7. import { StyleType } from '../../../styles';
  8. import { bottomSheetStyles as styles } from './styles';
  9. /**
  10. * The type of {@code BottomSheet}'s React {@code Component} prop types.
  11. */
  12. type Props = {
  13. /**
  14. * The color-schemed stylesheet of the feature.
  15. */
  16. _styles: StyleType,
  17. /**
  18. * The children to be displayed within this component.
  19. */
  20. children: Node,
  21. /**
  22. * Handler for the cancel event, which happens when the user dismisses
  23. * the sheet.
  24. */
  25. onCancel: ?Function
  26. };
  27. /**
  28. * A component emulating Android's BottomSheet. For all intents and purposes,
  29. * this component has been designed to work and behave as a {@code Dialog}.
  30. */
  31. class BottomSheet extends Component<Props> {
  32. /**
  33. * Initializes a new {@code BottomSheet} instance.
  34. *
  35. * @inheritdoc
  36. */
  37. constructor(props: Props) {
  38. super(props);
  39. this._onCancel = this._onCancel.bind(this);
  40. }
  41. /**
  42. * Implements React's {@link Component#render()}.
  43. *
  44. * @inheritdoc
  45. * @returns {ReactElement}
  46. */
  47. render() {
  48. const { _styles } = this.props;
  49. return [
  50. <View
  51. key = 'overlay'
  52. style = { styles.overlay } />,
  53. <Modal
  54. key = 'modal'
  55. onRequestClose = { this._onCancel }
  56. visible = { true }>
  57. <View style = { styles.container }>
  58. <TouchableWithoutFeedback
  59. onPress = { this._onCancel } >
  60. <View style = { styles.backdrop } />
  61. </TouchableWithoutFeedback>
  62. <View style = { _styles.sheet }>
  63. { this.props.children }
  64. </View>
  65. </View>
  66. </Modal>
  67. ];
  68. }
  69. _onCancel: () => void;
  70. /**
  71. * Cancels the dialog by calling the onCancel prop callback.
  72. *
  73. * @private
  74. * @returns {void}
  75. */
  76. _onCancel() {
  77. const { onCancel } = this.props;
  78. onCancel && onCancel();
  79. }
  80. }
  81. /**
  82. * Maps part of the Redux state to the props of this component.
  83. *
  84. * @param {Object} state - The Redux state.
  85. * @returns {{
  86. * _styles: StyleType
  87. * }}
  88. */
  89. function _mapStateToProps(state) {
  90. return {
  91. _styles: ColorSchemeRegistry.get(state, 'BottomSheet')
  92. };
  93. }
  94. // $FlowExpectedError
  95. export default connect(_mapStateToProps)(BottomSheet);