React Native: How to implement 2 columns of swipping cards? - react-native

I am trying to implement a scrollable list of cards in 2 columns. The cards should be swipe-able left or right out of the screen to be removed.
Basically, it should be like how the Chrome app is showing the list of tabs currently, which can be swiped away to be closed. See example image here.
I am able to implement the list of cards in 2 columns using FlatList. However, I have trouble making the cards swipe-able. I tried react-tinder-card but it cannot restrict swiping up and down and hence the list becomes not scrollable. react-native-deck-swiper also does not work well with list.
Any help is appreciated. Thank you!

I am going to implement a component that satisfies the following requirements:
Create a two column FlatList whose items are your cards.
Implement a gesture handling that recognizes swipeLeft and swipeRight actions which will remove the card that was swiped.
The swipe actions should be animated, meaning we have some kind of drag of the screen behavior.
I will use the basic react-native FlatList with numColumns={2} and react-native-swipe-list-view to handle swipeLeftand swipeRight actions as well as the desired animations.
I will implement a fire and forget action, thus after removing an item, it is gone forever. We will implement a restore mechanism later if we want to be able to restore removed items.
My initial implementation works as follows:
Create a FlatList with numColumns={2} and some additional dummy styling to add some margins.
Create state using useState which holds an array of objects that represent our cards.
Implement a function that removes an item from the state provided an id.
Wrap the item to be rendered in a SwipeRow.
Pass the removeItem function to the swipeGestureEnded prop.
import React, { useState } from "react"
import { FlatList, SafeAreaView, Text, View } from "react-native"
import { SwipeRow } from "react-native-swipe-list-view"
const data = [
{
id: "0",
title: "Title 1",
},
{
id: "1",
title: "Title 2",
},
{
id: "2",
title: "Title 3",
},
{
id: "3",
title: "Title 4",
},
{
id: "4",
title: "Title 5",
},
{
id: "5",
title: "Title 6",
},
{
id: "6",
title: "Title 7",
},
{
id: "7",
title: "Title 8",
},
]
export function Test() {
const [cards, setCards] = useState(data)
const [removed, setRemoved] = useState([])
function removeItem(id) {
let previous = [...cards]
let itemToRemove = previous.find((x) => x.id === id)
setCards(previous.filter((c) => c.id !== id))
setRemoved([...removed, itemToRemove])
}
return (
<SafeAreaView style={{ margin: 20 }}>
<FlatList
data={cards}
numColumns={2}
keyExtractor={(item) => item.id}
renderItem={({ index, item }) => (
<SwipeRow swipeGestureEnded={() => removeItem(item.id)}>
<View />
<View style={{ margin: 20, borderWidth: 1, padding: 20 }}>
<Text>{item.title}</Text>
</View>
</SwipeRow>
)}
/>
</SafeAreaView>
)
}
Notice that we need some kind of property in our objects in order to determine which one we want to remove. I have used a basic id property here, which is quite common using FlatList. If you are retrieving your data from an API which does not provide the same id, then we could just do some preprocessing (normalization) first and add the id prop ourselves.
The initial view looks as follows.
Swiping, let's say the item with 'Title 6' to the right or to the left removes it.
It might be desired to implement the following feature as well.
If the item is in the first column, then only swiping to the left will remove the item.
If the item is in the second column, then only swiping to the right will remove the item.
This is easily implemented using the index param which is passed to the renderItem function and the vx prop of the gestureState passed to the swipeGestureEnded function.
Here is fully working implementation.
import React, { useState } from "react"
import { FlatList, SafeAreaView, Text, View } from "react-native"
import { SwipeRow } from "react-native-swipe-list-view"
const data = [
{
id: "0",
title: "Title 1",
},
{
id: "1",
title: "Title 2",
},
{
id: "2",
title: "Title 3",
},
{
id: "3",
title: "Title 4",
},
{
id: "4",
title: "Title 5",
},
{
id: "5",
title: "Title 6",
},
{
id: "6",
title: "Title 7",
},
{
id: "7",
title: "Title 8",
},
]
export function Test() {
const [cards, setCards] = useState(data)
const [removed, setRemoved] = useState([])
function removeItem(id) {
let previous = [...cards]
let itemToRemove = previous.find((x) => x.id === id)
setCards(previous.filter((c) => c.id !== id))
setRemoved([...removed, itemToRemove])
}
return (
<SafeAreaView style={{ margin: 20 }}>
<FlatList
data={cards}
numColumns={2}
keyExtractor={(item) => item.id}
renderItem={({ index, item }) => (
<SwipeRow
swipeGestureEnded={(key, event) => {
if (event.gestureState.vx < 0) {
if (index % 2 === 0) {
removeItem(item.id)
}
} else if (event.gestureState.vx >= 0) {
if (index % 2 === 1) {
removeItem(item.id)
}
}
}}
disableLeftSwipe={index % 2 === 1}
disableRightSwipe={index % 2 === 0}>
<View />
<View style={{ margin: 20, borderWidth: 1, padding: 20 }}>
<Text>{item.title}</Text>
</View>
</SwipeRow>
)}
/>
</SafeAreaView>
)
}
Since the index is zero based in a FlatList, an item is in the second column if and only if index % 2 === 1 (e.g. an item with index 3 is always in the second column and thus not divisible by 2), on the other hand an item is in the first column if and only if index % 2 === 0 that is index is divisible by 2.
There are several callback function props in the SwipeRowComponent that should be fired in certain situations. However, most of them did not work in my setup and I still have no clue why. I got it to work by using the event.gestureState.vx property which is negative if we swipe to the left and positive (including zero) if we swipe to the right.
It might be desired to implement an undo button as it is quite common in this kind of functionalities. This can be done as follows:
Implement a second state which represents a Queue that holds lastly removed items. The undo button then just pops the lastly removed item.
Here is a fully working implementation with a dummy undo button that achieves exactly that.
import React, { useState } from "react"
import { Button, FlatList, SafeAreaView, Text, View } from "react-native"
import { SwipeRow } from "react-native-swipe-list-view"
const data = [
{
id: "0",
title: "Title 1",
},
{
id: "1",
title: "Title 2",
},
{
id: "2",
title: "Title 3",
},
{
id: "3",
title: "Title 4",
},
{
id: "4",
title: "Title 5",
},
{
id: "5",
title: "Title 6",
},
{
id: "6",
title: "Title 7",
},
{
id: "7",
title: "Title 8",
},
]
export function Test() {
const [cards, setCards] = useState(data)
const [removed, setRemoved] = useState([])
function removeItem(id) {
let previous = [...cards]
let itemToRemove = previous.find((x) => x.id === id)
setCards(previous.filter((c) => c.id !== id))
setRemoved([...removed, itemToRemove])
}
function undoRemove() {
if (removed && removed.length > 0) {
let itemToUndo = removed[removed.length - 1]
setCards([...cards, itemToUndo])
setRemoved(removed.filter((c) => c.id !== itemToUndo.id))
}
}
return (
<SafeAreaView style={{ margin: 20 }}>
<FlatList
data={cards}
numColumns={2}
keyExtractor={(item) => item.id}
renderItem={({ index, item }) => (
<SwipeRow
swipeGestureEnded={(key, event) => {
if (event.gestureState.vx < 0) {
if (index % 2 === 0) {
removeItem(item.id)
}
} else if (event.gestureState.vx >= 0) {
if (index % 2 === 1) {
removeItem(item.id)
}
}
}}
disableLeftSwipe={index % 2 === 1}
disableRightSwipe={index % 2 === 0}>
<View />
<View style={{ margin: 20, borderWidth: 1, padding: 20 }}>
<Text>{item.title}</Text>
</View>
</SwipeRow>
)}
/>
<Button onPress={undoRemove} title="Undo" />
</SafeAreaView>
)
}
Notice that my undo button just appends the removed item to the end of the list. If you want to keep the initial index, then you need to save the old index and push the item to the correct position.
Here is workin snack of my last implementation.

