-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollapseCard.tsx
212 lines (192 loc) · 5.05 KB
/
CollapseCard.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
210
211
212
import React, {useState} from 'react';
import {
Dimensions,
Image,
Pressable,
StyleSheet,
Text,
View,
} from 'react-native';
import Animated, {
interpolate,
useAnimatedStyle,
useSharedValue,
withSpring,
withTiming,
} from 'react-native-reanimated';
import {listData} from './data';
import Feather from 'react-native-vector-icons/Feather';
export const {width: SCREEN_WIDTH, height: SCREEN_HEIGHT} =
Dimensions.get('window');
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
interface RotateIconType {}
export const CollapseCard: React.FC<RotateIconType> = () => {
const expand = useSharedValue(1);
const opacity = useSharedValue(0);
const [isExpanded, setIsExpanded] = useState(true);
const onToggle = () => {
expand.value = expand.value === 0 ? 1 : 0;
setIsExpanded(prev => !prev);
};
const animatedOpacity = useAnimatedStyle(() => {
return {
opacity: opacity.value,
};
});
const iconStyle = useAnimatedStyle(() => {
const rotation = interpolate(expand.value, [0, 1], [0, 90]);
return {
transform: [{rotate: `${rotation}deg`}],
};
});
const expandButtonAnimatedStyle = useAnimatedStyle(() => {
return {
width: expand.value === 1 ? SCREEN_WIDTH * 0.33 : SCREEN_WIDTH * 0.3,
};
});
const collapseContentStyle = useAnimatedStyle(() => {
// TODO: move to dynamic height
return {
height: withTiming(expand.value === 1 ? 100 : 0, {
duration: 320,
}),
opacity: withSpring(expand.value),
overflow: 'hidden',
transform: [{scale: withSpring(expand.value === 1 ? 1 : 0.95)}],
marginBottom: withTiming(expand.value ? 12 : 0, {duration: 200}),
};
});
return (
<Animated.ScrollView>
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerTitle}>Transactions</Text>
<AnimatedPressable
style={[styles.collapseButton, expandButtonAnimatedStyle]}
onPress={onToggle}>
<Text style={styles.collapseText}>
{isExpanded ? 'Collapse' : 'Expand'}
</Text>
<Animated.View style={[iconStyle]}>
<Feather name="chevron-right" size={24} color="black" />
</Animated.View>
</AnimatedPressable>
</View>
<Animated.View style={[collapseContentStyle, animatedOpacity]}>
<Text>
Lorem ipsum dolor sit amet consectetur adipisicing elit. In,
nesciunt est provident dolorum excepturi incidunt, aperiam tempora
quos, nobis nihil non temporibus quisquam minus? Eos asperiores
delectus vel sed in.
</Text>
</Animated.View>
</View>
<View style={{paddingHorizontal: 22}}>
{listData.map(item => {
return <ListItem item={item} key={item.id} />;
})}
</View>
</Animated.ScrollView>
);
};
interface ListItemProps {
item: any;
}
export const ListItem: React.FC<ListItemProps> = ({item}) => {
return (
<View style={[styles.itemContainer]}>
<View style={styles.itemLeftContainer}>
<Image style={styles.itemImage} source={item.image} />
<View style={styles.itemTextContainer}>
<Text style={styles.itemTitle}>{item.title}</Text>
<Text style={{color: 'gray'}}>{item.subTitle}</Text>
</View>
</View>
<View>
<Text
style={[
styles.itemPrice,
{color: item.price > 0 ? 'green' : 'black'},
]}>
{item.price > 0 ? '+' : ''}
{item.price}€
</Text>
<Text style={styles.itemDate}>{item.category}</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
paddingHorizontal: 22,
backgroundColor: '#eee6f0',
marginBottom: 12,
},
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginVertical: 12,
},
headerTitle: {
fontSize: 24,
fontWeight: 'bold',
},
collapseButton: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'white',
borderRadius: 20,
paddingHorizontal: 8,
width: SCREEN_WIDTH * 0.3,
height: 45,
justifyContent: 'center',
},
collapseText: {
marginRight: 8,
fontSize: 16,
fontWeight: 'bold',
},
itemContainer: {
height: 70,
flexDirection: 'row',
justifyContent: 'space-between',
backgroundColor: 'white',
alignItems: 'center',
borderRadius: 10,
padding: 12,
marginBottom: 10,
shadowColor: 'rgba(0,0,0,1)',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.3,
shadowRadius: 0.5,
elevation: 5,
},
itemLeftContainer: {
flexDirection: 'row',
alignItems: 'center',
},
itemImage: {
width: 40,
height: 40,
borderRadius: 20,
},
itemTextContainer: {
marginLeft: 10,
},
itemTitle: {
fontSize: 16,
fontWeight: '500',
},
itemPrice: {
fontSize: 16,
fontWeight: '700',
textAlign: 'right',
},
itemDate: {
color: 'gray',
},
});