import React, { useLayoutEffect, useRef } from 'react'; import { Animated, Dimensions, TouchableOpacity, I18nManager, StyleSheet, Text, View, Pressable } from 'react-native'; import { Swipeable } from 'react-native-gesture-handler'; import type { SwipeableProps } from 'react-native-gesture-handler/lib/typescript/components/Swipeable'; import type { FCCWD, RenderRightActionProps, SwipeProps } from '../../types'; import { applyDefaults } from '../../core/KitraProvider'; const AnimatedTouchableOpacity = Animated.createAnimatedComponent(Pressable); const screenWith = Dimensions.get('screen').width; let swipeWidth: number; const Swipe: FCCWD = ({ children = , theme, typography, variant = 'no-radius', rightActions, leftAction = { onPress: () => {}, }, ...props }) => { const swipeRef = useRef(null); const isRadius = variant === 'radius'; useLayoutEffect(() => { if (rightActions) { const radiusValue = isRadius ? 5 + rightActions.length * 5 : 0; // @ts-expect-error swipeWidth = (rightActions?.reduce((accumulator, currentValue) => accumulator + (currentValue?.style?.width || (isRadius ? 50 : 82)), 0) || 0) + radiusValue; } }, [variant]); const renderLeftAction = ( _progress: any, dragX: any, ) => { const trans = dragX.interpolate({ inputRange: [0, screenWith], outputRange: [-20, screenWith / 2], }); return ( { leftAction?.onPress?.(swipeRef); close(); }}> {leftAction?.icon} {leftAction?.text} ); }; const renderRightAction = ({ item, dragValue, progress, index, }: RenderRightActionProps) => { const { text, style, icon, textStyle } = item; const trans = progress.interpolate({ inputRange: [0, 1], outputRange: [dragValue, 0], }); return ( item.onPress?.(swipeRef)} > {icon} {!!text && {text}} ); }; const renderRightActions = ( progress: any, _dragAnimatedValue: any, ) => ( {rightActions?.map((item, index) => { const dragValue = rightActions .slice(0, index * -1 + rightActions.length) // @ts-expect-error .reduce((accumulator, currentValue) => accumulator + (currentValue?.style?.width || (isRadius ? 50 : 82)), 0); return renderRightAction({ item, dragValue, progress, index }); })} ); const close = () => { swipeRef?.current?.close(); }; return ( {React.Children.map(children, item => React.cloneElement(item, { style: [{ backgroundColor: theme?.white, height: 82 }, item.props?.style, { borderRadius: isRadius ? 10 : 0, overflow: 'hidden' }], }))} ); }; export default applyDefaults(Swipe); const styles = StyleSheet.create({ leftAction: { flex: 1, justifyContent: 'center', }, actionText: { backgroundColor: 'transparent', }, leftActionContainer: { justifyContent: 'center', alignItems: 'center', alignSelf: 'baseline', }, rightAction: { alignItems: 'center', justifyContent: 'center', }, });