forked from zulip/zulip-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartUrlInput.js
165 lines (151 loc) · 4.65 KB
/
SmartUrlInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* @flow strict-local */
import React, { PureComponent } from 'react';
import { TextInput, TouchableWithoutFeedback, View } from 'react-native';
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
import type { NavigationEventSubscription, NavigationScreenProp } from 'react-navigation';
import type { ThemeData } from '../styles';
import { ThemeContext, createStyleSheet } from '../styles';
import { autocompleteRealmPieces, autocompleteRealm, fixRealmUrl } from '../utils/url';
import type { Protocol } from '../utils/url';
import RawLabel from './RawLabel';
const styles = createStyleSheet({
wrapper: {
flexDirection: 'row',
opacity: 0.8,
},
realmInput: {
padding: 0,
fontSize: 20,
},
realmPlaceholder: {
opacity: 0.75,
},
realmInputEmpty: {
width: 1,
},
});
type Props = $ReadOnly<{|
defaultValue: string,
/**
* The protocol which will be used if the user doesn't specify one.
* Should almost certainly be "https://".
*/
defaultProtocol: Protocol,
/**
* The example organization name that will be displayed while the
* entry field is empty. Appears, briefly, as the initial (lowest-
* level) component of the realm's domain.
*/
defaultOrganization: string,
/**
* The default domain to which the user's input will be appended, if
* it appears not to contain an explicit domain.
*/
defaultDomain: string,
navigation: NavigationScreenProp<mixed>,
style?: ViewStyleProp,
onChangeText: (value: string) => void,
onSubmitEditing: () => Promise<void>,
enablesReturnKeyAutomatically: boolean,
|}>;
type State = {|
/**
* The actual input string, exactly as entered by the user,
* without modifications by autocomplete.
*/
value: string,
|};
export default class SmartUrlInput extends PureComponent<Props, State> {
static contextType = ThemeContext;
context: ThemeData;
state = {
value: '',
};
textInputRef = React.createRef<typeof TextInput>();
focusListener: void | NavigationEventSubscription;
componentDidMount() {
this.focusListener = this.props.navigation.addListener('didFocus', () => {
if (this.textInputRef.current) {
// See point 2 at
// https://github.com/facebook/react-native/issues/28459#issuecomment-609957836
// $FlowFixMe
this.textInputRef.current.focus();
}
});
}
componentWillUnmount() {
if (this.focusListener) {
this.focusListener.remove();
}
}
handleChange = (value: string) => {
this.setState({ value });
const { onChangeText, defaultProtocol: protocol, defaultDomain: domain } = this.props;
onChangeText(fixRealmUrl(autocompleteRealm(value, { protocol, domain })));
};
urlPress = () => {
const { textInputRef } = this;
if (textInputRef.current) {
// See point 2 at
// https://github.com/facebook/react-native/issues/28459#issuecomment-609957836
// $FlowFixMe
textInputRef.current.blur();
setTimeout(() => {
if (textInputRef.current) {
// $FlowFixMe - same as above
textInputRef.current.focus();
}
}, 100);
}
};
renderPlaceholderPart = (text: string) => (
<TouchableWithoutFeedback onPress={this.urlPress}>
<RawLabel
style={[styles.realmInput, { color: this.context.color }, styles.realmPlaceholder]}
text={text}
/>
</TouchableWithoutFeedback>
);
render() {
const {
defaultValue,
defaultProtocol,
defaultOrganization,
defaultDomain,
style,
onSubmitEditing,
enablesReturnKeyAutomatically,
} = this.props;
const { value } = this.state;
const [prefix, , suffix] = autocompleteRealmPieces(value || defaultValue, {
domain: defaultDomain,
protocol: defaultProtocol,
});
return (
<View style={[styles.wrapper, style]}>
{prefix !== null && this.renderPlaceholderPart(prefix)}
<TextInput
style={[
styles.realmInput,
{ color: this.context.color },
value.length === 0 && styles.realmInputEmpty,
]}
autoFocus
autoCorrect={false}
autoCapitalize="none"
returnKeyType="go"
defaultValue={defaultValue}
onChangeText={this.handleChange}
blurOnSubmit={false}
keyboardType="url"
underlineColorAndroid="transparent"
onSubmitEditing={onSubmitEditing}
enablesReturnKeyAutomatically={enablesReturnKeyAutomatically}
ref={this.textInputRef}
/>
{!value && this.renderPlaceholderPart(defaultOrganization)}
{suffix !== null && this.renderPlaceholderPart(suffix)}
</View>
);
}
}