-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
297 lines (272 loc) · 8.03 KB
/
Copy pathindex.js
File metadata and controls
297 lines (272 loc) · 8.03 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import React from 'react';
import {
StyleSheet,
FlatList,
View,
Dimensions,
Text,
TouchableOpacity,
Platform,
StatusBar,
I18nManager,
} from 'react-native';
import DefaultSlide from './DefaultSlide';
const { width, height } = Dimensions.get('window');
const isIphoneX =
Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS && (height === 812 || width === 812);
const isAndroidRTL = I18nManager.isRTL && Platform.OS === 'android';
export default class AppIntroSlider extends React.Component {
static defaultProps = {
activeDotStyle: {
backgroundColor: 'rgba(255, 255, 255, .9)',
},
dotStyle: {
backgroundColor: 'rgba(0, 0, 0, .2)',
},
skipLabel: 'Skip',
doneLabel: 'Done',
nextLabel: 'Next',
prevLabel: 'Back',
buttonStyle: null,
buttonTextStyle: null,
paginationStyle: null,
showDoneButton: true,
showNextButton: true,
};
state = {
width,
height,
activeIndex: 0,
};
goToSlide = pageNum => {
this.setState({ activeIndex: pageNum });
this.flatList.scrollToOffset({
offset: this._rtlSafeIndex(pageNum) * this.state.width,
});
};
// Get the list ref
getListRef = () => this.flatList;
_onNextPress = () => {
this.goToSlide(this.state.activeIndex + 1);
this.props.onSlideChange &&
this.props.onSlideChange(this.state.activeIndex + 1, this.state.activeIndex);
};
_onPrevPress = () => {
this.goToSlide(this.state.activeIndex - 1);
this.props.onSlideChange &&
this.props.onSlideChange(this.state.activeIndex - 1, this.state.activeIndex);
};
_onPaginationPress = index => {
const activeIndexBeforeChange = this.state.activeIndex;
this.goToSlide(index);
this.props.onSlideChange && this.props.onSlideChange(index, activeIndexBeforeChange);
};
_renderItem = flatListArgs => {
const { width, height } = this.state;
const props = { ...flatListArgs, dimensions: { width, height } };
return (
<View style={{ width, flex: 1 }}>
{this.props.renderItem ? (
this.props.renderItem(props)
) : (
<DefaultSlide bottomButton={this.props.bottomButton} {...props} />
)}
</View>
);
};
_renderButton = (name, onPress) => {
const show = this.props[`show${name}Button`];
const content = this.props[`render${name}Button`]
? this.props[`render${name}Button`]()
: this._renderDefaultButton(name);
return show && this._renderOuterButton(content, name, onPress);
};
_renderDefaultButton = name => {
let content = (
<Text style={[styles.buttonText, this.props.buttonTextStyle]}>
{this.props[`${name.toLowerCase()}Label`]}
</Text>
);
if (this.props.bottomButton) {
content = (
<View
style={[
styles.bottomButton,
(name === 'Skip' || name === 'Prev') && {
backgroundColor: 'transparent',
},
this.props.buttonStyle,
]}
>
{content}
</View>
);
}
return content;
};
_renderOuterButton = (content, name, onPress) => {
const style =
name === 'Skip' || name === 'Prev' ? styles.leftButtonContainer : styles.rightButtonContainer;
return (
<View style={!this.props.bottomButton && style}>
<TouchableOpacity
onPress={onPress}
style={this.props.bottomButton ? styles.flexOne : this.props.buttonStyle}
>
{content}
</TouchableOpacity>
</View>
);
};
_renderNextButton = () => this._renderButton('Next', this._onNextPress);
_renderPrevButton = () => this._renderButton('Prev', this._onPrevPress);
_renderDoneButton = () => this._renderButton('Done', this.props.onDone && this.props.onDone);
_renderSkipButton = () =>
// scrollToEnd does not work in RTL so use goToSlide instead
this._renderButton('Skip', () =>
this.props.onSkip ? this.props.onSkip() : this.goToSlide(this.props.slides.length - 1)
);
_renderPagination = () => {
const isLastSlide = this.state.activeIndex === this.props.slides.length - 1;
const isFirstSlide = this.state.activeIndex === 0;
const skipBtn =
(!isFirstSlide && this._renderPrevButton()) || (!isLastSlide && this._renderSkipButton());
const btn = isLastSlide ? this._renderDoneButton() : this._renderNextButton();
return (
<View style={[styles.paginationContainer, this.props.paginationStyle]}>
<View style={styles.paginationDots}>
{this.props.slides.length > 1 &&
this.props.slides.map((_, i) => (
<TouchableOpacity
key={i}
style={[
styles.dot,
this._rtlSafeIndex(i) === this.state.activeIndex
? this.props.activeDotStyle
: this.props.dotStyle,
]}
onPress={() => this._onPaginationPress(i)}
/>
))}
</View>
{btn}
{skipBtn}
</View>
);
};
_rtlSafeIndex = i => (isAndroidRTL ? this.props.slides.length - 1 - i : i);
_onMomentumScrollEnd = e => {
const offset = e.nativeEvent.contentOffset.x;
// Touching very very quickly and continuous brings about
// a variation close to - but not quite - the width.
// That's why we round the number.
// Also, Android phones and their weird numbers
const newIndex = this._rtlSafeIndex(Math.round(offset / this.state.width));
if (newIndex === this.state.activeIndex) {
// No page change, don't do anything
return;
}
const lastIndex = this.state.activeIndex;
this.setState({ activeIndex: newIndex });
this.props.onSlideChange && this.props.onSlideChange(newIndex, lastIndex);
};
_onLayout = () => {
const { width, height } = Dimensions.get('window');
if (width !== this.state.width || height !== this.state.height) {
// Set new width to update rendering of pages
this.setState({ width, height });
// Set new scroll position
const func = () => {
this.flatList.scrollToOffset({
offset: this._rtlSafeIndex(this.state.activeIndex) * width,
animated: false,
});
};
Platform.OS === 'android' ? setTimeout(func, 0) : func();
}
};
render() {
// Separate props used by the component to props passed to FlatList
const {
hidePagination,
activeDotStyle,
dotStyle,
skipLabel,
doneLabel,
nextLabel,
prevLabel,
buttonStyle,
buttonTextStyle,
renderItem,
data,
...otherProps
} = this.props;
return (
<View style={styles.flexOne}>
<FlatList
ref={ref => (this.flatList = ref)}
data={this.props.slides}
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
bounces={false}
style={styles.flatList}
renderItem={this._renderItem}
onMomentumScrollEnd={this._onMomentumScrollEnd}
extraData={this.state.width}
onLayout={this._onLayout}
{...otherProps}
/>
{!hidePagination && this._renderPagination()}
</View>
);
}
}
const styles = StyleSheet.create({
flexOne: {
flex: 1,
},
flatList: {
flex: 1,
flexDirection: isAndroidRTL ? 'row-reverse' : 'row',
},
paginationContainer: {
position: 'absolute',
bottom: 16 + (isIphoneX ? 34 : 0),
left: 16,
right: 16,
},
paginationDots: {
height: 16,
margin: 16,
flexDirection: isAndroidRTL ? 'row-reverse' : 'row',
justifyContent: 'center',
alignItems: 'center',
},
dot: {
width: 10,
height: 10,
borderRadius: 5,
marginHorizontal: 4,
},
leftButtonContainer: {
position: 'absolute',
left: 0,
},
rightButtonContainer: {
position: 'absolute',
right: 0,
},
bottomButton: {
flex: 1,
backgroundColor: 'rgba(0, 0, 0, .3)',
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
backgroundColor: 'transparent',
color: 'white',
fontSize: 18,
padding: 12,
},
});