Skip to content

Commit 8242962

Browse files
Chris Bobbechrisbobbe
Chris Bobbe
authored andcommitted
Input: Switch to new React Context API for theme colors.
Per zulip#1946, we're moving to the new React Context API (https://reactjs.org/docs/context.html). Use the same strategy as in a recent commit, for ChatScreen: put the constant parts of the style in an instance field, and compute the theme-dependent parts in the `render` method.
1 parent 82f10f3 commit 8242962

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/common/Input.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* @flow strict-local */
22
import React, { PureComponent } from 'react';
3-
import { TextInput } from 'react-native';
3+
import { TextInput, Platform } from 'react-native';
44
import { FormattedMessage } from 'react-intl';
55

6-
import type { Context, LocalizableText } from '../types';
7-
import { HALF_COLOR, BORDER_COLOR } from '../styles';
6+
import type { LocalizableText } from '../types';
7+
import type { ThemeColors } from '../styles';
8+
import { ThemeContext, HALF_COLOR, BORDER_COLOR } from '../styles';
89

910
export type Props = $ReadOnly<{|
1011
...$PropertyType<TextInput, 'props'>,
@@ -32,16 +33,27 @@ type State = {|
3233
* See upstream: https://reactnative.dev/docs/textinput
3334
*/
3435
export default class Input extends PureComponent<Props, State> {
35-
context: Context;
36+
static contextType = ThemeContext;
37+
context: ThemeColors;
38+
39+
styles = {
40+
input: {
41+
...Platform.select({
42+
ios: {
43+
borderWidth: 1,
44+
borderColor: BORDER_COLOR,
45+
borderRadius: 2,
46+
padding: 8,
47+
},
48+
}),
49+
},
50+
};
51+
3652
state = {
3753
isFocused: false,
3854
};
3955
textInput: ?TextInput;
4056

41-
static contextTypes = {
42-
styles: () => null,
43-
};
44-
4557
handleClear = () => {
4658
const { onChangeText } = this.props;
4759
if (onChangeText) {
@@ -65,7 +77,6 @@ export default class Input extends PureComponent<Props, State> {
6577
};
6678

6779
render() {
68-
const { styles: contextStyles } = this.context;
6980
const { style, placeholder, textInputRef, ...restProps } = this.props;
7081
const { isFocused } = this.state;
7182
const fullPlaceholder =
@@ -81,7 +92,7 @@ export default class Input extends PureComponent<Props, State> {
8192
>
8293
{(text: string) => (
8394
<TextInput
84-
style={[contextStyles.input, style]}
95+
style={[this.styles.input, { color: this.context.color }, style]}
8596
placeholder={text}
8697
placeholderTextColor={HALF_COLOR}
8798
underlineColorAndroid={isFocused ? BORDER_COLOR : HALF_COLOR}

0 commit comments

Comments
 (0)