1
- import React from 'react' ;
2
- import { Appearance , SafeAreaView , StatusBar , StyleSheet , Text , View } from 'react-native' ;
3
- import { Userpic } from 'react-native-userpic' ;
1
+ import React , { useCallback , useState } from 'react' ;
2
+ import { Appearance , ImageSourcePropType , StatusBar , StyleSheet , Text , View } from 'react-native' ;
3
+ import { Userpic , UserpicProps } from 'react-native-userpic' ;
4
4
5
5
StatusBar . setBarStyle ( 'light-content' ) ;
6
6
7
7
function colorScheme ( lightColor : string , darkColor : string ) {
8
8
return Appearance . getColorScheme ( ) === 'dark' ? darkColor : lightColor ;
9
9
}
10
10
11
- // Random user images:
12
- const images = [
13
- 'https://minimaltoolkit.com/images/randomdata/male/78 .jpg' ,
14
- 'https://minimaltoolkit.com/images/randomdata/ male/62 .jpg' ,
15
- 'https://minimaltoolkit.com/images/randomdata/male/46 .jpg' ,
16
- 'https://minimaltoolkit.com/images/randomdata/female/107 .jpg' ,
17
- 'https://minimaltoolkit.com/images/randomdata/ female/70 .jpg' ,
18
- 'https://minimaltoolkit.com/images/randomdata/female/96 .jpg' ,
11
+ const IMAGES = [
12
+ { uri : 'https://example.com/image.png' } ,
13
+ { uri : 'https://xsgames.co/randomusers/assets/avatars/female/44 .jpg' } ,
14
+ { uri : 'https://xsgames.co/randomusers/assets/avatars/ male/42 .jpg' } ,
15
+ { uri : 'https://xsgames.co/randomusers/assets/avatars/female/38 .jpg' } ,
16
+ { uri : 'https://xsgames.co/randomusers/assets/avatars/male/73 .jpg' } ,
17
+ { uri : 'https://xsgames.co/randomusers/assets/avatars/ female/2 .jpg' } ,
18
+ { uri : 'https://xsgames.co/randomusers/assets/avatars/male/46 .jpg' } ,
19
19
] ;
20
20
21
- const App = ( ) => (
22
- < SafeAreaView style = { styles . container } >
23
- < View style = { styles . content } >
21
+ const App = ( ) => {
22
+ const [ badge , setBadge ] = useState < number > ( 0 ) ;
23
+ const [ image , setImage ] = useState < number > ( 1 ) ;
24
+ const [ badImage , setBadImage ] = useState < ImageSourcePropType > ( ) ;
25
+
26
+ const toggleBadge = useCallback ( ( ) => {
27
+ console . log ( '-- STATE UPDATED --' ) ;
28
+ setBadge ( badge === 5 ? 0 : badge + 1 ) ;
29
+ } , [ badge ] ) ;
30
+
31
+ const toggleImage = useCallback ( ( ) => {
32
+ console . log ( '-- STATE UPDATED --' ) ;
33
+ setImage ( image + 1 === IMAGES . length ? 0 : image + 1 ) ;
34
+ } , [ image ] ) ;
35
+
36
+ const toggleBadImage = useCallback ( ( ) => {
37
+ console . log ( '-- STATE UPDATED --' ) ;
38
+ setBadImage ( badImage ? undefined : IMAGES [ 0 ] ) ;
39
+ } , [ badImage ] ) ;
40
+
41
+ return (
42
+ < View style = { styles . container } >
24
43
< View style = { styles . row } >
25
44
< Text style = { styles . label } > Shape</ Text >
26
45
< Userpic borderRadius = { 0 } />
27
- < Userpic borderRadius = "25%" />
28
- < Userpic borderRadius = "50%" />
46
+ < Userpic borderRadius = { 15 } />
47
+ < Userpic />
29
48
</ View >
30
49
< View style = { styles . row } >
31
- < Text style = { styles . label } > No image</ Text >
32
- < Userpic />
50
+ < Text style = { styles . label } onPress = { toggleBadImage } >
51
+ No image
52
+ </ Text >
53
+ < Userpic source = { badImage } />
33
54
< Userpic defaultSource = { require ( './assets/custom.png' ) } />
34
55
< Userpic name = "👩" />
35
56
</ View >
36
57
< View style = { styles . row } >
37
58
< Text style = { styles . label } > Initials</ Text >
38
- < Userpic name = "Nick" />
59
+ < Userpic name = "Nick" color = "gray" />
39
60
< Userpic name = "Jason Smith" colorize = { true } />
40
- < Userpic name = "Emma Miller" colorize = { true } />
61
+ < Userpic name = "Emma Miller" email = "[email protected] " colorize = { true } />
41
62
</ View >
42
63
< View style = { styles . row } >
43
- < Text style = { styles . label } > Image</ Text >
44
- < Userpic source = { { uri : images [ 1 ] } } />
45
- < Userpic source = { { uri : images [ 2 ] } } />
46
- < Userpic source = { { uri : images [ 5 ] } } />
64
+ < Text style = { styles . label } onPress = { toggleImage } >
65
+ Image
66
+ </ Text >
67
+ < Userpic source = { IMAGES [ image ] } />
68
+ < Userpic source = { IMAGES [ 3 ] } />
69
+ < Userpic source = { IMAGES [ 6 ] } />
47
70
</ View >
48
71
< View style = { styles . row } >
49
72
< Text style = { styles . label } > Gravatar</ Text >
@@ -52,36 +75,50 @@ const App = () => (
52
75
< Userpic borderRadius = { 10 } email = "[email protected] " />
53
76
</ View >
54
77
< View style = { styles . row } >
55
- < Text style = { styles . label } > Badge</ Text >
56
- < Userpic badge = { true } badgeColor = "#34c759" email = "[email protected] " />
57
- < Userpic badge = { 3 } badgeColor = "#007aff" email = "[email protected] " />
58
- < Userpic badge = { 100 } email = "[email protected] " />
78
+ < Text style = { styles . label } onPress = { toggleBadge } >
79
+ Badge
80
+ </ Text >
81
+ < Userpic
82
+ source = { IMAGES [ 1 ] }
83
+
84
+ badge = { ! ! badge }
85
+ badgeColor = "#34c759"
86
+ />
87
+ < Userpic
88
+ borderRadius = { 20 }
89
+
90
+ badge = { badge }
91
+ badgeColor = "#007aff"
92
+ />
93
+ < Userpic
94
+ borderRadius = { 10 }
95
+
96
+ badge = { badge ? badge + 100 : undefined }
97
+ />
59
98
</ View >
60
99
< View style = { styles . row } >
61
100
< Text style = { styles . label } > Status</ Text >
62
- < Userpic badge = "👋" badgeProps = { statusBadgeProps } email = "[email protected] " />
63
- < Userpic badge = "😀" badgeProps = { statusBadgeProps } email = "[email protected] " />
64
- < Userpic badge = "🐵" badgeProps = { statusBadgeProps } email = "[email protected] " />
101
+ < Userpic email = "[email protected] " badge = "👋" badgeProps = { statusBadgeProps } />
102
+ < Userpic email = "[email protected] " badge = "😀" badgeProps = { statusBadgeProps } />
103
+ < Userpic email = "[email protected] " badge = "🐵" badgeProps = { statusBadgeProps } />
65
104
</ View >
66
105
< View style = { styles . row } >
67
106
< Text style = { styles . label } > Size</ Text >
68
- < Userpic borderRadius = "25%" size = { 30 } email = "[email protected] " />
69
- < Userpic borderRadius = "25%" size = { 50 } email = "[email protected] " />
70
- < Userpic borderRadius = "25%" size = { 75 } email = "[email protected] " />
107
+ < Userpic borderRadius = { 8 } size = { 30 } email = "[email protected] " />
108
+ < Userpic borderRadius = { 12 } size = { 50 } email = "[email protected] " />
109
+ < Userpic borderRadius = { 18 } size = { 75 } email = "[email protected] " />
71
110
</ View >
72
111
</ View >
73
- </ SafeAreaView >
74
- ) ;
112
+ ) ;
113
+ } ;
75
114
76
115
const styles = StyleSheet . create ( {
77
116
container : {
78
117
flex : 1 ,
118
+ padding : 30 ,
79
119
justifyContent : 'center' ,
80
120
backgroundColor : colorScheme ( '#fff' , '#212124' ) ,
81
121
} ,
82
- content : {
83
- margin : 30 ,
84
- } ,
85
122
row : {
86
123
marginVertical : 10 ,
87
124
flexDirection : 'row' ,
@@ -104,8 +141,9 @@ const styles = StyleSheet.create({
104
141
} ,
105
142
} ) ;
106
143
107
- const statusBadgeProps = {
144
+ const statusBadgeProps : UserpicProps [ 'badgeProps' ] = {
108
145
size : 22 ,
146
+ position : 'bottom-right' ,
109
147
style : styles . badgeStyle ,
110
148
textStyle : styles . badgeTextStyle ,
111
149
} ;
0 commit comments