forked from zulip/zulip-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.js
100 lines (90 loc) · 2.76 KB
/
Input.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
/* @flow strict-local */
import React, { PureComponent } from 'react';
import { TextInput, Platform } from 'react-native';
import { FormattedMessage } from 'react-intl';
import type { LocalizableText } from '../types';
import type { ThemeData } from '../styles';
import { ThemeContext, HALF_COLOR, BORDER_COLOR } from '../styles';
export type Props = $ReadOnly<{|
// See point 2 at
// https://github.com/facebook/react-native/issues/28459#issuecomment-609957836
// $FlowFixMe
...$PropertyType<typeof TextInput, 'props'>,
placeholder: LocalizableText,
onChangeText?: (text: string) => void,
textInputRef?: React$Ref<typeof TextInput>,
|}>;
type State = {|
isFocused: boolean,
|};
/**
* A light abstraction over the standard TextInput component
* that allows us to seamlessly provide internationalization
* capabilities and also style the component depending on
* the platform the app is running on.
*
* @prop [style] - Can override our default style for inputs.
* @prop placeholder - Translated before passing to TextInput as
* a prop of the same name.
* @prop [textInputRef] - Passed to TextInput in `ref`. See upstream docs
* on refs: https://reactjs.org/docs/refs-and-the-dom.html
* @prop ...all other TextInput props - Passed through verbatim to TextInput.
* See upstream: https://reactnative.dev/docs/textinput
*/
export default class Input extends PureComponent<Props, State> {
static contextType = ThemeContext;
context: ThemeData;
styles = {
input: {
...Platform.select({
ios: {
borderWidth: 1,
borderColor: BORDER_COLOR,
borderRadius: 2,
padding: 8,
},
}),
},
};
state = {
isFocused: false,
};
handleFocus = () => {
this.setState({
isFocused: true,
});
};
handleBlur = () => {
this.setState({
isFocused: false,
});
};
render() {
const { style, placeholder, textInputRef, ...restProps } = this.props;
const { isFocused } = this.state;
const fullPlaceholder =
typeof placeholder === 'object' /* force linebreak */
? placeholder
: { text: placeholder, values: undefined };
return (
<FormattedMessage
id={fullPlaceholder.text}
defaultMessage={fullPlaceholder.text}
values={fullPlaceholder.values}
>
{(text: string) => (
<TextInput
style={[this.styles.input, { color: this.context.color }, style]}
placeholder={text}
placeholderTextColor={HALF_COLOR}
underlineColorAndroid={isFocused ? BORDER_COLOR : HALF_COLOR}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
ref={textInputRef}
{...restProps}
/>
)}
</FormattedMessage>
);
}
}