-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Progress component. (#182)
* feat:添加进度条 * doc:添加文档 * fix:添加动画
- Loading branch information
Showing
10 changed files
with
415 additions
and
205 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, {useState} from 'react'; | ||
import {View, Text} from 'react-native'; | ||
import {Progress, Button, Spacing} from '@uiw/react-native'; | ||
import Layout, {Container} from '../../Layout'; | ||
|
||
const {Header, Body, Card} = Layout; | ||
|
||
const ProgressDemo = (props: any) => { | ||
const {route} = props; | ||
const description = route.params.description; | ||
const title = route.params.title; | ||
|
||
const [val, setValue] = useState<number>(0); | ||
|
||
const onPress = () => { | ||
let count = val + 10; | ||
if (count > 100) { | ||
count = 0; | ||
} | ||
setValue(count) | ||
} | ||
|
||
return ( | ||
<Container> | ||
<Progress progress={30}/> | ||
<Header title={title} description={description} /> | ||
<Body> | ||
<Card title="基础实例" style={{margin:10}}> | ||
<Progress progressColor="red" progress={40}/> | ||
<Spacing /> | ||
<Button onPress={onPress}>(+-)10</Button> | ||
<Spacing /> | ||
<View style={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}> | ||
<Progress progress={val} progressColor='purple'/> | ||
<Text style={{fontSize: 12, width: 40, textAlign: 'right'}}>{val}%</Text> | ||
</View> | ||
<Spacing /> | ||
<Progress progressColor="orange" progress={60}/> | ||
<Spacing /> | ||
<Progress progressColor="yellow" progress={80}/> | ||
</Card> | ||
</Body> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default ProgressDemo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Progress 进度条 | ||
--- | ||
|
||
表明某个任务的当前进度。 | ||
|
||
### 基础示例 | ||
|
||
<!--DemoStart--> | ||
```jsx | ||
import { SafeAreaView } from 'react-native'; | ||
import { Progress } from '@uiw/react-native'; | ||
|
||
function Demo() { | ||
return ( | ||
<SafeAreaView style={{ flex: 1 }}> | ||
<Progress progress={30} position="fixed"/> | ||
</SafeAreaView> | ||
) | ||
} | ||
``` | ||
<!--End--> | ||
|
||
|
||
## Props | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
|------|------|-----|------| | ||
| `progress` | 进度百分比(可选) | Number | 0 | | ||
| `progressColor` | 颜色(可选) | String | none | | ||
| `position` | 位置,fixed 将浮出固定在最顶层(可选) | String | none | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, {useRef, useState, useEffect} from 'react'; | ||
import { | ||
Animated, | ||
View, | ||
StyleSheet, | ||
ViewProps, | ||
LayoutChangeEvent | ||
} from 'react-native' | ||
|
||
type PositionType = 'fixed' | 'relative' | ||
|
||
// 组件的属性定义 | ||
export interface ProgressProps extends ViewProps { | ||
/** 当前进度百分比, 0 - 100, 默认0 */ | ||
progress?: number; | ||
/** 颜色 */ | ||
progressColor? : string | ||
/** 位置 */ | ||
position?: PositionType; | ||
/** 动画持续的时间 */ | ||
animation?: { duration?: number; } | boolean; | ||
} | ||
|
||
export default (props: ProgressProps) => { | ||
const { | ||
style, | ||
progress = 0, | ||
progressColor = '#108ee9', | ||
position, | ||
animation = { duration: 500 }, | ||
} = props; | ||
|
||
const progWidth = useRef<any>(new Animated.Value(0)).current; | ||
const [wrapWidth, setWrapWidth] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
startAnimation(); | ||
}, [wrapWidth, progress]) | ||
|
||
const startAnimation = () => { | ||
Animated.timing(progWidth, { | ||
toValue: getWidth(), | ||
duration: typeof animation !== 'boolean' ? animation.duration : 1000, | ||
useNativeDriver: false, | ||
}).start(); | ||
}; | ||
|
||
const onLayout = (e: LayoutChangeEvent) => { | ||
setWrapWidth(e.nativeEvent.layout.width); | ||
} | ||
|
||
const getWidth = (percent: number = progress) => { | ||
return wrapWidth * (normalPercent(percent) / 100) | ||
} | ||
|
||
const normalPercent = (percent?: number) => { | ||
let widthPercent: any = 0 | ||
if (percent !== undefined && percent > 0) { | ||
widthPercent = percent > 100 ? 100 : percent | ||
} | ||
return widthPercent | ||
} | ||
|
||
return ( | ||
<View style={[styles.container, style]}> | ||
<View onLayout={onLayout} style={[ | ||
styles.pre, | ||
position === 'fixed' ? { position: 'absolute', top: 0 } : {}, | ||
{ borderColor: progressColor }]}> | ||
<Animated.View | ||
style={[styles.preOisn, { | ||
width: progWidth, | ||
backgroundColor: progressColor | ||
}]}></Animated.View> | ||
</View> | ||
</View> | ||
) | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
position: 'relative', | ||
flex: 1, | ||
}, | ||
pre: { | ||
borderWidth: 1, | ||
height: 4, | ||
width: '100%', | ||
borderRadius: 20, | ||
marginBottom: 0, | ||
marginTop: 0, | ||
overflow: 'hidden', | ||
}, | ||
preOisn: { | ||
position: 'absolute', | ||
height: 4, | ||
left: 0, | ||
top: 0, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Markdown, { importAll } from '../../../component/Markdown'; | ||
|
||
export default class Page extends Markdown { | ||
path="/packages/core/src/Progress/README.md" | ||
getMarkdown = async () => { | ||
const md = await import('@uiw/react-native/lib/Progress/README.md'); | ||
// 支持 markdown 中,相对于当前 index.tsx 相对路径引入图片资源 | ||
importAll((require as any).context('./', true, /\.(png|gif|jpg|svg)$/), this.imageFiles); | ||
return md.default || md; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters