Guide.js
3.71 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
'use strict';
import React from 'react-native';
import GuideItem from './GuideItem';
import PageControl from './PageControl';
let {
Component,
View,
ScrollView,
Text,
Platform,
Dimensions,
ViewPagerAndroid
} = React;
export default class Guide extends Component {
constructor(props) {
super(props);
this.state = {
currentPage: 0,
containerWidth: Dimensions.get('window').width,
};
this.onLayout = this.onLayout.bind(this);
this.updateSelectedPage = this.updateSelectedPage.bind(this);
}
onLayout(e) {
const { width, } = e.nativeEvent.layout;
if (width !== this.state.containerWidth) {
this.setState({
containerWidth: width,
});
}
}
updateSelectedPage(currentPage) {
let localCurrentPage = currentPage;
if (typeof localCurrentPage === 'object') {
localCurrentPage = currentPage.nativeEvent.position;
}
this.setState({
currentPage: localCurrentPage,
});
}
renderGuide() {
if (Platform.OS === 'ios') {
return (
<ScrollView
automaticallyAdjustContentInsets={false}
horizontal={true}
pagingEnabled={true}
showsHorizontalScrollIndicator={false}
onMomentumScrollEnd={(e) => {
const offsetX = e.nativeEvent.contentOffset.x;
const page = Math.round(offsetX / this.state.containerWidth);
if (this.state.currentPage !== page) {
this.updateSelectedPage(page);
}
}}>
{/*<GuideItem uri={'guide1'} text={'客源多多,免费开店'} />
<GuideItem uri={'guide2'} text={'店铺随身管,实时查看数据'} />
<GuideItem uri={'guide3'} text={'各种工具,玩转销售'} buttonText={'立即使用'} />*/}
{this.props.items.map((item, i) => {
return <GuideItem key={i} uri={item.uri} text={item.text} buttonText={item.buttonText} onPress={item.onPress} />;
})}
</ScrollView>
);
} else {
return (
<ViewPagerAndroid
initialPage={this.state.currentPage}
onPageSelected={this.updateSelectedPage}
keyboardDismissMode="on-drag">
{this.props.items.map((item, i) => {
return <GuideItem key={i} uri={item.uri} text={item.text} buttonText={item.buttonText} onPress={item.onPress} />;
})}
</ViewPagerAndroid>
);
}
}
render() {
let pageTop = Dimensions.get('window').height - 27;
if (Platform.OS === 'android') {
pageTop -= 30;
}
return (
<View onLayout={this.onLayout}>
{this.renderGuide()}
<PageControl style={{position:'absolute', left:0, right:0, top: pageTop}}
numberOfPages={this.props.items.length}
currentPage={this.state.currentPage}
hidesForSinglePage={true}
pageIndicatorTintColor='gray'
currentPageIndicatorTintColor='white'
indicatorStyle={{borderRadius: 5}}
currentIndicatorStyle={{borderRadius: 5}}
indicatorSize={{width:8, height:8}}/>
</View>
);
}
}