Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 172 additions & 120 deletions docs/buildingSwipeList.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,133 +12,185 @@ Here is an example to show how easily and quickly we can use [react-native-swipe
```SnackPlayer name=SwipeList dependencies=react-native-swipe-list-view

import React, { useState } from 'react';
import {
Dimensions,
TouchableOpacity,
View,
} from 'react-native';
import { Dimensions, TouchableOpacity, View } from 'react-native';

import {NativeBaseProvider,Box, Text,Pressable,Heading,IconButton,Icon, HStack, Avatar } from 'native-base';
import {
NativeBaseProvider,
Box,
Text,
Pressable,
Heading,
IconButton,
Icon,
HStack,
Avatar,
VStack,
Spacer,
} from 'native-base';
import { SwipeListView } from 'react-native-swipe-list-view';
import { MaterialIcons,Ionicons } from '@expo/vector-icons';
import { MaterialIcons, Ionicons, Entypo } from '@expo/vector-icons';

export default function App() {
const [mode, setMode] = useState('Basic');

return (
<NativeBaseProvider >
<Box textAlign="center" bg="white" flex= "1" safeAreaTop>
<Heading my="6" textAlign="center" size="lg">Swipe list Example</Heading>
<Basic />
</Box>
</NativeBaseProvider>
);
const [mode, setMode] = useState('Basic');

return (
<NativeBaseProvider>
<Box bg="white" flex="1" safeAreaTop>
<Heading p="4" pb="3" size="lg">
Inbox
</Heading>
<Basic />
</Box>
</NativeBaseProvider>
);
}


function Basic() {
const [listData, setListData] = useState(
Array(20)
.fill('')
.map((_, i) => ({ key: `${i}`, text: `item #${i}` }))
);

const closeRow = (rowMap, rowKey) => {
if (rowMap[rowKey]) {
rowMap[rowKey].closeRow();
}
};

const deleteRow = (rowMap, rowKey) => {
closeRow(rowMap, rowKey);
const newData = [...listData];
const prevIndex = listData.findIndex(item => item.key === rowKey);
newData.splice(prevIndex, 1);
setListData(newData);
};

const onRowDidOpen = rowKey => {
console.log('This row opened', rowKey);
};

const renderItem = ({item, index}) => (
<Box>
<Pressable
onPress={() => console.log('You touched me')}
alignItems= 'center'
bg="white"
borderBottomColor= 'trueGray.200'
borderBottomWidth= "1"
justifyContent= 'center'
height= "50"
underlayColor={'#AAA'}
_pressed={{
bg:'trueGray.200'
}}
py={8}
>

<HStack width="100%" px="4">
<HStack space="2" alignItems="center">
<Avatar color="white" bg="secondary.700">
{index}
</Avatar>
<Text>
{item.text}
</Text>
</HStack>
</HStack>
</Pressable>
</Box>
);

const renderHiddenItem = (data, rowMap) => (
<HStack
flex= "1"
pl="2"
>
<Pressable
px="4"
ml='auto'
cursor="pointer"
bg="dark.500"
justifyContent="center"
onPress={() => closeRow(rowMap, data.item.key)}
_pressed={{
opacity: 0.5
}}
>
<Icon as={<Ionicons name="close" />} color='white'/>
</Pressable>
<Pressable
px="4"
cursor="pointer"
bg="red.500"
justifyContent="center"
onPress={() => deleteRow(rowMap, data.item.key)}
_pressed={{
opacity: 0.5
}}
>
<Icon as={<MaterialIcons name="delete"/>} color="white" />
</Pressable>
</HStack>
);

