-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathTextInput.js
More file actions
120 lines (102 loc) · 2.99 KB
/
Copy pathTextInput.js
File metadata and controls
120 lines (102 loc) · 2.99 KB
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
/* eslint-disable react/no-multi-comp */
import React, { forwardRef, PureComponent } from 'react';
import { Platform, TextInput as RNTextInput } from 'react-native';
import autoBindReact from 'auto-bind/react';
import { TextInputPropTypes } from 'deprecated-react-native-prop-types';
import PropTypes from 'prop-types';
import { connectAnimation, Wiggle } from '@shoutem/animation';
import { connectStyle } from '@shoutem/theme';
import { Caption } from './Text';
import { View } from './View';
class TextInput extends PureComponent {
constructor(props) {
super(props);
autoBindReact(this);
this.state = {
isFocused: props.highlightOnFocus && props.autoFocus,
};
}
handleBlur() {
const { onBlur } = this.props;
if (onBlur) {
onBlur();
}
this.setState({ isFocused: false });
}
handleFocus() {
const { onFocus } = this.props;
if (onFocus) {
onFocus();
}
this.setState({ isFocused: true });
}
render() {
const {
animate,
errorMessage,
highlightOnFocus,
style,
forwardedRef,
...otherProps
} = this.props;
const { isFocused } = this.state;
const {
errorBorderColor,
placeholderTextColor,
selectionColor,
underlineColorAndroid,
withBorder,
withoutBorder,
wiggleAnimation,
errorText,
height,
...otherStyle
} = style;
const containerStyle = Platform.OS === 'web' ? { height } : undefined;
const hasBorder = (isFocused && highlightOnFocus) || !!errorMessage;
const startErrorAnimation = animate && !!errorMessage;
return (
<View style={containerStyle}>
<Wiggle style={wiggleAnimation} startAnimation={startErrorAnimation}>
<RNTextInput
{...otherProps}
onBlur={this.handleBlur}
onFocus={this.handleFocus}
placeholderTextColor={placeholderTextColor}
selectionColor={selectionColor}
underlineColorAndroid={underlineColorAndroid}
style={{
...(hasBorder ? withBorder : withoutBorder),
...otherStyle,
...(errorMessage ? errorBorderColor : {}),
height,
}}
ref={forwardedRef}
/>
</Wiggle>
{!!errorMessage && (
<Caption styleName="form-error sm-gutter-top" style={errorText}>
{errorMessage}
</Caption>
)}
</View>
);
}
}
TextInput.propTypes = {
...TextInputPropTypes,
style: PropTypes.object.isRequired,
animate: PropTypes.bool,
errorMessage: PropTypes.string,
};
TextInput.defaultProps = {
animate: true,
errorMessage: undefined,
};
const AnimatedTextInput = connectAnimation(TextInput);
const StyledTextInput = connectStyle('shoutem.ui.TextInput')(AnimatedTextInput);
const ForwardedTextInput = forwardRef((props, ref) => (
<StyledTextInput {...props} forwardedRef={ref} />
));
ForwardedTextInput.displayName = 'TextInput';
export { ForwardedTextInput as TextInput };