您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SimpleBottomSheet.native.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. type Option = {
  20. /**
  21. * Name of the icon which will be rendered on the right.
  22. */
  23. iconName: string,
  24. /**
  25. * True if the element is selected (will be highlighted in blue),
  26. * false otherwise.
  27. */
  28. selected: boolean,
  29. /**
  30. * Text which will be rendered in the row.
  31. */
  32. text: string
  33. };
  34. /**
  35. * The type of {@code SimpleBottomSheet}'s React {@code Component} prop types.
  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. const { onSubmit } = this.props;
  111. onSubmit && onSubmit(option);
  112. }
  113. _onCancel: () => void;
  114. /**
  115. * Cancels the dialog by calling the onCancel prop callback.
  116. *
  117. * @private
  118. * @returns {void}
  119. */
  120. _onCancel() {
  121. const { onCancel } = this.props;
  122. onCancel && onCancel();
  123. }
  124. /**
  125. * Renders sheet rows based on the options prop.
  126. *
  127. * @private
  128. * @returns {Array} - Array of rows to be rendered in the sheet.
  129. */
  130. _renderOptions() {
  131. return this.props.options.map(
  132. (option, index) => this._renderRow(option, index));
  133. }
  134. /**
  135. * Renders a single row of the sheet.
  136. *
  137. * @param {Object} option - Single option which needs to be rendered.
  138. * @param {int} index - Option index, used as a key for React.
  139. * @private
  140. * @returns {ReactElement} - A row element with an icon and text.
  141. */
  142. _renderRow(option, index) {
  143. const { iconName, selected, text } = option;
  144. const selectedStyle = selected ? styles.rowSelectedText : {};
  145. return (
  146. <TouchableHighlight
  147. key = { index }
  148. // TODO The following disables an eslint error alerting about a
  149. // known potential/theoretical performance pernalty:
  150. //
  151. // A bind call or arrow function in a JSX prop will create a
  152. // brand new function on every single render. This is bad for
  153. // performance, as it will result in the garbage collector being
  154. // invoked way more than is necessary. It may also cause
  155. // unnecessary re-renders if a brand new function is passed as a
  156. // prop to a component that uses reference equality check on the
  157. // prop to determine if it should update.
  158. //
  159. // I'm not addressing the potential/theoretical performance
  160. // penalty at the time of this writing because it doesn't seem
  161. // to me that it's a practical performance penalty in the case.
  162. //
  163. // eslint-disable-next-line react/jsx-no-bind
  164. onPress = { this._onButtonPress.bind(this, option) }
  165. underlayColor = { BUTTON_UNDERLAY_COLOR } >
  166. <View style = { styles.row } >
  167. <Icon
  168. name = { iconName }
  169. style = { [ styles.rowIcon, selectedStyle ] } />
  170. <View style = { styles.rowPadding } />
  171. <Text style = { [ styles.rowText, selectedStyle ] } >
  172. { text }
  173. </Text>
  174. </View>
  175. </TouchableHighlight>
  176. );
  177. }
  178. }
  179. export default connect()(SimpleBottomSheet);