1- import React from 'react' ;
2- import { View , ViewProps , Image , ImageProps , StyleSheet } from 'react-native' ;
1+ import React , { useState } from 'react' ;
2+ import { View , ViewProps , Image , ImageProps , StyleSheet , ActivityIndicator } from 'react-native' ;
33
44const styles = StyleSheet . create ( {
5- defalut : {
5+ default : {
66 backgroundColor : '#e4e4e4' ,
77 overflow : 'hidden' ,
88 } ,
9- logo : {
10- width : 66 ,
11- height : 58 ,
12- } ,
139} ) ;
1410
1511const defaultImage = require ( './assets/user.png' ) ;
1612
1713export interface AvatarProps extends ViewProps {
18- /** React Native `Image` 组件 Props */
14+ /* React Native `Image` 组件 Props */
1915 imageProps ?: ImageProps ;
20- /** 图像源(远程URL或本地文件资源)。 */
16+ /* 图像源(远程URL或本地文件资源) */
2117 src ?: string | number ;
2218 /**
2319 * 尺寸
@@ -36,31 +32,35 @@ export interface AvatarProps extends ViewProps {
3632 shape ?: 'circle' | 'square' ;
3733}
3834
39- export default function Avatar ( props : AvatarProps ) {
40- const { style, src, size, shape, rounded, imageProps, ...otherProps } = props ;
35+ 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 ) ;
4138
4239 return (
4340 < View
4441 style = { [
45- styles . defalut ,
42+ styles . default ,
4643 style ,
47- { width : size , height : size } ,
48- { borderRadius : shape === 'circle' ? size ! / 2 : rounded } ,
44+ { width : size , height : size , borderRadius : shape === 'circle' ? size / 2 : rounded } ,
4945 ] }
5046 { ...otherProps }
5147 >
48+ < View
49+ style = { [
50+ styles . default ,
51+ { justifyContent : 'center' , alignItems : 'center' , flexDirection : 'row' , width : size , height : size } ,
52+ ] }
53+ >
54+ { ! imageLoaded && < ActivityIndicator size = "small" color = "gray" /> }
55+ </ View >
5256 < Image
53- style = { { width : size , height : size } }
57+ style = { { width : size , height : size , position : 'absolute' , top : 0 , left : 0 } }
5458 source = { typeof src === 'number' ? src : { uri : src as string } }
59+ onLoad = { ( ) => setImageLoaded ( true ) }
5560 { ...imageProps }
5661 />
5762 </ View >
5863 ) ;
59- }
64+ } ;
6065
61- Avatar . defaultProps = {
62- src : defaultImage ,
63- shape : 'square' ,
64- rounded : 3 ,
65- size : 40 ,
66- } as AvatarProps ;
66+ export default Avatar ;
0 commit comments