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 9.3KB

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