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

Dialog.native.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. textInputProps = { this.props.textInputProps }
  103. title = { titleString || t(titleKey) }
  104. visible = { true } />
  105. );
  106. // XXX The following implements workarounds with knowledge of
  107. // react-native-prompt/Prompt's implementation.
  108. // eslint-disable-next-line no-extra-parens, new-cap
  109. element = (new (element.type)(element.props)).render();
  110. let { children } = this.props;
  111. children = React.Children.count(children) ? children : undefined;
  112. // eslint-disable-next-line no-shadow
  113. element = this._mapReactElement(element, element => {
  114. const { type } = element;
  115. if (type === Modal) {
  116. // * Modal handles hardware button presses for back navigation.
  117. // Firstly, we don't want Prompt's default behavior to merely
  118. // hide the Modal - we want this Dialog to be canceled.
  119. // Secondly, we cannot get Prompt's default behavior anyway
  120. // because we've removed Prompt and we're preserving whatever
  121. // it's rendered only.
  122. return this._cloneElement(element, /* props */ {
  123. onRequestClose: this._onCancel
  124. });
  125. }
  126. if (type === TextInput) {
  127. // * If this Dialog has children, they are to be rendered
  128. // instead of Prompt's TextInput.
  129. if (children) {
  130. element = children; // eslint-disable-line no-param-reassign
  131. children = undefined;
  132. }
  133. } else {
  134. let { style } = element.props;
  135. if (style
  136. && (style = StyleSheet.flatten(style))
  137. && _TAG_KEY in style) {
  138. switch (style[_TAG_KEY]) {
  139. case _SUBMIT_TEXT_TAG_VALUE:
  140. if (this.state.submitting) {
  141. // * If this Dialog is submitting, render a
  142. // LoadingIndicator.
  143. return (
  144. <LoadingIndicator
  145. color = { submitButtonTextStyle.color }
  146. size = { 'small' } />
  147. );
  148. }
  149. break;
  150. }
  151. return this._cloneElement(element, /* props */ {
  152. style: set(style, _TAG_KEY, undefined)
  153. });
  154. }
  155. }
  156. return element;
  157. });
  158. return element;
  159. }
  160. /**
  161. * Clones a specific {@code ReactElement} and adds/merges specific props
  162. * into the clone.
  163. *
  164. * @param {ReactElement} element - The {@code ReactElement} to clone.
  165. * @param {Object} props - The props to add/merge into the clone.
  166. * @returns {ReactElement} The close of the specified {@code element} with
  167. * the specified {@code props} added/merged.
  168. */
  169. _cloneElement(element, props) {
  170. return (
  171. React.cloneElement(
  172. element,
  173. props,
  174. ...React.Children.toArray(element.props.children)));
  175. }
  176. /**
  177. * Creates a deep clone of a specific {@code ReactElement} with the results
  178. * of calling a specific function on every node of a specific
  179. * {@code ReactElement} tree.
  180. *
  181. * @param {ReactElement} element - The {@code ReactElement} to clone and
  182. * call the specified {@code f} on.
  183. * @param {Function} f - The function to call on every node of the
  184. * {@code ReactElement} tree represented by the specified {@code element}.
  185. * @private
  186. * @returns {ReactElement}
  187. */
  188. _mapReactElement(element, f) {
  189. if (!element || !element.props || !element.type) {
  190. return element;
  191. }
  192. let mapped = f(element);
  193. if (mapped) {
  194. const { children } = mapped.props;
  195. if (mapped === element || React.Children.count(children)) {
  196. mapped
  197. = React.cloneElement(
  198. mapped,
  199. /* props */ undefined,
  200. ...React.Children.toArray(React.Children.map(
  201. children,
  202. function(element) { // eslint-disable-line no-shadow
  203. // eslint-disable-next-line no-invalid-this
  204. return this._mapReactElement(element, f);
  205. },
  206. this)));
  207. }
  208. }
  209. return mapped;
  210. }
  211. _onChangeText: (string) => void;
  212. /**
  213. * Notifies this {@code Dialog} that the text/value of the {@code TextInput}
  214. * rendered by {@code Prompt} has changed.
  215. *
  216. * @param {string} text - The new text/value of the {@code TextInput}
  217. * rendered by {@code Prompt}.
  218. * @returns {void}
  219. */
  220. _onChangeText(text: string) {
  221. this.setState({ text });
  222. }
  223. /**
  224. * Submits this {@code Dialog} with the value of the {@link TextInput}
  225. * rendered by {@link Prompt} unless a value is explicitly specified.
  226. *
  227. * @override
  228. * @param {string} [value] - The submitted value if any.
  229. * @returns {void}
  230. */
  231. _onSubmit(value: ?string) {
  232. super._onSubmit(value || this.state.text);
  233. }
  234. }
  235. export default translate(connect()(Dialog));