return (
<Box bg= 'white' safeArea
flex= "1">
<SwipeListView
data={listData}
renderItem={renderItem}
renderHiddenItem={renderHiddenItem}
rightOpenValue={-130}
previewRowKey={'0'}
previewOpenValue={-40}
previewOpenDelay={3000}
onRowDidOpen={onRowDidOpen}
/>
const data = [
{
id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
fullName: 'Afreen Khan',
timeStamp: '12:47 PM',
recentText: 'Good Day!',
avatarUrl:
'https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
},
{
id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
fullName: 'Sujitha Alexander',
timeStamp: '11:11 PM',
recentText: 'Cheer up, there!',
avatarUrl:
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTyEaZqT3fHeNrPGcnjLLX1v_W4mvBlgpwxnA&usqp=CAU',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29d72',
fullName: 'Anci Barroco',
timeStamp: '6:22 PM',
recentText: 'Good Day!',
avatarUrl: 'https://miro.medium.com/max/1400/0*0fClPmIScV5pTLoE.jpg',
},
{
id: '68694a0f-3da1-431f-bd56-142371e29d72',
fullName: 'Aniket Kumar',
timeStamp: '8:56 PM',
recentText: 'All the best',
avatarUrl:
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSr01zI37DYuR8bMV5exWQBSw28C1v_71CAh8d7GP1mplcmTgQA6Q66Oo--QedAN1B4E1k&usqp=CAU',
},
{
id: '28694a0f-3da1-471f-bd96-142456e29d72',
fullName: 'Kiara',
timeStamp: '12:47 PM',
recentText: 'I will call him today.',
avatarUrl:
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRBwgu1A5zgPSvfE83nurkuzNEoXs9DMNr8Ww&usqp=CAU',
},
];

const [listData, setListData] = useState(data);

const closeRow = (rowMap, rowKey) => {
if (rowMap[rowKey]) {
rowMap[rowKey].closeRow();
}
};

const deleteRow = (rowMap, rowKey) => {
closeRow(rowMap, rowKey);
const newData = [...listData];
const prevIndex = listData.findIndex((item) => item.key === rowKey);
newData.splice(prevIndex, 1);
setListData(newData);
};

const onRowDidOpen = (rowKey) => {
console.log('This row opened', rowKey);
};

const renderItem = ({ item, index }) => (
<Box>
<Pressable onPress={() => console.log('You touched me')} bg="white">
<Box
borderBottomWidth="1"
borderColor="coolGray.200"
pl="4"
pr="5"
py="2">
<HStack alignItems="center" space={3}>
<Avatar size="48px" source={{ uri: item.avatarUrl }} />
<VStack>
<Text color="coolGray.800" bold>
{item.fullName}
</Text>
<Text color="coolGray.500">{item.recentText}</Text>
</VStack>
<Spacer />
<Text fontSize="xs" color="coolGray.800" alignSelf="flex-start">
{item.timeStamp}
</Text>
</HStack>
</Box>
);
</Pressable>
</Box>
);

const renderHiddenItem = (data, rowMap) => (
<HStack flex="1" pl="2">
<Pressable
w="70"
ml="auto"
cursor="pointer"
bg="coolGray.200"
justifyContent="center"
onPress={() => closeRow(rowMap, data.item.key)}
_pressed={{
opacity: 0.5,
}}>
<VStack alignItems="center" space={2}>
<Icon
as={<Entypo name="dots-three-horizontal" />}
size="xs"
color="coolGray.800"
/>
<Text fontSize="xs" fontWeight="medium" color="coolGray.800">
More
</Text>
</VStack>
</Pressable>
<Pressable
w="70"
cursor="pointer"
bg="red.500"
justifyContent="center"
onPress={() => deleteRow(rowMap, data.item.key)}
_pressed={{
opacity: 0.5,
}}>
<VStack alignItems="center" space={2}>
<Icon as={<MaterialIcons name="delete" />} color="white" size="xs" />
<Text color="white" fontSize="xs" fontWeight="medium">
Delete
</Text>
</VStack>
</Pressable>
</HStack>
);

return (
<Box bg="white" safeArea flex="1">
<SwipeListView
data={listData}
renderItem={renderItem}
renderHiddenItem={renderHiddenItem}
rightOpenValue={-130}
previewRowKey={'0'}
previewOpenValue={-40}
previewOpenDelay={3000}
onRowDidOpen={onRowDidOpen}
/>
</Box>
);
}

```
Loading