import React, { Component } from
'react'
;
import { StyleSheet, Text, TouchableOpacity, View }
from
'react-native'
;
import { SwipeListView } from
'react-native-swipe-list-view'
;
class App extends Component {
constructor() {
super
();
this
.state = {
itemList: [
{ key:
'1'
, description:
'Item 1'
},
{ key:
'2'
, description:
'Item 2'
},
{ key:
'3'
, description:
'Item 3'
},
{ key:
'4'
, description:
'Item 4'
},
{ key:
'5'
, description:
'Item 5'
},
],
};
}
closeRow = (rowMap, rowKey) => {
if
(rowMap[rowKey]) {
rowMap[rowKey].closeRow();
}
};
deleteItem = (rowMap, rowKey) => {
this
.closeRow(rowMap, rowKey);
const newData = [...
this
.state.itemList];
const prevIndex =
this
.state.itemList
.findIndex(item => item.key === rowKey);
newData.splice(prevIndex, 1);
this
.setState({ itemList: newData });
};
onRowOpen = rowKey => {
console.log(
'Opened row with key:'
, rowKey);
};
renderItem = rowData => (
<TouchableOpacity
onPress={() => console.log(
'Item touched'
)}
style={styles.itemContainer}
>
<Text style={styles.itemText}>
{rowData.item.description}
</Text>
</TouchableOpacity>
);
renderHiddenItem = (rowData, rowMap) => (
<View style={styles.hiddenContainer}>
<TouchableOpacity
style={[styles.hiddenButton, styles.closeButton]}
onPress={() =>
this
.closeRow(rowMap, rowData.item.key)}
>
<Text style={styles.buttonText}>Close</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.hiddenButton, styles.deleteButton]}
onPress={() =>
this
.deleteItem(rowMap, rowData.item.key)}
>
<Text style={styles.buttonText}>Delete</Text>
</TouchableOpacity>
</View>
);
render() {
return
(
<View style={styles.container}>
{
}
<Text style={styles.heading}>Geeksforgeeks</Text>
<Text style={styles.subheading}>Swipe to
delete
</Text>
{
}
<SwipeListView
data={
this
.state.itemList}
renderItem={
this
.renderItem}
renderHiddenItem={
this
.renderHiddenItem}
leftOpenValue={75}
rightOpenValue={-150}
previewRowKey={
'0'
}
previewOpenValue={-40}
previewOpenDelay={3000}
onRowDidOpen={
this
.onRowOpen}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:
'#eee'
,
paddingVertical: 20,
paddingHorizontal: 15,
},
heading: {
fontSize: 30,
fontWeight:
'bold'
,
marginBottom: 20,
color:
'green'
,
margin: 20,
textAlign:
'center'
,
},
subheading: {
fontSize: 24,
fontWeight:
'bold'
,
marginBottom: 20,
color:
'#333'
,
margin: 10,
textAlign:
'center'
,
},
itemContainer: {
alignItems:
'center'
,
justifyContent:
'center'
,
backgroundColor:
'#FFF'
,
borderBottomColor:
'#E0E0E0'
,
borderBottomWidth: 1,
height: 80,
borderRadius: 8,
shadowColor:
'#000'
,
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
shadowRadius: 3,
elevation: 3,
marginBottom: 10,
},
itemText: {
color:
'#333'
,
fontSize: 16,
fontWeight:
'bold'
,
},
hiddenContainer: {
flexDirection:
'row'
,
justifyContent:
'flex-end'
,
alignItems:
'center'
,
backgroundColor:
'#FFF'
,
height: 80,
borderRadius: 20,
},
hiddenButton: {
justifyContent:
'center'
,
alignItems:
'center'
,
width: 75,
height: 80,
},
closeButton: {
backgroundColor:
'green'
,
borderRadius: 20,
},
deleteButton: {
backgroundColor:
'#E74C3C'
,
borderRadius: 20,
},
buttonText: {
color:
'#FFF'
,
fontSize: 16,
fontWeight:
'bold'
,
},
});
export
default
App;