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