|
@@ -1,12 +1,31 @@
|
1
|
1
|
import PropTypes from 'prop-types';
|
2
|
2
|
import React from 'react';
|
3
|
|
-import { TextInput } from 'react-native';
|
|
3
|
+import { StyleSheet, TextInput } from 'react-native';
|
4
|
4
|
import Prompt from 'react-native-prompt';
|
5
|
5
|
import { connect } from 'react-redux';
|
6
|
6
|
|
7
|
7
|
import { translate } from '../../i18n';
|
|
8
|
+import { LoadingIndicator } from '../../react';
|
|
9
|
+import { set } from '../../redux';
|
8
|
10
|
|
9
|
11
|
import AbstractDialog from './AbstractDialog';
|
|
12
|
+import styles from './styles';
|
|
13
|
+
|
|
14
|
+/**
|
|
15
|
+ * The value of the style property {@link _TAG_KEY} which identifies the
|
|
16
|
+ * OK/submit button of <tt>Prompt</tt>.
|
|
17
|
+ */
|
|
18
|
+const _SUBMIT_TEXT_TAG_VALUE = '_SUBMIT_TEXT_TAG_VALUE';
|
|
19
|
+
|
|
20
|
+/**
|
|
21
|
+ * The name of the style property which identifies ancestors of <tt>Prompt</tt>
|
|
22
|
+ * such as its OK/submit button for the purposes of workarounds implemented by
|
|
23
|
+ * <tt>Dialog</tt>.
|
|
24
|
+ *
|
|
25
|
+ * XXX The value may trigger a react-native warning in the Debug configuration
|
|
26
|
+ * but, unfortunately, I couldn't find a value that wouldn't.
|
|
27
|
+ */
|
|
28
|
+const _TAG_KEY = '_TAG_KEY';
|
10
|
29
|
|
11
|
30
|
/**
|
12
|
31
|
* Implements <tt>AbstractDialog</tt> on react-native using <tt>Prompt</tt>.
|
|
@@ -37,7 +56,6 @@ class Dialog extends AbstractDialog {
|
37
|
56
|
bodyKey,
|
38
|
57
|
cancelDisabled,
|
39
|
58
|
cancelTitleKey = 'dialog.Cancel',
|
40
|
|
- children,
|
41
|
59
|
okDisabled,
|
42
|
60
|
okTitleKey = 'dialog.Ok',
|
43
|
61
|
t,
|
|
@@ -45,30 +63,82 @@ class Dialog extends AbstractDialog {
|
45
|
63
|
titleString
|
46
|
64
|
} = this.props;
|
47
|
65
|
|
48
|
|
- /* eslint-disable react/jsx-wrap-multilines */
|
49
|
|
-
|
50
|
|
- let element
|
51
|
|
- = <Prompt
|
52
|
|
- cancelText = { cancelDisabled ? undefined : t(cancelTitleKey) }
|
|
66
|
+ const cancelButtonTextStyle
|
|
67
|
+ = cancelDisabled ? styles.disabledButtonText : styles.buttonText;
|
|
68
|
+ let submitButtonTextStyle
|
|
69
|
+ = okDisabled ? styles.disabledButtonText : styles.buttonText;
|
|
70
|
+
|
|
71
|
+ submitButtonTextStyle = {
|
|
72
|
+ ...submitButtonTextStyle,
|
|
73
|
+ [_TAG_KEY]: _SUBMIT_TEXT_TAG_VALUE
|
|
74
|
+ };
|
|
75
|
+
|
|
76
|
+ // eslint-disable-next-line no-extra-parens
|
|
77
|
+ let element = (
|
|
78
|
+ <Prompt
|
|
79
|
+ cancelButtonTextStyle = { cancelButtonTextStyle }
|
|
80
|
+ cancelText = { t(cancelTitleKey) }
|
53
|
81
|
onCancel = { this._onCancel }
|
54
|
82
|
onSubmit = { this._onSubmit }
|
55
|
83
|
placeholder = { t(bodyKey) }
|
56
|
|
- submitText = { okDisabled ? undefined : t(okTitleKey) }
|
|
84
|
+ submitButtonTextStyle = { submitButtonTextStyle }
|
|
85
|
+ submitText = { t(okTitleKey) }
|
57
|
86
|
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
|
|
- }
|
|
87
|
+ visible = { true } />
|
|
88
|
+ );
|
|
89
|
+
|
|
90
|
+ // XXX The following implements workarounds with knowledge of
|
|
91
|
+ // react-native-prompt/Prompt's implementation.
|
|
92
|
+
|
|
93
|
+ // eslint-disable-next-line no-extra-parens, new-cap
|
|
94
|
+ element = (new (element.type)(element.props)).render();
|
|
95
|
+
|
|
96
|
+ let { children } = this.props;
|
|
97
|
+
|
|
98
|
+ children = React.Children.count(children) ? children : undefined;
|
|
99
|
+
|
|
100
|
+ // eslint-disable-next-line no-shadow
|
|
101
|
+ element = this._mapReactElement(element, element => {
|
|
102
|
+ // * If this Dialog has children, they are to be rendered instead of
|
|
103
|
+ // Prompt's TextInput.
|
|
104
|
+ if (element.type === TextInput) {
|
|
105
|
+ if (children) {
|
|
106
|
+ element = children; // eslint-disable-line no-param-reassign
|
|
107
|
+ children = undefined;
|
|
108
|
+ }
|
|
109
|
+ } else {
|
|
110
|
+ let { style } = element.props;
|
|
111
|
+
|
|
112
|
+ if (style
|
|
113
|
+ && (style = StyleSheet.flatten(style))
|
|
114
|
+ && _TAG_KEY in style) {
|
|
115
|
+ switch (style[_TAG_KEY]) {
|
|
116
|
+ case _SUBMIT_TEXT_TAG_VALUE:
|
|
117
|
+ if (this.state.submitting) {
|
|
118
|
+ // * If this Dialog is submitting, render a
|
|
119
|
+ // LoadingIndicator.
|
|
120
|
+ return (
|
|
121
|
+ <LoadingIndicator
|
|
122
|
+ color = { submitButtonTextStyle.color }
|
|
123
|
+ size = { 'small' } />
|
|
124
|
+ );
|
|
125
|
+ }
|
|
126
|
+ break;
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ // eslint-disable-next-line no-param-reassign
|
|
130
|
+ element
|
|
131
|
+ = React.cloneElement(
|
|
132
|
+ element,
|
|
133
|
+ /* props */ {
|
|
134
|
+ style: set(style, _TAG_KEY, undefined)
|
|
135
|
+ },
|
|
136
|
+ ...React.Children.toArray(element.props.children));
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ return element;
|
|
141
|
+ });
|
72
|
142
|
|
73
|
143
|
return element;
|
74
|
144
|
}
|
|
@@ -108,36 +178,6 @@ class Dialog extends AbstractDialog {
|
108
|
178
|
|
109
|
179
|
return mapped;
|
110
|
180
|
}
|
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
|
|
- });
|
140
|
|
- }
|
141
|
181
|
}
|
142
|
182
|
|
143
|
183
|
export default translate(connect()(Dialog));
|