import React, {Component, useRef} from 'react';
import {
Animated,
StyleSheet,
Text,
View,
I18nManager,
TouchableOpacity,
Pressable,
ViewStyle,
TextStyle,
} from 'react-native';
import {RectButton, Swipeable} from 'react-native-gesture-handler';
import {ActionButton, SwipeActionsButtonProps} from './interfaces';
import styles from './swipe-actions-button.styles';
const SwipeActionsButton = ({
leftAction,
rightActions,
leftThreshold = 150,
rightThreshold = 40,
rightActionsTotalWidthInPrecentages = '50%',
onPressButton,
onEndSwipeLeft,
style,
...props
}: SwipeActionsButtonProps) => {
let swipeableRow = useRef();
const renderLeftActions = (progress, dragX) => {
return (
{
leftAction.onPress && leftAction.onPress();
close();
}}>
{leftAction.name}
);
};
const renderRightAction = ({
text,
onPress,
x,
progress,
buttonStyle,
textStyle,
}: {
text?: string;
backColor?: string;
textColor?: string;
onPress?: () => void;
x: any;
progress: any;
buttonStyle?: ViewStyle;
textStyle: TextStyle;
}) => {
const trans = progress.interpolate({
inputRange: [0, 1],
outputRange: [x, 0],
});
return (
{
onPress && onPress();
close();
}}>
{text}
);
};
const renderRightActions = progress => (
{rightActions.map((action: ActionButton) =>
renderRightAction({
text: action.name,
onPress: action.onPress,
x: 0,
progress,
buttonStyle: action.buttonStyle,
textStyle: action.textStyle,
}),
)}
);
const updateRef = ref => {
swipeableRow = ref;
};
const close = () => {
swipeableRow.close();
};
return (
{
onPressButton && onPressButton();
}}>
{
if (direction === 'left') {
onEndSwipeLeft && onEndSwipeLeft();
}
}}
renderLeftActions={renderLeftActions}
renderRightActions={renderRightActions}>
{props.children}
);
};
export default SwipeActionsButton;