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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import PropTypes from 'prop-types';
  2. import React from 'react';
  3. import { 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. // * If this Dialog has children, they are to be rendered instead of
  88. // Prompt's TextInput.
  89. if (element.type === TextInput) {
  90. if (children) {
  91. element = children; // eslint-disable-line no-param-reassign
  92. children = undefined;
  93. }
  94. } else {
  95. let { style } = element.props;
  96. if (style
  97. && (style = StyleSheet.flatten(style))
  98. && _TAG_KEY in style) {
  99. switch (style[_TAG_KEY]) {
  100. case _SUBMIT_TEXT_TAG_VALUE:
  101. if (this.state.submitting) {
  102. // * If this Dialog is submitting, render a
  103. // LoadingIndicator.
  104. return (
  105. <LoadingIndicator
  106. color = { submitButtonTextStyle.color }
  107. size = { 'small' } />
  108. );
  109. }
  110. break;
  111. }
  112. // eslint-disable-next-line no-param-reassign
  113. element
  114. = React.cloneElement(
  115. element,
  116. /* props */ {
  117. style: set(style, _TAG_KEY, undefined)
  118. },
  119. ...React.Children.toArray(element.props.children));
  120. }
  121. }
  122. return element;
  123. });
  124. return element;
  125. }
  126. /**
  127. * Creates a deep clone of a specific <tt>ReactElement</tt> with the results
  128. * of calling a specific function on every node of a specific
  129. * <tt>ReactElement</tt> tree.
  130. *
  131. * @param {ReactElement} element - The <tt>ReactElement</tt> to clone and
  132. * call the specified <tt>f</tt> on.
  133. * @param {Function} f - The function to call on every node of the
  134. * <tt>ReactElement</tt> tree represented by the specified <tt>element</tt>.
  135. * @private
  136. * @returns {ReactElement}
  137. */
  138. _mapReactElement(element, f) {
  139. if (!element || !element.props || !element.type) {
  140. return element;
  141. }
  142. let mapped = f(element);
  143. if (mapped === element) {
  144. mapped
  145. = React.cloneElement(
  146. element,
  147. /* props */ undefined,
  148. ...React.Children.toArray(React.Children.map(
  149. element.props.children,
  150. function(element) { // eslint-disable-line no-shadow
  151. // eslint-disable-next-line no-invalid-this
  152. return this._mapReactElement(element, f);
  153. },
  154. this)));
  155. }
  156. return mapped;
  157. }
  158. }
  159. export default translate(connect()(Dialog));