Related

React Native List Map method selected item scroll to the middle of ScrollView

`
import * as React from 'react';
import { Text, View, StyleSheet, ScrollView, TouchableOpacity } from 'react-native';
import { useState } from "react";
import {useRef,createRef} from 'react';
import { useEffect } from 'react';
import { Dimensions } from 'react-native';
const windowWidth = Dimensions.get('window').width;
export default function App() {
const [selectedId, setSelectedId] = useState(null);
const elementsRef = useRef(persons.map(() => createRef()));
const [y, setY] = useState(false);
return (
<View style={{
flex:1,
justifyContent:'center',
alignItems:'center',
}}>
<ScrollView contentContainerStyle={{
paddingHorizontal:20
}}>
<View>
{persons.map((person, index) => {
const backgroundColor = person.id === selectedId ? "#6e3b6e" : "#f9c2ff";
const onPress = (newRef) => {
newRef?.current?.measureInWindow( (fx, fy, width, height, px, py) => {
console.log('Component width is: ' + width)
console.log('Component height is: ' + height)
console.log('X offset to frame: ' + fx)
console.log('Y offset to frame: ' + fy)
console.log('X offset to page: ' + px)
console.log('Y offset to page: ' + py)
setY(fy)
})
setSelectedId(person.id)
}
return (
<TouchableOpacity
ref={elementsRef?.current[index]}
key={person.id}
style={{
backgroundColor:backgroundColor,
marginBottom:20,
height:200,
justifyContent:'center',
alignItems:'center',
width: windowWidth-40
}}
onPress={() => onPress(elementsRef?.current[index])} >
<Text>{person.name}</Text>
<Text>{y}</Text>
</TouchableOpacity>
);
})}
</View>
</ScrollView>
</View>
);
}
const persons = [
{
id: "1",
name: "Earnest Green",
},
{
id: "2",
name: "Winston Orn",
},
{
id: "3",
name: "Carlton Collins",
},
{
id: "4",
name: "Malcolm Labadie",
},
{
id: "5",
name: "Michelle Dare",
},
{
id: "6",
name: "Carlton Zieme",
},
{
id: "7",
name: "Jessie Dickinson",
},
{
id: "8",
name: "Julian Gulgowski",
},
{
id: "9",
name: "Ellen Veum",
},
{
id: "10",
name: "Lorena Rice",
},
{
id: "11",
name: "Carlton Zieme",
},
{
id: "12",
name: "Jessie Dickinson",
},
{
id: "13",
name: "Julian Gulgowski",
},
{
id: "14",
name: "Ellen Veum",
},
{
id: "15",
name: "Lorena Rice",
},
];
`
Open code in Expo Snack > https://snack.expo.dev/#stefanosalexandrou/d6a238
I create a list with Map method. Every item in screen is selectable. When Item selected then background of selected item changed. Also the code get the Y position of item in window screen.
What I want:
I want when click item, the selected item from start Y position move/scroll to the middle of ScrollView.
Like this> https://www.youtube.com/shorts/XqX0x9OzN8M,
https://www.youtube.com/shorts/an2_Dih-D8g

