Skip to content

Commit d612ef2

Browse files
author
Chris Bobbe
committed
SmartUrlInput: Switch to new React Context API for theme colors.
(Commit message largely copied from that of a606bf3.) Per zulip#1946, we're moving to the new React Context API (https://reactjs.org/docs/context.html). We’re also isolating the theme colors in their own context type, to be accessed from there instead of the previously used `context.styles` object. So, components that consume the theme colors can and should use `static contextType = ThemeContext;` and, e.g., this.context.backgroundColor, instead of this.context.styles.backgroundColor, as was done before.
1 parent d66fb7c commit d612ef2

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/common/SmartUrlInput.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { StyleSheet, TextInput, TouchableWithoutFeedback, View } from 'react-nat
44
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
55
import type { NavigationEventSubscription, NavigationScreenProp } from 'react-navigation';
66

7-
import type { Context } from '../types';
7+
import type { ThemeColors } from '../styles';
8+
import { ThemeContext } from '../styles';
89
import { autocompleteRealmPieces, autocompleteRealm, fixRealmUrl } from '../utils/url';
910
import type { Protocol } from '../utils/url';
1011
import RawLabel from './RawLabel';
@@ -60,17 +61,14 @@ type State = {|
6061
|};
6162

6263
export default class SmartUrlInput extends PureComponent<Props, State> {
63-
context: Context;
64+
static contextType = ThemeContext;
65+
context: ThemeColors;
6466
state = {
6567
value: '',
6668
};
6769
textInputRef: ?TextInput;
6870
focusListener: void | NavigationEventSubscription;
6971

70-
static contextTypes = {
71-
styles: () => null,
72-
};
73-
7472
componentDidMount() {
7573
this.focusListener = this.props.navigation.addListener('didFocus', () => {
7674
if (this.textInputRef) {
@@ -103,14 +101,13 @@ export default class SmartUrlInput extends PureComponent<Props, State> {
103101
renderPlaceholderPart = (text: string) => (
104102
<TouchableWithoutFeedback onPress={this.urlPress}>
105103
<RawLabel
106-
style={[styles.realmInput, this.context.styles.color, styles.realmPlaceholder]}
104+
style={[styles.realmInput, { color: this.context.color }, styles.realmPlaceholder]}
107105
text={text}
108106
/>
109107
</TouchableWithoutFeedback>
110108
);
111109

112110
render() {
113-
const { styles: contextStyles } = this.context;
114111
const {
115112
defaultValue,
116113
defaultProtocol,
@@ -133,7 +130,7 @@ export default class SmartUrlInput extends PureComponent<Props, State> {
133130
<TextInput
134131
style={[
135132
styles.realmInput,
136-
contextStyles.color,
133+
{ color: this.context.color },
137134
value.length === 0 && styles.realmInputEmpty,
138135
]}
139136
autoFocus

0 commit comments

Comments
 (0)