1
- import React , { useState } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import {
3
3
SafeAreaView ,
4
4
StyleSheet ,
9
9
Alert ,
10
10
} from 'react-native' ;
11
11
12
+ // @ts -ignore-next-line
12
13
import SwipeableFlatList from 'react-native-swipeable-list' ;
13
14
14
15
import { dummyData } from './data/dummyData' ;
@@ -28,11 +29,28 @@ const colorEmphasis = {
28
29
disabled : 0.38 ,
29
30
} ;
30
31
31
- const extractItemKey = item => {
32
+ const extractItemKey = ( item : Item ) => {
32
33
return item . id . toString ( ) ;
33
34
} ;
34
35
35
- const Item = ( { item, backgroundColor, textColor, deleteItem} ) => {
36
+ interface RenderItemProps {
37
+ item : {
38
+ id : number ;
39
+ name : string ;
40
+ subject : string ;
41
+ text : string ;
42
+ } ;
43
+ deleteItem : ( itemId : Item [ 'id' ] ) => void ;
44
+ }
45
+
46
+ type Item = {
47
+ id : number ;
48
+ name : string ;
49
+ subject : string ;
50
+ text : string ;
51
+ } ;
52
+
53
+ const RenderItem = ( { item} : RenderItemProps ) => {
36
54
return (
37
55
< >
38
56
< View style = { styles . item } >
@@ -54,21 +72,56 @@ const Item = ({item, backgroundColor, textColor, deleteItem}) => {
54
72
) ;
55
73
} ;
56
74
75
+ interface QuickActionsProps {
76
+ index : number ;
77
+ item : Item ;
78
+ archiveItem : ( itemId : Item [ 'id' ] ) => void ;
79
+ snoozeItem : ( itemId : Item [ 'id' ] ) => void ;
80
+ deleteItem : ( itemId : Item [ 'id' ] ) => void ;
81
+ }
82
+
83
+ const QuickActions = ( {
84
+ item,
85
+ archiveItem,
86
+ snoozeItem,
87
+ deleteItem,
88
+ } : QuickActionsProps ) => {
89
+ return (
90
+ < View style = { styles . qaContainer } >
91
+ < View style = { [ styles . button , styles . button1 ] } >
92
+ < Pressable onPress = { ( ) => archiveItem ( item . id ) } >
93
+ < Text style = { styles . buttonText } > Archive</ Text >
94
+ </ Pressable >
95
+ </ View >
96
+ < View style = { [ styles . button , styles . button2 ] } >
97
+ < Pressable onPress = { ( ) => snoozeItem ( item . id ) } >
98
+ < Text style = { styles . buttonText } > Snooze</ Text >
99
+ </ Pressable >
100
+ </ View >
101
+ < View style = { [ styles . button , styles . button3 ] } >
102
+ < Pressable onPress = { ( ) => deleteItem ( item . id ) } >
103
+ < Text style = { styles . buttonText } > Delete</ Text >
104
+ </ Pressable >
105
+ </ View >
106
+ </ View >
107
+ ) ;
108
+ } ;
109
+
57
110
function renderItemSeparator ( ) {
58
111
return < View style = { styles . itemSeparator } /> ;
59
112
}
60
113
61
114
const App = ( ) => {
62
115
const [ data , setData ] = useState ( dummyData ) ;
63
116
64
- const deleteItem = itemId => {
117
+ const deleteItem = ( itemId : Item [ 'id' ] ) => {
65
118
// ! Please don't do something like this in production. Use proper state management.
66
119
const newState = [ ...data ] ;
67
120
const filteredState = newState . filter ( item => item . id !== itemId ) ;
68
121
return setData ( filteredState ) ;
69
122
} ;
70
123
71
- const archiveItem = itemId => {
124
+ const archiveItem = ( itemId : Item [ 'id' ] ) => {
72
125
Alert . alert (
73
126
'DISHONESTY ALERT' ,
74
127
"Not gonna Archive it. We're actually are gonna just delete it." ,
@@ -87,7 +140,7 @@ const App = () => {
87
140
) ;
88
141
} ;
89
142
90
- const snoozeItem = itemId => {
143
+ const snoozeItem = ( itemId : Item [ 'id' ] ) => {
91
144
Alert . alert (
92
145
'DISHONESTY ALERT' ,
93
146
"Not gonna Snooze it. We're actually are gonna just delete it." ,
@@ -106,28 +159,6 @@ const App = () => {
106
159
) ;
107
160
} ;
108
161
109
- const QuickActions = ( index , qaItem ) => {
110
- return (
111
- < View style = { styles . qaContainer } >
112
- < View style = { [ styles . button , styles . button1 ] } >
113
- < Pressable onPress = { ( ) => archiveItem ( qaItem . id ) } >
114
- < Text style = { [ styles . buttonText , styles . button1Text ] } > Archive</ Text >
115
- </ Pressable >
116
- </ View >
117
- < View style = { [ styles . button , styles . button2 ] } >
118
- < Pressable onPress = { ( ) => snoozeItem ( qaItem . id ) } >
119
- < Text style = { [ styles . buttonText , styles . button2Text ] } > Snooze</ Text >
120
- </ Pressable >
121
- </ View >
122
- < View style = { [ styles . button , styles . button3 ] } >
123
- < Pressable onPress = { ( ) => deleteItem ( qaItem . id ) } >
124
- < Text style = { [ styles . buttonText , styles . button3Text ] } > Delete</ Text >
125
- </ Pressable >
126
- </ View >
127
- </ View >
128
- ) ;
129
- } ;
130
-
131
162
return (
132
163
< >
133
164
< StatusBar barStyle = "dark-content" />
@@ -138,13 +169,16 @@ const App = () => {
138
169
< SwipeableFlatList
139
170
keyExtractor = { extractItemKey }
140
171
data = { data }
141
- renderItem = { ( { item} ) => (
142
- < Item item = { item } deleteItem = { ( ) => deleteItem } />
172
+ renderItem = { ( { item} : RenderItemProps ) => (
173
+ < RenderItem item = { item } deleteItem = { ( ) => deleteItem } />
143
174
) }
144
175
maxSwipeDistance = { 240 }
145
- renderQuickActions = { ( { index, item} ) => QuickActions ( index , item ) }
176
+ renderQuickActions = { ( { index, item} : { index : number ; item : Item } ) =>
177
+ QuickActions ( { index, item, archiveItem, snoozeItem, deleteItem} )
178
+ }
146
179
contentContainerStyle = { styles . contentContainerStyle }
147
- shouldBounceOnMount = { true }
180
+ // shouldBounceOnMount={false} -- This is not working on 0.74+ React Native
181
+ bounceFirstRowOnMount = { false } // THIS IS THE WORKAROUND
148
182
ItemSeparatorComponent = { renderItemSeparator }
149
183
/>
150
184
</ SafeAreaView >
@@ -175,7 +209,7 @@ const styles = StyleSheet.create({
175
209
padding : 10 ,
176
210
} ,
177
211
messageContainer : {
178
- backgroundColor : darkColors . backgroundColor ,
212
+ backgroundColor : darkColors . background ,
179
213
maxWidth : 300 ,
180
214
} ,
181
215
name : {
@@ -228,22 +262,24 @@ const styles = StyleSheet.create({
228
262
alignItems : 'center' ,
229
263
justifyContent : 'center' ,
230
264
} ,
231
- buttonText : {
232
- fontWeight : 'bold' ,
233
- opacity : colorEmphasis . high ,
265
+ button1 : {
266
+ backgroundColor : darkColors . primary ,
234
267
} ,
235
- button1Text : {
236
- color : darkColors . primary ,
268
+ button2 : {
269
+ backgroundColor : darkColors . secondary ,
237
270
} ,
238
- button2Text : {
239
- color : darkColors . secondary ,
271
+ button3 : {
272
+ backgroundColor : darkColors . error ,
240
273
} ,
241
- button3Text : {
242
- color : darkColors . error ,
274
+ buttonText : {
275
+ fontWeight : 'bold' ,
276
+ opacity : colorEmphasis . high ,
277
+ color : darkColors . onBackground ,
278
+ fontSize : 16 ,
243
279
} ,
244
280
contentContainerStyle : {
245
281
flexGrow : 1 ,
246
- backgroundColor : darkColors . backgroundColor ,
282
+ backgroundColor : darkColors . background ,
247
283
} ,
248
284
} ) ;
249
285
0 commit comments