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

Dialog.js 9.5KB

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