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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. supportedOrientations: [ 'landscape', 'portrait' ]
  135. });
  136. }
  137. if (type === TextInput) {
  138. // * If this Dialog has children, they are to be rendered
  139. // instead of Prompt's TextInput.
  140. if (children) {
  141. // $FlowFixMe
  142. el = children; // eslint-disable-line no-param-reassign
  143. children = undefined;
  144. }
  145. } else {
  146. let { style } = el.props;
  147. if (style
  148. && (style = StyleSheet.flatten(style))
  149. && _TAG_KEY in style) {
  150. // $FlowFixMe
  151. switch (style[_TAG_KEY]) {
  152. case _SUBMIT_TEXT_TAG_VALUE:
  153. if (this.state.submitting) {
  154. // * If this Dialog is submitting, render a
  155. // LoadingIndicator.
  156. return (
  157. <LoadingIndicator
  158. color = { submitButtonTextStyle.color }
  159. size = { 'small' } />
  160. );
  161. }
  162. break;
  163. }
  164. return this._cloneElement(el, /* props */ {
  165. style: set(style, _TAG_KEY, undefined)
  166. });
  167. }
  168. }
  169. return el;
  170. });
  171. return el;
  172. }
  173. /**
  174. * Clones a specific {@code ReactElement} and adds/merges specific props
  175. * into the clone.
  176. *
  177. * @param {ReactElement} el - The {@code ReactElement} to clone.
  178. * @param {Object} props - The props to add/merge into the clone.
  179. * @returns {ReactElement} The close of the specified {@code el} with
  180. * the specified {@code props} added/merged.
  181. */
  182. _cloneElement(el: React$Element<*>, props) {
  183. return (
  184. React.cloneElement(
  185. el,
  186. props,
  187. ...React.Children.toArray(el.props.children)));
  188. }
  189. /**
  190. * Creates a deep clone of a specific {@code ReactElement} with the results
  191. * of calling a specific function on every node of a specific
  192. * {@code ReactElement} tree.
  193. *
  194. * @param {ReactElement} el - The {@code ReactElement} to clone and
  195. * call the specified {@code f} on.
  196. * @param {Function} f - The function to call on every node of the
  197. * {@code ReactElement} tree represented by the specified {@code el}.
  198. * @private
  199. * @returns {ReactElement}
  200. */
  201. _mapReactElement(
  202. el: ?React$Element<*>,
  203. f: (React$Element<*>) => ?React$Element<*>): ?React$Element<*> {
  204. if (!el || !el.props || !el.type) {
  205. return el;
  206. }
  207. let mapped = f(el);
  208. if (mapped) {
  209. const { children } = mapped.props;
  210. if (mapped === el || React.Children.count(children)) {
  211. mapped
  212. = React.cloneElement(
  213. mapped,
  214. /* props */ {},
  215. ...React.Children.toArray(React.Children.map(
  216. children,
  217. function(el) { // eslint-disable-line no-shadow
  218. // eslint-disable-next-line no-invalid-this
  219. return this._mapReactElement(el, f);
  220. },
  221. this)));
  222. }
  223. }
  224. return mapped;
  225. }
  226. _onCancel: () => void;
  227. _onChangeText: (string) => void;
  228. /**
  229. * Notifies this {@code Dialog} that the text/value of the {@code TextInput}
  230. * rendered by {@code Prompt} has changed.
  231. *
  232. * @param {string} text - The new text/value of the {@code TextInput}
  233. * rendered by {@code Prompt}.
  234. * @returns {void}
  235. */
  236. _onChangeText(text: string) {
  237. this.setState({ text });
  238. }
  239. _onSubmit: (?string) => void;
  240. /**
  241. * Submits this {@code Dialog} with the value of the {@link TextInput}
  242. * rendered by {@link Prompt} unless a value is explicitly specified.
  243. *
  244. * @override
  245. * @param {string} [value] - The submitted value if any.
  246. * @returns {void}
  247. */
  248. _onSubmit(value: ?string) {
  249. // $FlowFixMeState
  250. super._onSubmit(value || this.state.text);
  251. }
  252. }
  253. export default translate(connect()(Dialog));