Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Dialog.native.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 {@code Prompt}.
  14. */
  15. const _SUBMIT_TEXT_TAG_VALUE = '_SUBMIT_TEXT_TAG_VALUE';
  16. /**
  17. * The name of the style property which identifies ancestors of {@code Prompt}
  18. * such as its OK/submit button for the purposes of workarounds implemented by
  19. * {@code Dialog}.
  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 {@code AbstractDialog} on react-native using {@code Prompt}.
  27. */
  28. class Dialog extends AbstractDialog {
  29. /**
  30. * {@code AbstractDialog}'s React {@code Component} 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. state = {
  42. /**
  43. * The text of the {@link TextInput} rendered by {@link Prompt} in
  44. * general and by this {@code Dialog} in particular if no
  45. * {@code children} are specified to it. It mimics/reimplements the
  46. * functionality of {@code Prompt} because this {@code Dialog} does not
  47. * really render the (whole) {@code Prompt}.
  48. *
  49. * @type {string}
  50. */
  51. text: ''
  52. };
  53. /**
  54. * Initailizes a new {@code Dialog} instance.
  55. *
  56. * @param {Object} props - The read-only React {@code Component} props with
  57. * which the new instance is to be initialized.
  58. */
  59. constructor(props: Object) {
  60. super(props);
  61. // Bind event handlers so they are only bound once per instance.
  62. this._onChangeText = this._onChangeText.bind(this);
  63. this._onSubmit = this._onSubmit.bind(this);
  64. }
  65. /**
  66. * Implements React's {@link Component#render()}.
  67. *
  68. * @inheritdoc
  69. * @returns {ReactElement}
  70. */
  71. render() {
  72. const {
  73. bodyKey,
  74. cancelDisabled,
  75. cancelTitleKey = 'dialog.Cancel',
  76. okDisabled,
  77. okTitleKey = 'dialog.Ok',
  78. t,
  79. titleKey,
  80. titleString
  81. } = this.props;
  82. const cancelButtonTextStyle
  83. = cancelDisabled ? styles.disabledButtonText : styles.buttonText;
  84. let submitButtonTextStyle
  85. = okDisabled ? styles.disabledButtonText : styles.buttonText;
  86. submitButtonTextStyle = {
  87. ...submitButtonTextStyle,
  88. [_TAG_KEY]: _SUBMIT_TEXT_TAG_VALUE
  89. };
  90. // eslint-disable-next-line no-extra-parens
  91. let element = (
  92. <Prompt
  93. cancelButtonTextStyle = { cancelButtonTextStyle }
  94. cancelText = { t(cancelTitleKey) }
  95. defaultValue = { this.state.text }
  96. onCancel = { this._onCancel }
  97. onChangeText = { this._onChangeText }
  98. onSubmit = { this._onSubmit }
  99. placeholder = { t(bodyKey) }
  100. submitButtonTextStyle = { submitButtonTextStyle }
  101. submitText = { t(okTitleKey) }
  102. title = { titleString || t(titleKey) }
  103. visible = { true } />
  104. );
  105. // XXX The following implements workarounds with knowledge of
  106. // react-native-prompt/Prompt's implementation.
  107. // eslint-disable-next-line no-extra-parens, new-cap
  108. element = (new (element.type)(element.props)).render();
  109. let { children } = this.props;
  110. children = React.Children.count(children) ? children : undefined;
  111. // eslint-disable-next-line no-shadow
  112. element = this._mapReactElement(element, element => {
  113. const { type } = element;
  114. if (type === Modal) {
  115. // * Modal handles hardware button presses for back navigation.
  116. // Firstly, we don't want Prompt's default behavior to merely
  117. // hide the Modal - we want this Dialog to be canceled.
  118. // Secondly, we cannot get Prompt's default behavior anyway
  119. // because we've removed Prompt and we're preserving whatever
  120. // it's rendered only.
  121. return this._cloneElement(element, /* props */ {
  122. onRequestClose: this._onCancel
  123. });
  124. }
  125. if (type === TextInput) {
  126. // * If this Dialog has children, they are to be rendered
  127. // instead of Prompt's TextInput.
  128. if (children) {
  129. element = children; // eslint-disable-line no-param-reassign
  130. children = undefined;
  131. }
  132. } else {
  133. let { style } = element.props;
  134. if (style
  135. && (style = StyleSheet.flatten(style))
  136. && _TAG_KEY in style) {
  137. switch (style[_TAG_KEY]) {
  138. case _SUBMIT_TEXT_TAG_VALUE:
  139. if (this.state.submitting) {
  140. // * If this Dialog is submitting, render a
  141. // LoadingIndicator.
  142. return (
  143. <LoadingIndicator
  144. color = { submitButtonTextStyle.color }
  145. size = { 'small' } />
  146. );
  147. }
  148. break;
  149. }
  150. return this._cloneElement(element, /* props */ {
  151. style: set(style, _TAG_KEY, undefined)
  152. });
  153. }
  154. }
  155. return element;
  156. });
  157. return element;
  158. }
  159. /**
  160. * Clones a specific {@code ReactElement} and adds/merges specific props
  161. * into the clone.
  162. *
  163. * @param {ReactElement} element - The {@code ReactElement} to clone.
  164. * @param {Object} props - The props to add/merge into the clone.
  165. * @returns {ReactElement} The close of the specified {@code element} with
  166. * the specified {@code props} added/merged.
  167. */
  168. _cloneElement(element, props) {
  169. return (
  170. React.cloneElement(
  171. element,
  172. props,
  173. ...React.Children.toArray(element.props.children)));
  174. }
  175. /**
  176. * Creates a deep clone of a specific {@code ReactElement} with the results
  177. * of calling a specific function on every node of a specific
  178. * {@code ReactElement} tree.
  179. *
  180. * @param {ReactElement} element - The {@code ReactElement} to clone and
  181. * call the specified {@code f} on.
  182. * @param {Function} f - The function to call on every node of the
  183. * {@code ReactElement} tree represented by the specified {@code element}.
  184. * @private
  185. * @returns {ReactElement}
  186. */
  187. _mapReactElement(element, f) {
  188. if (!element || !element.props || !element.type) {
  189. return element;
  190. }
  191. let mapped = f(element);
  192. if (mapped) {
  193. const { children } = mapped.props;
  194. if (mapped === element || React.Children.count(children)) {
  195. mapped
  196. = React.cloneElement(
  197. mapped,
  198. /* props */ undefined,
  199. ...React.Children.toArray(React.Children.map(
  200. children,
  201. function(element) { // eslint-disable-line no-shadow
  202. // eslint-disable-next-line no-invalid-this
  203. return this._mapReactElement(element, f);
  204. },
  205. this)));
  206. }
  207. }
  208. return mapped;
  209. }
  210. _onChangeText: (string) => void;
  211. /**
  212. * Notifies this {@code Dialog} that the text/value of the {@code TextInput}
  213. * rendered by {@code Prompt} has changed.
  214. *
  215. * @param {string} text - The new text/value of the {@code TextInput}
  216. * rendered by {@code Prompt}.
  217. * @returns {void}
  218. */
  219. _onChangeText(text: string) {
  220. this.setState({ text });
  221. }
  222. /**
  223. * Submits this {@code Dialog} with the value of the {@link TextInput}
  224. * rendered by {@link Prompt} unless a value is explicitly specified.
  225. *
  226. * @override
  227. * @param {string} [value] - The submitted value if any.
  228. * @returns {void}
  229. */
  230. _onSubmit(value: ?string) {
  231. super._onSubmit(value || this.state.text);
  232. }
  233. }
  234. export default translate(connect()(Dialog));