forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelperText.tsx
209 lines (196 loc) · 4.56 KB
/
HelperText.tsx
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
import * as React from 'react';
import color from 'color';
import {
Animated,
StyleSheet,
StyleProp,
TextStyle,
LayoutChangeEvent,
} from 'react-native';
import AnimatedText from './Typography/AnimatedText';
import { withTheme } from '../core/theming';
import { Theme, $Omit } from '../types';
type Props = $Omit<
$Omit<React.ComponentPropsWithRef<typeof AnimatedText>, 'padding'>,
'type'
> & {
/**
* Type of the helper text.
*/
type: 'error' | 'info';
/**
* Whether to display the helper text.
*/
visible?: boolean;
/**
* Whether to apply padding to the helper text.
*/
padding?: 'none' | 'normal';
/**
* Text content of the HelperText.
*/
children: React.ReactNode;
style?: StyleProp<TextStyle>;
/**
* @optional
*/
theme: Theme;
/**
* TestID used for testing purposes
*/
testID?: string;
};
type State = {
shown: Animated.Value;
textHeight: number;
};
/**
* Helper text is used in conjuction with input elements to provide additional hints for the user.
*
* <div class="screenshots">
* <img class="medium" src="screenshots/helper-text.gif" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { HelperText, TextInput } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* text: ''
* };
*
* _onChangeText = text => this.setState({ text });
*
* _hasErrors = () => {
* return !this.state.text.includes('@');
* }
*
* render(){
* const { text } = this.state;
*
* return (
* <View>
* <TextInput
* label="Email"
* value={text}
* onChangeText={this._onChangeText}
* />
* <HelperText
* type="error"
* visible={this._hasErrors()}
* >
* Email address is invalid!
* </HelperText>
* </View>
* );
* }
* }
* ```
*/
class HelperText extends React.PureComponent<Props, State> {
static defaultProps: Partial<Props> = {
type: 'info',
padding: 'normal',
visible: true,
};
state = {
shown: new Animated.Value(this.props.visible ? 1 : 0),
textHeight: 0,
};
componentDidUpdate(prevProps: Props, prevState: State) {
if (
prevProps.visible !== this.props.visible ||
prevState.textHeight !== this.state.textHeight
) {
if (this.props.visible) {
this.showText();
} else {
this.hideText();
}
}
}
private showText = () => {
const { scale } = this.props.theme.animation;
Animated.timing(this.state.shown, {
toValue: 1,
duration: 150 * scale,
useNativeDriver: true,
}).start();
};
private hideText = () => {
const { scale } = this.props.theme.animation;
Animated.timing(this.state.shown, {
toValue: 0,
duration: 180 * scale,
useNativeDriver: true,
}).start();
};
private handleTextLayout = (e: LayoutChangeEvent) => {
//@ts-ignore Animated.Text typings are improved but something is still broken. It thinks onLayout is not callable.
this.props.onLayout && this.props.onLayout(e);
this.setState({
textHeight: e.nativeEvent.layout.height,
});
};
render() {
const {
style,
type,
visible,
theme,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onLayout,
padding,
...rest
} = this.props;
const { colors, dark } = theme;
const textColor =
this.props.type === 'error'
? colors.error
: color(colors.text)
.alpha(dark ? 0.7 : 0.54)
.rgb()
.string();
return (
<AnimatedText
onLayout={this.handleTextLayout}
style={[
styles.text,
padding !== 'none' ? styles.padding : {},
{
color: textColor,
opacity: this.state.shown,
transform:
visible && type === 'error'
? [
{
translateY: this.state.shown.interpolate({
inputRange: [0, 1],
outputRange: [-this.state.textHeight / 2, 0],
}),
},
]
: [],
},
style,
]}
{...rest}
>
{this.props.children}
</AnimatedText>
);
}
}
const styles = StyleSheet.create({
text: {
fontSize: 12,
paddingVertical: 4,
},
padding: {
paddingHorizontal: 12,
},
});
export default withTheme(HelperText);