-
Notifications
You must be signed in to change notification settings - Fork 4
/
ThumbnailSelector.tsx
188 lines (177 loc) · 4.73 KB
/
ThumbnailSelector.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
import React, {useRef, useState} from 'react';
import {
Text,
FlatList,
Image,
TouchableOpacity,
Animated,
useWindowDimensions,
ImageProps,
ImageSourcePropType,
TextProps,
TouchableOpacityProps,
ViewStyle,
LayoutChangeEvent,
} from 'react-native';
export type ThumbnailItem = {
caption?: string;
imageSrc: ImageSourcePropType;
};
export type ThumbnailItemIndex = {
item: ThumbnailItem;
index: number;
};
export type ThumbnailSelectorProps = {
thumbnails: ThumbnailItem[];
toggle?: (func: () => Promise<Animated.EndResult>) => void;
renderThumbnail?: (
item: ThumbnailItem,
index: number,
onSelect?: (item: ThumbnailItem, index: number) => void,
) => JSX.Element;
onSelect?: (item: ThumbnailItem, index: number) => void;
initialIndex?: number;
captionCharacterMaxLimit?: number;
captionEllipsis?: string;
activeColor?: string;
inactiveColor?: string;
imageProps?: ImageProps;
textProps?: TextProps;
touchableOpacityProps?: TouchableOpacityProps;
animatedViewStyle?: ViewStyle;
animationConfig?: Animated.SpringAnimationConfig;
flatListViewStyle?: ViewStyle;
animatedViewTestID?: string;
};
const ThumbnailSelector: React.FunctionComponent<ThumbnailSelectorProps> = ({
thumbnails,
toggle = undefined,
renderThumbnail = undefined,
onSelect = undefined,
initialIndex = -1,
captionCharacterMaxLimit = 15,
captionEllipsis = '...',
activeColor = 'white',
inactiveColor = 'black',
imageProps = {
style: {width: 136, height: 136, borderWidth: 1},
},
textProps = {
style: {fontSize: 16, textAlign: 'center', fontWeight: 'bold'},
numberOfLines: 1,
},
touchableOpacityProps = {
style: {padding: 8},
},
animatedViewStyle = {
elevation: 1,
zIndex: 1,
position: 'absolute',
top: 0,
left: 0,
right: 0,
},
animationConfig = {
toValue: 0,
friction: 9,
useNativeDriver: false,
},
flatListViewStyle = {backgroundColor: 'grey', padding: 8},
animatedViewTestID = 'ThumbnailSelector',
}) => {
const window = useWindowDimensions();
const [itemIndex, setItemIndex] = useState(initialIndex);
const [animViewHeight, setAnimViewHeight] = useState(0);
const animatedValue = useRef(new Animated.Value(0));
const toValue = useRef(animationConfig.toValue);
function _toggle(): Promise<Animated.EndResult> {
if (toValue.current) {
animationConfig.toValue = 0;
} else {
animationConfig.toValue = 1;
}
toValue.current = animationConfig.toValue;
return new Promise<Animated.EndResult>(resolve => {
Animated.spring(animatedValue.current, animationConfig).start(resolve);
});
}
if (toggle) {
toggle(_toggle);
}
function _renderItem(obj: ThumbnailItemIndex): React.JSX.Element {
const {item, index} = obj;
if (renderThumbnail) {
return renderThumbnail(item, index, onSelect);
}
const selected = itemIndex === index;
const color = selected ? activeColor : inactiveColor;
let caption = item.caption;
if (caption) {
if (caption.length > captionCharacterMaxLimit) {
const end = captionCharacterMaxLimit - captionEllipsis.length;
caption = `${caption.substring(0, end)}${captionEllipsis}`;
}
}
return (
<TouchableOpacity
{...touchableOpacityProps}
onPress={() => {
setItemIndex(index);
if (onSelect) {
onSelect(item, index);
}
}}>
<Image
{...imageProps}
style={[{borderColor: color}, imageProps.style]}
source={item.imageSrc}
/>
{caption && (
<Text {...textProps} style={[{color}, textProps.style]}>
{caption}
</Text>
)}
</TouchableOpacity>
);
}
function _onLayout(event: LayoutChangeEvent): void {
const {height} = event.nativeEvent.layout;
if (animViewHeight !== height) {
setAnimViewHeight(height);
}
}
function _getAnimViewStyle(): ViewStyle[] {
const start = window.height;
const end = window.height - animViewHeight;
return [
animatedViewStyle,
{
transform: [
{
translateY: animatedValue.current.interpolate({
inputRange: [0, 1],
outputRange: [start, end],
}),
},
],
},
];
}
return (
<Animated.View
testID={animatedViewTestID}
style={_getAnimViewStyle()}
onLayout={_onLayout}>
<FlatList
style={flatListViewStyle}
data={thumbnails}
initialNumToRender={thumbnails.length}
extraData={itemIndex}
horizontal={true}
renderItem={_renderItem}
keyExtractor={(_item, index) => `${index}`}
/>
</Animated.View>
);
};
export default ThumbnailSelector;