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.

SimpleBottomSheet.native.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // @flow
  2. import React, { Component } from 'react';
  3. import {
  4. Modal,
  5. Text,
  6. TouchableHighlight,
  7. TouchableWithoutFeedback,
  8. View
  9. } from 'react-native';
  10. import { connect } from 'react-redux';
  11. import { Icon } from '../../font-icons';
  12. import { simpleBottomSheet as styles } from './styles';
  13. /**
  14. * Underlay color for the buttons on the sheet.
  15. *
  16. * @type {string}
  17. */
  18. const BUTTON_UNDERLAY_COLOR = '#eee';
  19. /**
  20. * {@code SimpleBottomSheet}'s React {@code Component} prop types.
  21. */
  22. type Option = {
  23. /**
  24. * Name of the icon which will be rendered on the right.
  25. */
  26. iconName: string,
  27. /**
  28. * True if the element is selected (will be highlighted in blue),
  29. * false otherwise.
  30. */
  31. selected: boolean,
  32. /**
  33. * Text which will be rendered in the row.
  34. */
  35. text: string
  36. };
  37. type Props = {
  38. /**
  39. * Handler for the cancel event, which happens when the user dismisses
  40. * the sheet.
  41. */
  42. onCancel: Function,
  43. /**
  44. * Handler for the event when an option has been selected in the sheet.
  45. */
  46. onSubmit: Function,
  47. /**
  48. * Array of options which will be rendered as rows.
  49. */
  50. options: Array<Option>
  51. };
  52. /**
  53. * A component emulating Android's BottomSheet, in a simplified form.
  54. * It supports text options with an icon, which the user can tap. The style has
  55. * been implemented following the Material Design guidelines for bottom
  56. * sheets: https://material.io/guidelines/components/bottom-sheets.html
  57. *
  58. * For all intents and purposes, this component has been designed to work and
  59. * behave as a {@code Dialog}.
  60. */
  61. class SimpleBottomSheet extends Component<Props> {
  62. /**
  63. * Initializes a new {@code SimpleBottomSheet} instance.
  64. *
  65. * @param {Object} props - The read-only React {@code Component} props with
  66. * which the new instance is to be initialized.
  67. */
  68. constructor(props) {
  69. super(props);
  70. this._onButtonPress = this._onButtonPress.bind(this);
  71. this._onCancel = this._onCancel.bind(this);
  72. }
  73. /**
  74. * Implements React's {@link Component#render()}.
  75. *
  76. * @inheritdoc
  77. * @returns {ReactElement}
  78. */
  79. render() {
  80. return (
  81. <Modal
  82. animationType = { 'slide' }
  83. onRequestClose = { this._onCancel }
  84. transparent = { true }
  85. visible = { true }>
  86. <View style = { styles.container }>
  87. <TouchableWithoutFeedback
  88. onPress = { this._onCancel } >
  89. <View style = { styles.overlay } />
  90. </TouchableWithoutFeedback>
  91. <View style = { styles.sheet }>
  92. <View style = { styles.rowsWrapper }>
  93. { this._renderOptions() }
  94. </View>
  95. </View>
  96. </View>
  97. </Modal>
  98. );
  99. }
  100. _onButtonPress: (?Object) => void;
  101. /**
  102. * Handle pressing of one of the options. The sheet will be hidden and the
  103. * onSubmit prop will be called with the selected option.
  104. *
  105. * @param {Object} option - The option which the user selected.
  106. * @private
  107. * @returns {void}
  108. */
  109. _onButtonPress(option) {
  110. this.props.onSubmit && this.props.onSubmit(option);
  111. }
  112. _onCancel: () => void;
  113. /**
  114. * Cancels the dialog by calling the onCancel prop callback.
  115. *
  116. * @private
  117. * @returns {void}
  118. */
  119. _onCancel() {
  120. this.props.onCancel && this.props.onCancel();
  121. }
  122. /**
  123. * Renders sheet rows based on the options prop.
  124. *
  125. * @private
  126. * @returns {Array} - Array of rows to be rendered in the sheet.
  127. */
  128. _renderOptions() {
  129. return this.props.options.map(
  130. (option, index) => this._renderRow(option, index));
  131. }
  132. /**
  133. * Renders a single row of the sheet.
  134. *
  135. * @param {Object} option - Single option which needs to be rendered.
  136. * @param {int} index - Option index, used as a key for React.
  137. * @private
  138. * @returns {ReactElement} - A row element with an icon and text.
  139. */
  140. _renderRow(option, index) {
  141. const onPress = this._onButtonPress.bind(this, option);
  142. const { iconName, selected, text } = option;
  143. const selectedStyle = selected ? styles.rowSelectedText : {};
  144. return (
  145. <TouchableHighlight
  146. key = { index }
  147. onPress = { onPress }
  148. underlayColor = { BUTTON_UNDERLAY_COLOR } >
  149. <View style = { styles.row } >
  150. <Icon
  151. name = { iconName }
  152. style = { [ styles.rowIcon, selectedStyle ] } />
  153. <View style = { styles.rowPadding } />
  154. <Text style = { [ styles.rowText, selectedStyle ] } >
  155. { text }
  156. </Text>
  157. </View>
  158. </TouchableHighlight>
  159. );
  160. }
  161. }
  162. export default connect()(SimpleBottomSheet);