-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpandableCard.js
173 lines (161 loc) · 5.12 KB
/
ExpandableCard.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
import React, { useState, useEffect } from 'react';
import { View, Text, TouchableWithoutFeedback, Animated } from 'react-native';
import { PropTypes } from 'prop-types';
const ExpandableCard = ({ expandedCardItems, collapsedCardItems, labelStyle, valueStyle, style }) => {
const [expandedCard, setExpandedCard] = useState(false);
const [heightCollapsed, setHeightCollapsed] = useState(0);
const [heightMaximized, setHeightMaximized] = useState(0);
const [heightValue] = useState(new Animated.Value(1000));
useEffect(() => {
heightValue.setValue(heightCollapsed);
}, [heightCollapsed]);
useEffect(() => {
heightValue.setValue(!expandedCard ? heightMaximized : heightCollapsed);
Animated.spring(heightValue, {
toValue: expandedCard ? heightMaximized : heightCollapsed
}).start();
}, [expandedCard]);
const descriptionContainerAnimationStyle = (heightValue !== 0 ? {height: heightValue} : {});
const _renderInformation = (key, value) => {
return (
<View style={styles.labelContainer}>
<View><Text style={{...styles.label, ...labelStyle}}>{ key }</Text></View>
{
Array.isArray(value) ?
<View style={{flex: 1, alignItems: 'center'}}>
{
value.map((item, i) => {
if(typeof item === 'string') {
return <Text key={ item } style={{...styles.text, textAlign: 'right', ...valueStyle}}>{ item }</Text>
}
else if(React.isValidElement(item)) {
return (
<View key={i} style={{width: '100%', alignItems: 'flex-end'}}>
{ item }
</View>
);
}
})
}
</View>
:
React.isValidElement(value) ? <View key={i} style={{width: '100%', alignItems: 'flex-end'}}> { item } </View>
: <View style={{flex: 1, alignItems: 'center'}}><Text style={{...styles.text, ...valueStyle}}>{ value }</Text></View>
}
</View>
);
};
const _renderItems = (items) => {
return (
<React.Fragment>
{
items.map((item, i) => {
if(typeof item.label !== 'undefined' && typeof item.value !== 'undefined') {
return (
<React.Fragment key={i}>
{
_renderInformation(item.label, item.value)
}
</React.Fragment>
);
}
})
}
</React.Fragment>
);
};
return (
<View style={{...styles.card, ...style}}>
{
heightCollapsed !== 0 && heightMaximized !== 0 ?
<TouchableWithoutFeedback onPress={() => { setExpandedCard(!expandedCard) }}>
<Animated.View style={[{overflow: 'hidden'}, descriptionContainerAnimationStyle]}>
<React.Fragment>
{
!expandedCard ?
<React.Fragment>
{
_renderItems(collapsedCardItems)
}
<View
style={{width: '100%', position: 'absolute', bottom: 5}}
>
<Text style={{textAlign: 'center', fontWeight: 'bold', color: Colors.textDefault, fontSize: 24}}>. . .</Text>
</View>
</React.Fragment>
:
_renderItems(expandedCardItems)
}
</React.Fragment>
</Animated.View>
</TouchableWithoutFeedback>
:
<View>
<View onLayout={(e) => {
setHeightCollapsed(e.nativeEvent.layout.height + 30);
}}>
{
_renderItems(collapsedCardItems)
}
</View>
<View style={{opacity: 0}} onLayout={(e) => {
setHeightMaximized(e.nativeEvent.layout.height);
}}>
{
_renderItems(expandedCardItems)
}
</View>
</View>
}
</View>
);
};
const itemsPropType = PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.string.isRequired,
value: PropTypes.oneOfType([
PropTypes.string.isRequired,
PropTypes.number.isRequired,
PropTypes.array.isRequired,
PropTypes.element.isRequired
])
}));
ExpandableCard.propTypes = {
expandedCardItems: itemsPropType,
collapsedCardItems: itemsPropType,
labelStyle: PropTypes.object,
valueStyle: PropTypes.object,
style: PropTypes.object
};
ExpandableCard.defaultProps = {
style: { backgroundColor: '#F4F0F6' }
};
const styles = {
card: {
paddingHorizontal: 20,
paddingVertical: 10,
shadowColor: 'black',
shadowOpacity: 0.26,
shadowOffset: {width: 0, height: 2},
shadowRadius: 8,
elevation: 5,
borderRadius: 3,
backgroundColor: 'white',
},
labelContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'flex-start',
marginVertical: 5
},
label: {
fontSize: 18,
color: '#333'
},
text: {
width: '100%',
fontSize: 18,
textAlign: 'right',
marginTop: 1
}
};
export default ExpandableCard;