1 | import * as React from 'react';
|
2 | import color from 'color';
|
3 | import {
|
4 | Animated,
|
5 | StyleSheet,
|
6 | StyleProp,
|
7 | TextStyle,
|
8 | LayoutChangeEvent,
|
9 | } from 'react-native';
|
10 | import AnimatedText from './Typography/AnimatedText';
|
11 | import { withTheme } from '../core/theming';
|
12 | import type { $Omit } from '../types';
|
13 |
|
14 | type Props = $Omit<
|
15 | $Omit<React.ComponentPropsWithRef<typeof AnimatedText>, 'padding'>,
|
16 | 'type'
|
17 | > & {
|
18 | |
19 |
|
20 |
|
21 | type: 'error' | 'info';
|
22 | |
23 |
|
24 |
|
25 | visible?: boolean;
|
26 | |
27 |
|
28 |
|
29 | padding?: 'none' | 'normal';
|
30 | |
31 |
|
32 |
|
33 | children: React.ReactNode;
|
34 | style?: StyleProp<TextStyle>;
|
35 | |
36 |
|
37 |
|
38 | theme: ReactNativePaper.Theme;
|
39 | |
40 |
|
41 |
|
42 | testID?: string;
|
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 | const HelperText = ({
|
81 | style,
|
82 | type = 'info',
|
83 | visible = true,
|
84 | theme,
|
85 | onLayout,
|
86 | padding = 'normal',
|
87 | ...rest
|
88 | }: Props) => {
|
89 | const { current: shown } = React.useRef<Animated.Value>(
|
90 | new Animated.Value(visible ? 1 : 0)
|
91 | );
|
92 |
|
93 | let { current: textHeight } = React.useRef<number>(0);
|
94 |
|
95 | const { scale } = theme.animation;
|
96 |
|
97 | React.useEffect(() => {
|
98 | if (visible) {
|
99 |
|
100 | Animated.timing(shown, {
|
101 | toValue: 1,
|
102 | duration: 150 * scale,
|
103 | useNativeDriver: true,
|
104 | }).start();
|
105 | } else {
|
106 |
|
107 | Animated.timing(shown, {
|
108 | toValue: 0,
|
109 | duration: 180 * scale,
|
110 | useNativeDriver: true,
|
111 | }).start();
|
112 | }
|
113 | }, [visible, scale, shown]);
|
114 |
|
115 | const handleTextLayout = (e: LayoutChangeEvent) => {
|
116 | onLayout?.(e);
|
117 | textHeight = e.nativeEvent.layout.height;
|
118 | };
|
119 |
|
120 | const { colors, dark } = theme;
|
121 |
|
122 | const textColor =
|
123 | type === 'error'
|
124 | ? colors.error
|
125 | : color(colors.text)
|
126 | .alpha(dark ? 0.7 : 0.54)
|
127 | .rgb()
|
128 | .string();
|
129 |
|
130 | return (
|
131 | <AnimatedText
|
132 | onLayout={handleTextLayout}
|
133 | style={[
|
134 | styles.text,
|
135 | padding !== 'none' ? styles.padding : {},
|
136 | {
|
137 | color: textColor,
|
138 | opacity: shown,
|
139 | transform:
|
140 | visible && type === 'error'
|
141 | ? [
|
142 | {
|
143 | translateY: shown.interpolate({
|
144 | inputRange: [0, 1],
|
145 | outputRange: [-textHeight / 2, 0],
|
146 | }),
|
147 | },
|
148 | ]
|
149 | : [],
|
150 | },
|
151 | style,
|
152 | ]}
|
153 | {...rest}
|
154 | >
|
155 | {rest.children}
|
156 | </AnimatedText>
|
157 | );
|
158 | };
|
159 |
|
160 | const styles = StyleSheet.create({
|
161 | text: {
|
162 | fontSize: 12,
|
163 | paddingVertical: 4,
|
164 | },
|
165 | padding: {
|
166 | paddingHorizontal: 12,
|
167 | },
|
168 | });
|
169 |
|
170 | export default withTheme(HelperText);
|