Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Dialog.native.js 9.0KB

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