-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
119 lines (113 loc) · 2.83 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
StyleSheet,
Text,
View,
TouchableHighlight,
TouchableOpacity,
SafeAreaView,
} from 'react-native';
import React, {useState} from 'react';
import {SwipeListView} from 'react-native-swipe-list-view';
const App = () => {
const [listData, setListData] = useState(
Array(20)
.fill('')
.map((_, i) => ({key: `${i}`, text: `item #${i}`})),
);
const closeRow = (rowMap: any, rowKey: any) => {
if (rowMap[rowKey]) {
rowMap[rowKey].closeRow();
}
};
const deleteRow = (rowMap: any, rowKey: any) => {
closeRow(rowMap, rowKey);
const newData = [...listData];
const prevIndex = listData.findIndex(item => item.key === rowKey);
newData.splice(prevIndex, 1);
setListData(newData);
};
const onRowDidOpen = (rowKey: any) => {
console.log('This row opened', rowKey);
};
const renderItem = (data: any) => (
<TouchableHighlight
onPress={() => console.log('You touched me')}
style={styles.rowFront}
underlayColor={'#AAA'}>
<View>
<Text>I am {data.item.text} in a SwipeListView</Text>
</View>
</TouchableHighlight>
);
const renderHiddenItem = (data: any, rowMap: any) => (
<View style={styles.rowBack}>
<Text>Left</Text>
<TouchableOpacity
style={[styles.backRightBtn, styles.backRightBtnLeft]}
onPress={() => closeRow(rowMap, data.item.key)}>
<Text style={styles.backTextWhite}>Close</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.backRightBtn, styles.backRightBtnRight]}
onPress={() => deleteRow(rowMap, data.item.key)}>
<Text style={styles.backTextWhite}>Delete</Text>
</TouchableOpacity>
</View>
);
return (
<SafeAreaView style={styles.container}>
<SwipeListView
data={listData}
renderItem={renderItem}
renderHiddenItem={renderHiddenItem}
leftOpenValue={75}
rightOpenValue={-150}
previewRowKey={'0'}
previewOpenValue={-40}
previewOpenDelay={3000}
onRowDidOpen={onRowDidOpen}
/>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
},
backTextWhite: {
color: '#FFF',
},
rowFront: {
alignItems: 'center',
backgroundColor: '#CCC',
borderBottomColor: 'black',
borderBottomWidth: 1,
justifyContent: 'center',
height: 50,
},
rowBack: {
alignItems: 'center',
backgroundColor: '#DDD',
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
paddingLeft: 15,
},
backRightBtn: {
alignItems: 'center',
bottom: 0,
justifyContent: 'center',
position: 'absolute',
top: 0,
width: 75,
},
backRightBtnLeft: {
backgroundColor: 'blue',
right: 75,
},
backRightBtnRight: {
backgroundColor: 'red',
right: 0,
},
});