|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+import PropTypes from 'prop-types';
|
|
1
|
2
|
import React from 'react';
|
|
|
3
|
+import { TextInput } from 'react-native';
|
|
2
|
4
|
import Prompt from 'react-native-prompt';
|
|
3
|
5
|
import { connect } from 'react-redux';
|
|
4
|
6
|
|
|
|
@@ -7,20 +9,21 @@ import { translate } from '../../i18n';
|
|
7
|
9
|
import AbstractDialog from './AbstractDialog';
|
|
8
|
10
|
|
|
9
|
11
|
/**
|
|
10
|
|
- * Native dialog using Prompt.
|
|
|
12
|
+ * Implements <tt>AbstractDialog</tt> on react-native using <tt>Prompt</tt>.
|
|
11
|
13
|
*/
|
|
12
|
14
|
class Dialog extends AbstractDialog {
|
|
13
|
|
-
|
|
14
|
15
|
/**
|
|
15
|
|
- * Native sialog component's property types.
|
|
|
16
|
+ * <tt>AbstractDialog</tt>'s React <tt>Component</tt> prop types.
|
|
16
|
17
|
*
|
|
17
|
18
|
* @static
|
|
18
|
19
|
*/
|
|
19
|
20
|
static propTypes = {
|
|
|
21
|
+ ...AbstractDialog.propTypes,
|
|
|
22
|
+
|
|
20
|
23
|
/**
|
|
21
|
24
|
* I18n key to put as body title.
|
|
22
|
25
|
*/
|
|
23
|
|
- bodyKey: React.PropTypes.string
|
|
|
26
|
+ bodyKey: PropTypes.string
|
|
24
|
27
|
};
|
|
25
|
28
|
|
|
26
|
29
|
/**
|
|
|
@@ -31,27 +34,109 @@ class Dialog extends AbstractDialog {
|
|
31
|
34
|
*/
|
|
32
|
35
|
render() {
|
|
33
|
36
|
const {
|
|
34
|
|
- cancelDisabled,
|
|
35
|
|
- cancelTitleKey,
|
|
36
|
37
|
bodyKey,
|
|
|
38
|
+ cancelDisabled,
|
|
|
39
|
+ cancelTitleKey = 'dialog.Cancel',
|
|
|
40
|
+ children,
|
|
37
|
41
|
okDisabled,
|
|
38
|
|
- okTitleKey,
|
|
|
42
|
+ okTitleKey = 'dialog.Ok',
|
|
39
|
43
|
t,
|
|
40
|
|
- titleKey
|
|
|
44
|
+ titleKey,
|
|
|
45
|
+ titleString
|
|
41
|
46
|
} = this.props;
|
|
42
|
47
|
|
|
43
|
|
- return (
|
|
44
|
|
- <Prompt
|
|
45
|
|
- cancelText = { cancelDisabled
|
|
46
|
|
- ? undefined : t(cancelTitleKey || 'dialog.Cancel') }
|
|
|
48
|
+ /* eslint-disable react/jsx-wrap-multilines */
|
|
|
49
|
+
|
|
|
50
|
+ let element
|
|
|
51
|
+ = <Prompt
|
|
|
52
|
+ cancelText = { cancelDisabled ? undefined : t(cancelTitleKey) }
|
|
47
|
53
|
onCancel = { this._onCancel }
|
|
48
|
54
|
onSubmit = { this._onSubmit }
|
|
49
|
55
|
placeholder = { t(bodyKey) }
|
|
50
|
|
- submitText = { okDisabled
|
|
51
|
|
- ? undefined : t(okTitleKey || 'dialog.Ok') }
|
|
52
|
|
- title = { t(titleKey) }
|
|
53
|
|
- visible = { true } />
|
|
54
|
|
- );
|
|
|
56
|
+ submitText = { okDisabled ? undefined : t(okTitleKey) }
|
|
|
57
|
+ title = { titleString || t(titleKey) }
|
|
|
58
|
+ visible = { true } />;
|
|
|
59
|
+
|
|
|
60
|
+ /* eslint-enable react/jsx-wrap-multilines */
|
|
|
61
|
+
|
|
|
62
|
+ if (React.Children.count(children)) {
|
|
|
63
|
+ // XXX The following implements a workaround with knowledge of the
|
|
|
64
|
+ // implementation of react-native-prompt.
|
|
|
65
|
+ element
|
|
|
66
|
+ = this._replaceFirstElementOfType(
|
|
|
67
|
+ // eslint-disable-next-line no-extra-parens, new-cap
|
|
|
68
|
+ (new (element.type)(element.props)).render(),
|
|
|
69
|
+ TextInput,
|
|
|
70
|
+ children);
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ return element;
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ /**
|
|
|
77
|
+ * Creates a deep clone of a specific <tt>ReactElement</tt> with the results
|
|
|
78
|
+ * of calling a specific function on every node of a specific
|
|
|
79
|
+ * <tt>ReactElement</tt> tree.
|
|
|
80
|
+ *
|
|
|
81
|
+ * @param {ReactElement} element - The <tt>ReactElement</tt> to clone and
|
|
|
82
|
+ * call the specified <tt>f</tt> on.
|
|
|
83
|
+ * @param {Function} f - The function to call on every node of the
|
|
|
84
|
+ * <tt>ReactElement</tt> tree represented by the specified <tt>element</tt>.
|
|
|
85
|
+ * @private
|
|
|
86
|
+ * @returns {ReactElement}
|
|
|
87
|
+ */
|
|
|
88
|
+ _mapReactElement(element, f) {
|
|
|
89
|
+ if (!element || !element.props || !element.type) {
|
|
|
90
|
+ return element;
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+ let mapped = f(element);
|
|
|
94
|
+
|
|
|
95
|
+ if (mapped === element) {
|
|
|
96
|
+ mapped
|
|
|
97
|
+ = React.cloneElement(
|
|
|
98
|
+ element,
|
|
|
99
|
+ /* props */ undefined,
|
|
|
100
|
+ ...React.Children.toArray(React.Children.map(
|
|
|
101
|
+ element.props.children,
|
|
|
102
|
+ function(element) { // eslint-disable-line no-shadow
|
|
|
103
|
+ // eslint-disable-next-line no-invalid-this
|
|
|
104
|
+ return this._mapReactElement(element, f);
|
|
|
105
|
+ },
|
|
|
106
|
+ this)));
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ return mapped;
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ /**
|
|
|
113
|
+ * Replaces the first <tt>ReactElement</tt> of a specific type found in a
|
|
|
114
|
+ * specific <tt>ReactElement</tt> tree with a specific replacement
|
|
|
115
|
+ * <tt>ReactElement</tt>.
|
|
|
116
|
+ *
|
|
|
117
|
+ * @param {ReactElement} element - The <tt>ReactElement</tt> tree to search
|
|
|
118
|
+ * through and replace in.
|
|
|
119
|
+ * @param {*} type - The type of the <tt>ReactElement</tt> to be replaced.
|
|
|
120
|
+ * @param {ReactElement} replacement - The <tt>ReactElement</tt> to replace
|
|
|
121
|
+ * the first <tt>ReactElement</tt> in <tt>element</tt> of the specified
|
|
|
122
|
+ * <tt>type</tt>.
|
|
|
123
|
+ * @private
|
|
|
124
|
+ * @returns {ReactElement}
|
|
|
125
|
+ */
|
|
|
126
|
+ _replaceFirstElementOfType(element, type, replacement) {
|
|
|
127
|
+ // eslint-disable-next-line no-shadow
|
|
|
128
|
+ return this._mapReactElement(element, element => {
|
|
|
129
|
+ if (replacement && element.type === type) {
|
|
|
130
|
+ /* eslint-disable no-param-reassign */
|
|
|
131
|
+
|
|
|
132
|
+ element = replacement;
|
|
|
133
|
+ replacement = undefined;
|
|
|
134
|
+
|
|
|
135
|
+ /* eslint-enable no-param-reassign */
|
|
|
136
|
+ }
|
|
|
137
|
+
|
|
|
138
|
+ return element;
|
|
|
139
|
+ });
|
|
55
|
140
|
}
|
|
56
|
141
|
}
|
|
57
|
142
|
|