Insert Data to State in React Native

I have a list data and a input type for add data, so I make state for save data and rendering it. I want when I type text in TextInput then I enter so data will added. but for this issue, screen must to be refresh for data added.
const [dataSubjects, setDataSubject] = useState([
{
id: "PPKN",
name: "Pendidikan Pancasila dan Kewarganegaraan (PPKn)",
},
{
id: "BI",
name: "Bahasa Indonesia (BI)",
},
{
id: "MAT",
name: "Matematika (MAT)",
},
{
id: "IPA",
name: "Ilmu Pengetahuan Alam (IPA)",
},
{
id: "IPS",
name: "Ilmu Pengetahuan Sosial (IPS)",
},
{
id: "SBDP",
name: "Seni Budaya dan Prakarya (SBdP)",
},
{
id: "PJOK",
name: "Pendidikan Jasmani, Olahraga, dan Kesehatan (PJOK)",
},
])
const HandleSubmit = (name) => {
setText("")
setDataSubject((prevSubject) => {
return [{ name, id: Math.random().toString() }, ...prevSubject]
})
}
This is a code for state data and function add data
<TextInput
visible={inputSubject}
mode="outlined"
placeholder="Type other subject…"
underlineColor="transparent"
value={text}
style={{
backgroundColor: theme.colors.surface,
fontSize: 14,
borderRadius: 4,
flex: 1,
}}
onChangeText={handleChangeText}
onSubmitEditing={() => HandleSubmit(text)}
/>
this is a TextInput code
Are you trying to add whatever you type in <TextInput/> as a new item in dataSubjects? If you are you can add an item to an array by using either of the two:
Array.prototype.unshift() to addd an item at the start of the array
Array.prototype.push() to add an item at the end of the array
Specifically, you can try something like this:
const HandleSubmit = (name) => {
setText("")
setDataSubject(dataSubjects.unshift({name: name, id: Math.random().toString()})
}

Set Selected items in react native multi select

I want to select the selected items which i already select & save at backend after that if i get response then set already selected those items which i have set in react native multi select
here is my code for react native multiselect
//Example Multiple select / Dropdown / Picker in React Native
import React, { Component } from "react";
//Import React
import { View, Text, Picker, StyleSheet, SafeAreaView } from "react-native";
//Import basic react native components
import MultiSelect from "react-native-multiple-select";
//Import MultiSelect library
//Dummy Data for the MutiSelect
this.items = [
{ id: "1", name: "America" },
{ id: "2", name: "Argentina" },
{ id: "3", name: "Armenia" },
{ id: "4", name: "Australia" },
{ id: "5", name: "Austria" },
{ id: "6", name: "Azerbaijan" },
{ id: "7", name: "Argentina" },
{ id: "8", name: "Belarus" },
{ id: "9", name: "Belgium" },
{ id: "10", name: "Brazil" }
];
export default class App extends Component {
state = {
//We will store selected item in this
selectedItems: [
{ id: "1", name: "America" },
{ id: "2", name: "Argentina" },
{ id: "3", name: "Armenia" },
{ id: "4", name: "Australia" }
]
};
onSelectedItemsChange = selectedItems => {
this.setState({ selectedItems });
//Set Selected Items
};
render() {
const { selectedItems } = this.state;
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1, padding: 30 }}>
<MultiSelect
hideTags
items={items}
uniqueKey="id"
ref={component => {
this.multiSelect = component;
}}
onSelectedItemsChange={this.onSelectedItemsChange}
selectedItems={selectedItems}
selectText="Pick Items"
searchInputPlaceholderText="Search Items..."
onChangeInput={text => console.log(text)}
tagRemoveIconColor="#CCC"
tagBorderColor="#CCC"
tagTextColor="#CCC"
selectedItemTextColor="#CCC"
selectedItemIconColor="#CCC"
itemTextColor="#000"
displayKey="name"
searchInputStyle={{ color: "#CCC" }}
submitButtonColor="#48d22b"
submitButtonText="Submit"
/>
<View>
{this.multiSelect &&
this.multiSelect.getSelectedItemsExt(selectedItems)}
</View>
</View>
</SafeAreaView>
);
}
}
Got it.
this.state={
selectedData: "158" // list of id here(without space, that is your response of api)
};
<MultiSelect
.....
.....
selectedItems={selectedData}
/>
<View>
{this.multiselect ? this.multiSelect.getSelectedItemsExt(selectedData): null}
</View>
I had this same issue and I figured out how to preset selections by using parts of the accepted answer.
I learned that selectedItems is a list of whatever is set as the uniqueKey in the MultipleSelect props.
For example, if you put the uniqueKey as a numbered id of each list member and you wanted the second member set as selected, you would have selectedItems = {[ "2" ]}
I hope this makes sense.

