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.native.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @flow
  2. import React, { Component, type Node } from 'react';
  3. import { Modal, TouchableWithoutFeedback, View } from 'react-native';
  4. import { bottomSheetStyles as styles } from './styles';
  5. /**
  6. * The type of {@code BottomSheet}'s React {@code Component} prop types.
  7. */
  8. type Props = {
  9. /**
  10. * The children to be displayed within this component.
  11. */
  12. children: Node,
  13. /**
  14. * Handler for the cancel event, which happens when the user dismisses
  15. * the sheet.
  16. */
  17. onCancel: ?Function
  18. };
  19. /**
  20. * A component emulating Android's BottomSheet. For all intents and purposes,
  21. * this component has been designed to work and behave as a {@code Dialog}.
  22. */
  23. export default class BottomSheet extends Component<Props> {
  24. /**
  25. * Initializes a new {@code BottomSheet} instance.
  26. *
  27. * @inheritdoc
  28. */
  29. constructor(props: Props) {
  30. super(props);
  31. this._onCancel = this._onCancel.bind(this);
  32. }
  33. /**
  34. * Implements React's {@link Component#render()}.
  35. *
  36. * @inheritdoc
  37. * @returns {ReactElement}
  38. */
  39. render() {
  40. return [
  41. <View
  42. key = 'overlay'
  43. style = { styles.overlay } />,
  44. <Modal
  45. animationType = { 'slide' }
  46. key = 'modal'
  47. onRequestClose = { this._onCancel }
  48. transparent = { true }
  49. visible = { true }>
  50. <View style = { styles.container }>
  51. <TouchableWithoutFeedback
  52. onPress = { this._onCancel } >
  53. <View style = { styles.backdrop } />
  54. </TouchableWithoutFeedback>
  55. <View style = { styles.sheet }>
  56. { this.props.children }
  57. </View>
  58. </View>
  59. </Modal>
  60. ];
  61. }
  62. _onCancel: () => void;
  63. /**
  64. * Cancels the dialog by calling the onCancel prop callback.
  65. *
  66. * @private
  67. * @returns {void}
  68. */
  69. _onCancel() {
  70. const { onCancel } = this.props;
  71. onCancel && onCancel();
  72. }
  73. }