-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swiper.js
162 lines (153 loc) · 5.96 KB
/
Swiper.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
import React, { Component } from 'react';
import { View, Dimensions, TouchableOpacity } from 'react-native';
import propTypes from 'prop-types';
import Carousel, { ParallaxImage, Pagination } from 'react-native-snap-carousel';
import Button from 'react-native-vector-icons/AntDesign';
import { scale, moderateScale } from 'react-native-size-matters';
import AutoHeightImage from 'react-native-auto-height-image';
const windowWidth = Dimensions.get('window').width;
export default class Swiper extends Component {
state = {
activeIndex: 0,
}
getContainerStyle() {
let containerWidth = this.props.containerWidth;
let containerHeight = this.props.containerHeight;
let swiperWidth = this.props.swiperWidth;
let swiperHeight = this.props.swiperHeight;
if (swiperWidth > containerWidth) {
swiperWidth = containerWidth;
console.warn('swiperWidth cant be larger containerWidth in Swiper.js');
}
if(swiperHeight > containerHeight) {
swiperHeight = containerHeight;
console.warn('swiperHeight cant be larger containerHeight in Swiper.js');
}
if (this.props.useButtons) {
if (containerWidth === swiperWidth) {
console.warn('no space for buttons in Swiper.js. Please provide a swiperWidth which is smaller than containerWidth to allow room for buttons');
swiperWidth = containerWidth * 0.70;
}
}
if (this.props.showPagination) {
if (containerHeight === swiperHeight) {
console.warn('no space for pagination in Swiper.js. Please provide a swiperHeight which is smaller than containerHeight to allow room for pagination');
swiperHeight = containerHeight * 0.90;
}
}
return { containerWidth, containerHeight, swiperWidth, swiperHeight };
}
render() {
const style = this.getContainerStyle();
console.log('containerWidth: ' + style.containerWidth + ' containerHeight: ' + style.containerHeight);
return (
<View style={{ width: style.containerWidth, height: style.containerHeight }}>
<View style={{ flex: 1, height: style.swiperHeight, flexDirection: 'row', }}>
{this.renderButtonArea('left')}
{this.renderCarousel(style)}
{this.renderButtonArea('right')}
</View>
<View>
{this.renderPagination(style)}
</View>
</View>
);
}
renderCarousel(style) {
return (
<View>
<Carousel
ref={(ref) => { this.carousel = ref; }}
data={this.props.data}
renderItem={({ item }, parallaxProps) => {
if (this.props.useParallax) {
return (
<ParallaxImage
source={{ uri: item.uri }}
dimensions={{ width: style.swiperWidth, height: style.swiperHeight }} // need set dimension for image to show
{...parallaxProps}
/>
);
} else {
return <AutoHeightImage source={{ uri: item.uri }} width={style.swiperWidth} height={style.swiperHeight} />
}
}}
sliderWidth={style.swiperWidth}
itemWidth={style.swiperWidth}
hasParallaxImages={this.props.useParallax}
onSnapToItem={(index) => { this.setState({ activeIndex: index }); }}
/>
</View>
);
}
renderPagination(style) {
/*
const paginationHeight = style.containerHeight - style.swiperHeight;
console.log('paginationHeight: ' + paginationHeight);
can't set pagination height for some reason
*/
return this.props.showPagination ? (
<Pagination activeDotIndex={this.state.activeIndex} dotsLength={this.props.data.length} />
)
:
null;
}
renderButtonArea(direction) {
return this.props.showButtons ? (
<View style={{ flex: 1, justifyContent: 'center', alignContent: 'center' }}>
{this.renderButton(direction)}
</View>
)
:
null;
}
renderButton(direction) {
if (direction === 'left') {
return this.state.activeIndex > 0 ? (
<TouchableOpacity
onPress={() => {
this.carousel._snapToItem(this.state.activeIndex - 1);
}}
>
<Button name={'left'} size={moderateScale(35)} />
</TouchableOpacity>
)
:
null;
} else if (direction === 'right') {
return this.state.activeIndex < this.props.data.length - 1 ? (
<TouchableOpacity
onPress={() => {
this.carousel._snapToItem(this.state.activeIndex + 1);
}}
>
<Button name={'right'} size={moderateScale(35)} />
</TouchableOpacity>
)
:
null;
} else {
throw new Exception('internal swiper.js error');
}
}
}
Swiper.propTypes = {
containerWidth: propTypes.number,
swiperWidth: propTypes.number,
containerHeight: propTypes.number,
swiperHeight: propTypes.number,
showPagination: propTypes.bool,
showButtons: propTypes.bool,
data: propTypes.arrayOf(propTypes.shape({ uri: propTypes.string.isRequired })),
useParallax: propTypes.bool
};
Swiper.defaultProps = {
containerWidth: windowWidth,
swiperWidth: windowWidth * 0.8,
containerHeight: 190,
swiperHeight: 190,
showPagination: false,
showButtons: false,
data: [],
useParallax: false
};