forked from FidMe/react-native-photo-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (111 loc) · 3.06 KB
/
index.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
import { ActivityIndicator, Dimensions, FlatList, View } from "react-native";
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Pagination, Slide } from "./src";
export default class Gallery extends Component {
constructor(props) {
super(props);
this.sendCurrentImageInfo(this.props.data[0]);
this.state = {
index: 0
};
if (this.props.initialIndex) {
setTimeout(() => {
this.goTo(this.props.initialIndex);
}, 100);
}
}
sendCurrentImageInfo = image => {
if (this.props.setCurrentImage) this.props.setCurrentImage(image);
};
onScrollEnd = e => {
const contentOffset = e.nativeEvent.contentOffset;
const viewSize = e.nativeEvent.layoutMeasurement;
const pageNum = Math.floor(contentOffset.x / viewSize.width);
if (pageNum !== this.state.index) {
this.setState({ index: pageNum });
}
};
getItemLayout = (data, index) => {
const { width } = this.state;
return {
length: width,
offset: width * index,
index
};
};
goTo = index => {
this.sendCurrentImageInfo(this.props.data[index]);
this.setState({ index });
this.swiper.scrollToOffset({
offset: Dimensions.get("window").width * index
});
};
_renderImage = item => {
return <Slide {...item} />;
};
render() {
const backgroundColor = this.props.backgroundColor || "#000";
const data = this.props.data || [];
return (
<View
onLayout={this.onLayout}
style={{
...styles.container,
backgroundColor
}}
>
{!data.length && <ActivityIndicator style={styles.loader} />}
<FlatList
onLayout={this.onLayout}
style={styles.flatList}
data={data}
extraData={this.state}
horizontal
initialNumToRender={this.props.initialNumToRender || 4}
ref={ref => (this.swiper = ref)}
pagingEnabled
onMomentumScrollEnd={this.onScrollEnd}
getItemLayout={this.getItemLayout}
renderItem={img => this._renderImage(img)}
keyExtractor={item => item.id}
/>
<Pagination
index={this.state.index}
data={data}
initialPaginationSize={this.props.initialPaginationSize || 10}
goTo={this.goTo}
backgroundColor={backgroundColor}
/>
</View>
);
}
}
Gallery.propTypes = {
backgroundColor: PropTypes.string,
data: PropTypes.arrayOf((propValue, key) => {
if (!propValue[key].id || !propValue[key].image) {
return new Error(
'Data prop is invalid. It must be an object containing "id" and "image" keys.'
);
}
}),
setCurrentImage: PropTypes.func
};
const styles = {
container: {
flex: 1,
flexDirection: "column",
justifyContent: "space-between"
},
flatList: {
flex: 1,
width: Dimensions.get("window").width,
alignSelf: "stretch"
},
loader: {
position: "absolute",
top: Dimensions.get("window").height / 2 - 10,
left: Dimensions.get("window").width / 2 - 10
}
};