-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
BaseExpensiTextInput.js
220 lines (198 loc) · 7.47 KB
/
BaseExpensiTextInput.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import _ from 'underscore';
import React, {Component} from 'react';
import {
Animated, TextInput, View, TouchableWithoutFeedback,
} from 'react-native';
import Str from 'expensify-common/lib/str';
import ExpensiTextInputLabel from './ExpensiTextInputLabel';
import {propTypes, defaultProps} from './baseExpensiTextInputPropTypes';
import themeColors from '../../styles/themes/default';
import styles from '../../styles/styles';
import InlineErrorText from '../InlineErrorText';
const ACTIVE_LABEL_TRANSLATE_Y = -12;
const ACTIVE_LABEL_TRANSLATE_X = (translateX = -22) => translateX;
const ACTIVE_LABEL_SCALE = 0.8668;
const INACTIVE_LABEL_TRANSLATE_Y = 0;
const INACTIVE_LABEL_TRANSLATE_X = 0;
const INACTIVE_LABEL_SCALE = 1;
class BaseExpensiTextInput extends Component {
constructor(props) {
super(props);
this.value = props.value || props.defaultValue || '';
const activeLabel = props.forceActiveLabel || this.value.length > 0;
this.state = {
isFocused: false,
labelTranslateY: new Animated.Value(activeLabel ? ACTIVE_LABEL_TRANSLATE_Y : INACTIVE_LABEL_TRANSLATE_Y),
labelTranslateX: new Animated.Value(activeLabel
? ACTIVE_LABEL_TRANSLATE_X(props.translateX) : INACTIVE_LABEL_TRANSLATE_X),
labelScale: new Animated.Value(activeLabel ? ACTIVE_LABEL_SCALE : INACTIVE_LABEL_SCALE),
};
this.input = null;
this.isLabelActive = activeLabel;
this.onPress = this.onPress.bind(this);
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onBlur.bind(this);
this.setValue = this.setValue.bind(this);
}
componentDidMount() {
// We are manually managing focus to prevent this issue: https://github.com/Expensify/App/issues/4514
if (this.props.autoFocus && this.input) {
this.input.focus();
}
}
componentDidUpdate(prevProps) {
// activate or deactivate the label when value is changed programmatically from outside
if (prevProps.value !== this.props.value) {
this.value = this.props.value;
if (this.props.value) {
this.activateLabel();
} else {
this.deactivateLabel();
}
}
}
onPress(event) {
if (this.props.disabled) {
return;
}
if (this.props.onPress) {
this.props.onPress(event);
}
if (!event.isDefaultPrevented()) {
this.input.focus();
}
}
onFocus() {
if (this.props.onFocus) { this.props.onFocus(); }
this.setState({isFocused: true});
this.activateLabel();
}
onBlur() {
if (this.props.onBlur) { this.props.onBlur(); }
this.setState({isFocused: false});
this.deactivateLabel();
}
/**
* Set Value & activateLabel
*
* @param {String} value
* @memberof BaseExpensiTextInput
*/
setValue(value) {
this.value = value;
Str.result(this.props.onChangeText, value);
this.activateLabel();
}
activateLabel() {
if (this.value.length >= 0 && !this.isLabelActive) {
this.animateLabel(
ACTIVE_LABEL_TRANSLATE_Y,
ACTIVE_LABEL_TRANSLATE_X(this.props.translateX),
ACTIVE_LABEL_SCALE,
);
this.isLabelActive = true;
}
}
deactivateLabel() {
if (!this.props.forceActiveLabel && this.value.length === 0) {
this.animateLabel(INACTIVE_LABEL_TRANSLATE_Y, INACTIVE_LABEL_TRANSLATE_X, INACTIVE_LABEL_SCALE);
this.isLabelActive = false;
}
}
animateLabel(translateY, translateX, scale) {
Animated.parallel([
Animated.spring(this.state.labelTranslateY, {
toValue: translateY,
duration: 80,
useNativeDriver: true,
}),
Animated.spring(this.state.labelTranslateX, {
toValue: translateX,
duration: 80,
useNativeDriver: true,
}),
Animated.spring(this.state.labelScale, {
toValue: scale,
duration: 80,
useNativeDriver: true,
}),
]).start();
}
render() {
const {
label,
value,
placeholder,
errorText,
hasError,
containerStyles,
inputStyle,
ignoreLabelTranslateX,
innerRef,
autoFocus,
multiline,
...inputProps
} = this.props;
const hasLabel = Boolean(label.length);
return (
<View>
<View
style={[
!multiline && styles.componentHeightLarge,
...containerStyles,
]}
>
<TouchableWithoutFeedback onPress={this.onPress} focusable={false}>
<View
style={[
styles.expensiTextInputContainer,
!hasLabel && styles.pv0,
this.state.isFocused && styles.borderColorFocus,
(hasError || errorText) && styles.borderColorDanger,
]}
>
{hasLabel ? (
<ExpensiTextInputLabel
label={label}
labelTranslateX={
ignoreLabelTranslateX
? new Animated.Value(0)
: this.state.labelTranslateX
}
labelTranslateY={this.state.labelTranslateY}
labelScale={this.state.labelScale}
/>
) : null}
<TextInput
ref={(ref) => {
if (typeof innerRef === 'function') { innerRef(ref); }
this.input = ref;
}}
// eslint-disable-next-line
{...inputProps}
value={value}
placeholder={(this.state.isFocused || !label) ? placeholder : null}
placeholderTextColor={themeColors.placeholderText}
underlineColorAndroid="transparent"
style={inputStyle}
multiline={multiline}
onFocus={this.onFocus}
onBlur={this.onBlur}
onChangeText={this.setValue}
onPressOut={this.props.onPress}
/>
</View>
</TouchableWithoutFeedback>
</View>
{!_.isEmpty(errorText) && (
<InlineErrorText>
{errorText}
</InlineErrorText>
)}
</View>
);
}
}
BaseExpensiTextInput.propTypes = propTypes;
BaseExpensiTextInput.defaultProps = defaultProps;
export default BaseExpensiTextInput;