react-select Render custom component as SingleValue

I need to display an icon before the selected value using react-select and typescript.
This is what I tried so far:
SingleValue: React.SFC<SingleValueProps<OptionType>> = ({ children, ...props }) => (
<components.SingleValue {...props}>
<i className={`fal fa-${this.props.icon} has-text-grey-light`} /> {children}
</components.SingleValue>
)
The main issue is with type definitions that expects that children passed to components.SingleValue must be a string.
You don´t have to use the standard components. You can easlily create a custom one but still keep the styles it needs.
The only requirement you need is emotion to get the styles the SingleValue component uses.
/**
* This Example uses the `FontAwesome` React library.
**/
const options = [
{ label: "Value A", value: "a", icon: faCoffee },
{ label: "Value B", value: "b", icon: faCar },
{ label: "Value C", value: "c", icon: faMobile },
{ label: "Value D", value: "d", icon: faCircle },
{ label: "Value E", value: "e", icon: faSquare }
];
const SingleValue = ({
cx,
getStyles,
selectProps,
data,
isDisabled,
className,
...props
}) => {
console.log(props);
return (
<div
className={cx(
emotionCss(getStyles("singleValue", props)),
{
"single-value": true,
"single-value--is-disabled": isDisabled
},
className
)}
>
<FontAwesomeIcon icon={data.icon} style={{ marginRight: 10 }} />
<div>{selectProps.getOptionLabel(data)}</div>
</div>
);
};
export default class MySelect extends Component {
render() {
return (
<Select
options={options}
components={{ SingleValue }}
styles={{
singleValue: (provided, state) => ({
...provided,
display: "flex", // To keep icon and label aligned
alignItems: "center"
})
}}
/>
);
}
}
Working example

Access property value after setting it with setNativeProps

