Skip to content

Commit 1262d65

Browse files
zuojiahui秋小白.丶
andauthored
feat(Swiper): Add new component. (#180)
* fix: 修改首页 * 添加Android(Windows)环境安装文档 * 添加轮播图组件 Co-authored-by: 秋小白.丶 <oncwnuOahxb2al-n8NtRsvkJAg2Q@git.weixin.qq.com>
1 parent 89b35f8 commit 1262d65

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

example/examples/src/routes.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {StackNavigationProp} from '@react-navigation/stack';
1+
import { StackNavigationProp } from '@react-navigation/stack';
22

33
type ModalStackNavigation = StackNavigationProp<{}>;
44

@@ -336,6 +336,14 @@ export const stackPageData: Routes[] = [
336336
params: {
337337
title: 'SpeedDial 悬浮标记',
338338
description: 'SpeedDial 悬浮标记组件按下时,浮动动作按钮可以以快速显示标记的形式显示指定相关动作。',
339+
}
340+
},
341+
{
342+
name: 'Swiper',
343+
component: require('./routes/Swiper').default,
344+
params: {
345+
title: 'Swiper 轮播图',
346+
description: '轮播图',
339347
},
340348
},
341349
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import { View } from 'react-native';
3+
import { Swiper } from '@uiw/react-native';
4+
const SwiperDemo = () => {
5+
const data = [
6+
{ url: 'https://img2.baidu.com/it/u=1669646624,570058533&fm=26&fmt=auto&gp=0.jpg' },
7+
{ url: 'https://img0.baidu.com/it/u=1079471900,3150741739&fm=26&fmt=auto&gp=0.jpg' },
8+
{ url: 'https://img0.baidu.com/it/u=2385784823,911121382&fm=26&fmt=auto&gp=0.jpg' },
9+
]
10+
return (
11+
<View>
12+
<Swiper dataSource={data} height={150} />
13+
</View>
14+
15+
)
16+
}
17+
export default SwiperDemo

packages/core/src/Swiper/index.tsx

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React, { useState, useEffect, useRef, useCallback, } from 'react'
2+
import { StyleSheet, View, Image } from 'react-native';
3+
export interface dataSourceType {
4+
url: string
5+
// onClick?: () => void
6+
}
7+
export interface SwiperProps {
8+
dataSource: dataSourceType[];
9+
time?: number;
10+
width?: number;
11+
height?: number;
12+
autoplay?: boolean
13+
}
14+
const Swiper = (porps: SwiperProps) => {
15+
const { dataSource = [], time = 3000, autoplay = true, width = "100%", height = 130 } = porps
16+
let [curIndex, setCurIndex] = useState(0)
17+
let timer = useRef<NodeJS.Timeout | undefined>();
18+
const autoPlay = useCallback(() => {
19+
clearInterval(timer.current as unknown as number);
20+
timer.current = setInterval(() => {
21+
if (curIndex === dataSource.length - 1) {
22+
setCurIndex(0);
23+
} else {
24+
setCurIndex(curIndex + 1);
25+
}
26+
}, time);
27+
}, [dataSource, curIndex]);
28+
useEffect(() => {
29+
if (autoplay) {
30+
autoPlay()
31+
}
32+
}, [autoPlay]);
33+
useEffect(() => {
34+
return () => {
35+
clearInterval(timer.current as unknown as number);
36+
};
37+
}, []);
38+
return (
39+
<View style={StyleSheet.flatten([styles.box, { height }])}>
40+
<View style={StyleSheet.flatten([styles.banner, { width }])}>
41+
<View style={StyleSheet.flatten([styles.bannerOut, { marginLeft: curIndex * -100 + '%' }])}>
42+
{dataSource.map((item: any, index: number) => {
43+
return <Image style={styles.tinyLogo} key={index} source={{
44+
uri: item.url,
45+
}} />
46+
})}
47+
</View>
48+
<View style={styles.dotBox}>
49+
{dataSource.map((_: any, index: number) => {
50+
return <View key={index} style={StyleSheet.flatten([styles.dot, index === curIndex ? styles.dotSetColor : styles.dotColor])} />
51+
})}
52+
</View>
53+
</View>
54+
</View>
55+
56+
)
57+
}
58+
const styles = StyleSheet.create({
59+
box: {
60+
margin: 12,
61+
height: 130,
62+
},
63+
banner: {
64+
width: "100%",
65+
borderRadius: 25,
66+
position: "relative",
67+
overflow: "hidden",
68+
},
69+
bannerOut: {
70+
width: "100%",
71+
height: "100%",
72+
flexDirection: 'row',
73+
alignItems: 'center',
74+
marginLeft: 0
75+
},
76+
tinyLogo: {
77+
width: "100%",
78+
height: "100%",
79+
},
80+
dotBox: {
81+
width: "100%",
82+
flexDirection: 'row',
83+
alignItems: 'center',
84+
justifyContent: "center",
85+
position: "absolute",
86+
bottom: 12
87+
},
88+
dot: {
89+
width: 10,
90+
height: 10,
91+
borderRadius: 5,
92+
marginTop: 0,
93+
marginBottom: 0,
94+
marginLeft: 12,
95+
marginRight: 12
96+
},
97+
dotColor: {
98+
backgroundColor: "lightgray",
99+
},
100+
dotSetColor: {
101+
backgroundColor: "red",
102+
}
103+
});
104+
export default Swiper

packages/core/src/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export * from './ExpandableSection';
6363
export { default as Steps } from './Steps';
6464
export * from './Steps';
6565

66-
export { default as Rating } from './Rating'
67-
export * from './Rating'
66+
export { default as Rating } from './Rating';
67+
export * from './Rating';
6868

6969
export { default as Timeline } from './Timeline';
7070
export * from './Timeline';
@@ -79,7 +79,8 @@ export * from './NoticeBar';
7979

8080
export { default as SpeedDial } from './SpeedDial'
8181
export * from './SpeedDial'
82-
82+
export { default as Swiper } from './Swiper';
83+
export * from './Swiper';
8384
/**
8485
* Typography
8586
*/

0 commit comments

Comments
 (0)