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

Dialog.native.js 9.3KB

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