I'm trying to toggle the display of dynamically created components in React Native (with Coffeescript). The components are created conform the structure of a JSON object and each component has a ref attribute with the according id so i can access the specific component like so: this.refs[ id ]
I have the following function to change the display of the component
changeDisplay: ( id ) ->
this.refs[ id ].setNativeProps( {
display: 'flex'
} );
This works fine when i want to change the initial display, but i want to toggle the display between none and flex (based on the current state).
Because i don't know how many items are rendered on forehand i can't (or don't know how to) define states for them.
I tried the following, but i somehow can't get the new display state back after setting it with setNativeProps()
changeDisplay: ( id ) ->
if this.refs[ id ].props.style.display is 'none'
this.refs[ id ].setNativeProps( {
display: 'flex'
} );
else
this.refs[ id ].setNativeProps( {
display: 'none'
} );
Initially this.refs[ id ].props.style.display returns the state the component is in when i load the view (= none), but after i run the function above the display state is visually changed (the component shows up), but this.refs[ id ].props.style.display still return the initial state none
Why?
EDIT:
Below is my (stripped) component (as requested). I want to toggle the display of the child tasks in a certain project. So if i tap the project Project 1, i want to toggle the display state of the tasks 1 & 2.
import React from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
ScrollView
} from 'react-native';
import {
Icon,
} from 'react-native-elements';
class ListTasks extends React.Component
constructor: ( props ) ->
super props
this.data =
list: [
{
id: 1
name: 'Project 1'
type: 'project'
parent: null
},{
id: 2
parent: 1
name: 'Task 1'
type: 'task'
},{
id: 3
parent: 1
name: 'Task 2'
type: 'task'
},{
id: 4
name: 'Project 2'
type: 'project'
parent: null
},{
id: 5
parent: 4
name: 'Task 3'
type: 'task'
},{
id: 6
parent: 4
name: 'Task 4'
type: 'task'
}
]
toggleDisplay: ( id ) ->
if this.refs[ id ].props.style.display is v[ 0 ]
this.refs[ id ].setNativeProps( {
display: 'flex'
} );
else
this.refs[ id ].setNativeProps( {
display: 'none'
} );
renderList: ( data ) ->
<View>
<View>
<TouchableOpacity onPress={() => this.toggleDisplay( data.id )}>
<Text>{data.name}</Text>
</TouchableOpacity>
</View>
<View ref={data.id} style={display: 'none'}>
{ this.renderTask cor for cor in this.data.list when cor.parent is data.id }
</View>
</View>
renderTask: ( data ) ->
<View>
<Text>{data.name}</Text>
</View>
render: ->
<View>
<ScrollView style={height:'100%'}>
{ this.data.list.map( ( data ) =>
if data.type == 'project'
this.renderList( data )
) }
</ScrollView>
</View>
export default ListTasks
Your data structure is not very optimal for storing data that clearly has a 2D sectional kind of relationships. I have modified the data structure a bit below.
DOM manipulation is also not the React way. Instead we use states to manage the visibility and properties of the elements.
A working example of a sectional collapsible list:
import React from 'react';
import { Text, TouchableOpacity, SectionList } from 'react-native';
const data = [
{
id: 1,
name: 'Project 1',
type: 'project',
data: [
{
id: 2,
name: 'Task 1',
type: 'task',
},
{
id: 3,
name: 'Task 2',
type: 'task',
},
],
},
{
id: 4,
name: 'Project 2',
type: 'project',
data: [
{
id: 5,
name: 'Task 3',
type: 'task',
},
{
id: 6,
name: 'Task 4',
type: 'task',
},
],
},
];
class ListTasks extends React.Component {
state = {
list: data,
show: {},
};
render() {
// SectionList is used instead of ScrollView as it has better performance
return (
<SectionList
style={{ marginTop: 50 }}
sections={this.state.list}
renderItem={({ item, section }) =>
// render null if show state for the section is falsy
(this.state.show[section.id] ? <Text style={{ padding: 10 }}>{item.name}</Text> : null)
}
renderSectionHeader={({ section }) => (
<TouchableOpacity
style={{ paddingVertical: 10, margin: 10, backgroundColor: 'grey' }}
onPress={() =>
this.setState((prevState) => ({
show: {
...prevState.show,
// toggle the show state for the seciton
[section.id]: !prevState.show[section.id],
},
}))
}
>
<Text>{section.name}</Text>
</TouchableOpacity>
)}
keyExtractor={(item) => item.id}
/>
);
}
}
export default ListTasks;