Last active
July 5, 2020 20:20
-
-
Save bakialmaci/a088fd39a468df6098648d7907f63db0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Animated, StyleSheet, Text, View, I18nManager } from 'react-native'; | |
import { RectButton } from 'react-native-gesture-handler'; | |
import Swipeable from 'react-native-gesture-handler/Swipeable'; | |
export default function AppleStyleSwipeableRow (props) { | |
const renderLeftActions = (progress, dragX) => { | |
const trans = dragX.interpolate({ | |
inputRange: [0, 50, 100, 101], | |
outputRange: [-20, 0, 0, 1], | |
}); | |
return ( | |
<RectButton style={styles.leftAction}> | |
<Animated.Text | |
style={[ | |
styles.actionText, | |
{ | |
transform: [{ translateX: trans }], | |
}, | |
]}> | |
Archive | |
</Animated.Text> | |
</RectButton> | |
); | |
}; | |
const renderRightAction = (text, color, x, progress) => { | |
const trans = progress.interpolate({ | |
inputRange: [0, 1], | |
outputRange: [x, 0], | |
}); | |
const pressHandler = () => { | |
alert(text); | |
}; | |
return ( | |
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}> | |
<RectButton | |
style={[styles.rightAction, { backgroundColor: color }]} | |
onPress={pressHandler}> | |
<Text style={styles.actionText}>{text}</Text> | |
</RectButton> | |
</Animated.View> | |
); | |
}; | |
const renderRightActions = progress => ( | |
<View style={{ width: 192, flexDirection: I18nManager.isRTL? 'row-reverse' : 'row' }}> | |
{renderRightAction('More', '#C8C7CD', 192, progress)} | |
{renderRightAction('Flag', '#ffab00', 128, progress)} | |
{renderRightAction('More', '#dd2c00', 64, progress)} | |
</View> | |
); | |
const { children } = props; | |
return ( | |
<Swipeable | |
friction={2} | |
leftThreshold={30} | |
rightThreshold={40} | |
renderLeftActions={renderLeftActions} | |
renderRightActions={renderRightActions}> | |
{children} | |
</Swipeable> | |
); | |
} | |
const styles = StyleSheet.create({ | |
leftAction: { | |
flex: 1, | |
backgroundColor: '#497AFC', | |
justifyContent: 'center', | |
}, | |
actionText: { | |
color: 'white', | |
fontSize: 16, | |
backgroundColor: 'transparent', | |
padding: 10, | |
}, | |
rightAction: { | |
alignItems: 'center', | |
flex: 1, | |
justifyContent: 'center', | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment