Skip to content

Instantly share code, notes, and snippets.

@madan712
Last active April 28, 2024 16:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madan712/4059e27253210ae8d3a059d6addadc8d to your computer and use it in GitHub Desktop.
Save madan712/4059e27253210ae8d3a059d6addadc8d to your computer and use it in GitHub Desktop.
React native draggable and swipeable list
import DraggableFlatList, { ScaleDecorator } from 'react-native-draggable-flatlist'
import SwipeableItem from 'react-native-swipeable-item'
.
.
.
const itemRefs = React.useRef(new Map())
<DraggableFlatList
activationDistance={15}
data={getCategoriesFromTaskList()}
renderItem={({ item, index, drag, isActive }) => {
<ScaleDecorator>
<SwipeableItem
key={props.cat.catId}
item={props.cat}
ref={(ref) => {
if (ref && !props.itemRefs.current.get(props.cat.catId)) {
props.itemRefs.current.set(props.cat.catId, ref);
}
}}
overSwipe={50}
renderUnderlayLeft={renderUnderlayLeft}
snapPointsLeft={[50]}
renderUnderlayRight={renderUnderlayRight}
snapPointsRight={[50]}
onChange={({ open }) => {
if (open) {
for (const [catId, ref] of props.itemRefs.current.entries()) {
if (catId !== props.cat.catId && ref) ref.close();
}
}
}}
>
<TouchableOpacity activeOpacity={0.6} onLongPress={props.drag} onPress={() => navigation.navigate('Task', { 'cat': props.cat })}>
<View style={[styles.list, (props.isActive ? { borderColor: '#000' } : { borderColor: '#808080' }), { backgroundColor: props.cat.color, justifyContent: 'center' }]}>
<Text style={styles.catfont}>{props.cat.catName + ' (' + props.cat.remainingCount + '/' + props.cat.totalCount + ')'}</Text>
</View>
</TouchableOpacity>
</SwipeableItem>
</ScaleDecorator>
}}
keyExtractor={item => item.catId.toString()}
onDragEnd={({ data }) => updateSequence(data)}
ListEmptyComponent={<Text style={[styles.center, styles.emptytext]}>EMPTY</Text>}
/>
@crashback-exe
Copy link

Thanks a lot! The official docs lack clarity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment