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.

Dialog.native.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import PropTypes from 'prop-types';
  2. import React from 'react';
  3. import { Modal, StyleSheet, TextInput } from 'react-native';
  4. import Prompt from 'react-native-prompt';
  5. import { connect } from 'react-redux';
  6. import { translate } from '../../i18n';
  7. import { LoadingIndicator } from '../../react';
  8. import { set } from '../../redux';
  9. import AbstractDialog from './AbstractDialog';
  10. import styles from './styles';
  11. /**
  12. * The value of the style property {@link _TAG_KEY} which identifies the
  13. * OK/submit button of <tt>Prompt</tt>.
  14. */
  15. const _SUBMIT_TEXT_TAG_VALUE = '_SUBMIT_TEXT_TAG_VALUE';
  16. /**
  17. * The name of the style property which identifies ancestors of <tt>Prompt</tt>
  18. * such as its OK/submit button for the purposes of workarounds implemented by
  19. * <tt>Dialog</tt>.
  20. *
  21. * XXX The value may trigger a react-native warning in the Debug configuration
  22. * but, unfortunately, I couldn't find a value that wouldn't.
  23. */
  24. const _TAG_KEY = '_TAG_KEY';
  25. /**
  26. * Implements <tt>AbstractDialog</tt> on react-native using <tt>Prompt</tt>.
  27. */
  28. class Dialog extends AbstractDialog {
  29. /**
  30. * <tt>AbstractDialog</tt>'s React <tt>Component</tt> prop types.
  31. *
  32. * @static
  33. */
  34. static propTypes = {
  35. ...AbstractDialog.propTypes,
  36. /**
  37. * I18n key to put as body title.
  38. */
  39. bodyKey: PropTypes.string
  40. };
  41. /**
  42. * Implements React's {@link Component#render()}.
  43. *
  44. * @inheritdoc
  45. * @returns {ReactElement}
  46. */
  47. render() {
  48. const {
  49. bodyKey,
  50. cancelDisabled,
  51. cancelTitleKey = 'dialog.Cancel',
  52. okDisabled,
  53. okTitleKey = 'dialog.Ok',
  54. t,
  55. titleKey,
  56. titleString
  57. } = this.props;
  58. const cancelButtonTextStyle
  59. = cancelDisabled ? styles.disabledButtonText : styles.buttonText;
  60. let submitButtonTextStyle
  61. = okDisabled ? styles.disabledButtonText : styles.buttonText;
  62. submitButtonTextStyle = {
  63. ...submitButtonTextStyle,
  64. [_TAG_KEY]: _SUBMIT_TEXT_TAG_VALUE
  65. };
  66. // eslint-disable-next-line no-extra-parens
  67. let element = (
  68. <Prompt
  69. cancelButtonTextStyle = { cancelButtonTextStyle }
  70. cancelText = { t(cancelTitleKey) }
  71. onCancel = { this._onCancel }
  72. onSubmit = { this._onSubmit }
  73. placeholder = { t(bodyKey) }
  74. submitButtonTextStyle = { submitButtonTextStyle }
  75. submitText = { t(okTitleKey) }
  76. title = { titleString || t(titleKey) }
  77. visible = { true } />
  78. );
  79. // XXX The following implements workarounds with knowledge of
  80. // react-native-prompt/Prompt's implementation.
  81. // eslint-disable-next-line no-extra-parens, new-cap
  82. element = (new (element.type)(element.props)).render();
  83. let { children } = this.props;
  84. children = React.Children.count(children) ? children : undefined;
  85. // eslint-disable-next-line no-shadow
  86. element = this._mapReactElement(element, element => {
  87. const { type } = element;
  88. if (type === Modal) {
  89. // * Modal handles hardware button presses for back navigation.
  90. // Firstly, we don't want Prompt's default behavior to merely
  91. // hide the Modal - we want this Dialog to be canceled.
  92. // Secondly, we cannot get Prompt's default behavior anyway
  93. // because we've removed Prompt and we're preserving whatever
  94. // it's rendered only.
  95. return (
  96. React.cloneElement(
  97. element,
  98. /* props */ {
  99. onRequestClose: this._onCancel
  100. },
  101. ...React.Children.toArray(element.props.children))
  102. );
  103. }
  104. if (type === TextInput) {
  105. // * If this Dialog has children, they are to be rendered
  106. // instead of Prompt's TextInput.
  107. if (children) {
  108. element = children; // eslint-disable-line no-param-reassign
  109. children = undefined;
  110. }
  111. } else {
  112. let { style } = element.props;
  113. if (style
  114. && (style = StyleSheet.flatten(style))
  115. && _TAG_KEY in style) {
  116. switch (style[_TAG_KEY]) {
  117. case _SUBMIT_TEXT_TAG_VALUE:
  118. if (this.state.submitting) {
  119. // * If this Dialog is submitting, render a
  120. // LoadingIndicator.
  121. return (
  122. <LoadingIndicator
  123. color = { submitButtonTextStyle.color }
  124. size = { 'small' } />
  125. );
  126. }
  127. break;
  128. }
  129. return (
  130. React.cloneElement(
  131. element,
  132. /* props */ {
  133. style: set(style, _TAG_KEY, undefined)
  134. },
  135. ...React.Children.toArray(element.props.children))
  136. );
  137. }
  138. }
  139. return element;
  140. });
  141. return element;
  142. }
  143. /**
  144. * Creates a deep clone of a specific <tt>ReactElement</tt> with the results
  145. * of calling a specific function on every node of a specific
  146. * <tt>ReactElement</tt> tree.
  147. *
  148. * @param {ReactElement} element - The <tt>ReactElement</tt> to clone and
  149. * call the specified <tt>f</tt> on.
  150. * @param {Function} f - The function to call on every node of the
  151. * <tt>ReactElement</tt> tree represented by the specified <tt>element</tt>.
  152. * @private
  153. * @returns {ReactElement}
  154. */
  155. _mapReactElement(element, f) {
  156. if (!element || !element.props || !element.type) {
  157. return element;
  158. }
  159. let mapped = f(element);
  160. if (mapped) {
  161. const { children } = mapped.props;
  162. if (mapped === element || React.Children.count(children)) {
  163. mapped
  164. = React.cloneElement(
  165. mapped,
  166. /* props */ undefined,
  167. ...React.Children.toArray(React.Children.map(
  168. children,
  169. function(element) { // eslint-disable-line no-shadow
  170. // eslint-disable-next-line no-invalid-this
  171. return this._mapReactElement(element, f);
  172. },
  173. this)));
  174. }
  175. }
  176. return mapped;
  177. }
  178. }
  179. export default translate(connect()(Dialog));