Skip to content

Commit 354de54

Browse files
committed
fix(Avatar): 增加loading api
1 parent 42d4fa7 commit 354de54

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

packages/core/src/Avatar/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function Demo() {
6969
export default Demo
7070

7171
```
72-
7372
### 指定头像的形状
7473

7574
circle 圆头像
@@ -91,6 +90,25 @@ function Demo() {
9190

9291
export default Demo
9392

93+
```
94+
95+
### loading
96+
97+
```jsx mdx:preview&background=#bebebe29
98+
import React from 'react';
99+
import { View } from 'react-native';
100+
import { Avatar } from '@uiw/react-native';
101+
102+
function Demo() {
103+
return (
104+
<View style={{ flexDirection: 'row',justifyContent:'space-around' }}>
105+
<Avatar shape='circle' src="https://avatars.githubusercontent.com/u/24369183?v=4" loading={true} />
106+
</View>
107+
);
108+
}
109+
110+
export default Demo
111+
94112
```
95113
### Props
96114

@@ -104,3 +122,4 @@ export default Demo
104122
| `size` | 尺寸 | number | - |
105123
| `rounded` | 设置圆角 | number | - |
106124
| `shape` |指定头像的形状 | `circle|square` | - |
125+
| `loading` | 是否加载 | `boolean` | `false` |

packages/core/src/Avatar/index.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState } from 'react';
2-
import { View, ViewProps, Image, ImageProps, StyleSheet, ActivityIndicator } from 'react-native';
1+
import React from 'react';
2+
import { View, ViewProps, Image, ImageProps, StyleSheet, ActivityIndicator, Text } from 'react-native';
33

44
const styles = StyleSheet.create({
55
default: {
@@ -30,12 +30,20 @@ export interface AvatarProps extends ViewProps {
3030
* @default square
3131
*/
3232
shape?: 'circle' | 'square';
33+
loading?: boolean;
3334
}
3435

3536
const Avatar: React.FC<AvatarProps> = (props) => {
36-
const { style, src = defaultImage, size = 40, shape = 'square', rounded = 3, imageProps, ...otherProps } = props;
37-
const [imageLoaded, setImageLoaded] = useState(false);
38-
37+
const {
38+
style,
39+
src = defaultImage,
40+
size = 40,
41+
shape = 'square',
42+
rounded = 3,
43+
imageProps,
44+
loading = false,
45+
...otherProps
46+
} = props;
3947
return (
4048
<View
4149
style={[
@@ -48,15 +56,22 @@ const Avatar: React.FC<AvatarProps> = (props) => {
4856
<View
4957
style={[
5058
styles.default,
51-
{ justifyContent: 'center', alignItems: 'center', flexDirection: 'row', width: size, height: size },
59+
{
60+
justifyContent: 'center',
61+
alignItems: 'center',
62+
flexDirection: 'row',
63+
width: size,
64+
height: size,
65+
zIndex: loading ? 2023 : 0,
66+
},
5267
]}
5368
>
54-
{!imageLoaded && <ActivityIndicator size="small" color="gray" />}
69+
{loading && <ActivityIndicator size="small" color="gray" />}
5570
</View>
71+
5672
<Image
5773
style={{ width: size, height: size, position: 'absolute', top: 0, left: 0 }}
5874
source={typeof src === 'number' ? src : { uri: src as string }}
59-
onLoad={() => setImageLoaded(true)}
6075
{...imageProps}
6176
/>
6277
</View>

0 commit comments

Comments
 (0)