11import React , { useImperativeHandle , forwardRef , useRef } from 'react' ;
2- import { Animated , StyleSheet , View , Text , I18nManager , StyleProp , ViewStyle } from 'react-native' ;
2+ import { Animated , StyleSheet , View , Text , I18nManager } from 'react-native' ;
33import { RectButton } from 'react-native-gesture-handler' ;
4- import Swipeable from 'react-native-gesture-handler/Swipeable' ;
4+ import Swipeable , { SwipeableProps } from 'react-native-gesture-handler/Swipeable' ;
55
66export interface Column {
77 /** 显示文字 */
@@ -16,49 +16,31 @@ export interface Column {
1616 render ?: ( text : string , record : Column , index : number ) => React . ReactNode ;
1717}
1818
19- export interface SwipeActionProps {
19+ export interface SwipeActionProps extends SwipeableProps {
2020 /** 右边滑动出来的元素 */
2121 right ?: Array < Column > ;
2222 /** 左边滑动出来的元素 */
2323 left ?: Array < Column > ;
2424 /** 按钮宽度 默认60 */
2525 buttonWidth ?: number ;
26- enableTrackpadTwoFingerGesture ?: boolean ;
27- friction ?: number ;
28- leftThreshold ?: number ;
29- rightThreshold ?: number ;
30- overshootLeft ?: boolean ;
31- overshootRight ?: boolean ;
32- overshootFriction ?: number ;
33- onSwipeableLeftOpen ?: ( ) => void ;
34- onSwipeableRightOpen ?: ( ) => void ;
35- onSwipeableOpen ?: ( ) => void ;
36- onSwipeableClose ?: ( ) => void ;
37- onSwipeableLeftWillOpen ?: ( ) => void ;
38- onSwipeableRightWillOpen ?: ( ) => void ;
39- onSwipeableWillOpen ?: ( ) => void ;
40- onSwipeableWillClose ?: ( ) => void ;
4126 children ?: React . ReactNode ;
42- renderLeftActions ?: (
43- progressAnimatedValue : Animated . AnimatedInterpolation ,
44- dragAnimatedValue : Animated . AnimatedInterpolation ,
45- ) => React . ReactNode ;
46- renderRightActions ?: (
47- progressAnimatedValue : Animated . AnimatedInterpolation ,
48- dragAnimatedValue : Animated . AnimatedInterpolation ,
49- ) => React . ReactNode ;
50- useNativeAnimations ?: boolean ;
51- animationOptions ?: Record < string , unknown > ;
52- containerStyle ?: StyleProp < ViewStyle > ;
53- childrenContainerStyle ?: StyleProp < ViewStyle > ;
5427}
5528
56- const SwipeAction = ( props : SwipeActionProps , ref : any ) => {
57- const { children, right = [ ] , left = [ ] , buttonWidth = 60 , ...others } = props ;
29+ const SwipeAction = (
30+ props : SwipeActionProps ,
31+ ref : React . ForwardedRef < Partial < React . LegacyRef < Swipeable > > | undefined | null > ,
32+ ) => {
33+ const { right = [ ] , left = [ ] , buttonWidth = 60 , children, ...others } = props ;
5834 const swipeableRef : React . MutableRefObject < null > = useRef ( null ) ;
5935
36+ useImperativeHandle ( ref , ( ) => ( { swipeable : swipeableRef . current } ) ) ;
37+
6038 // 滑出
61- const renderRightAction = ( progress : Animated . AnimatedInterpolation , dragX : any , isLeft = true ) => {
39+ const renderRightAction = (
40+ progress : Animated . AnimatedInterpolation ,
41+ dragX : Animated . AnimatedInterpolation ,
42+ isLeft = true ,
43+ ) => {
6244 const buttons = isLeft ? left : right ;
6345 if ( ! buttons ) {
6446 return null ;
@@ -100,25 +82,18 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
10082 ) ;
10183 } ;
10284
103- // 暴露给父组件调用 Swipeable上的方法
104- useImperativeHandle ( ref , ( ) => ( {
105- swipeable : swipeableRef . current ,
106- } ) ) ;
107-
108- return (
109- < Swipeable
110- ref = { swipeableRef }
111- friction = { 2 }
112- enableTrackpadTwoFingerGesture
113- leftThreshold = { 30 }
114- rightThreshold = { 40 }
115- renderRightActions = { ( progress , dragX ) => renderRightAction ( progress , dragX , false ) }
116- renderLeftActions = { ( progress , dragX ) => renderRightAction ( progress , dragX , true ) }
117- { ...others }
118- >
119- { children && children }
120- </ Swipeable >
121- ) ;
85+ const commonProps : Omit < Partial < SwipeActionProps > , 'right' | 'left' | 'buttonWidth' | 'children' > & {
86+ ref ?: React . LegacyRef < Swipeable > ;
87+ } = {
88+ ref : swipeableRef ,
89+ friction : 2 ,
90+ enableTrackpadTwoFingerGesture : true ,
91+ rightThreshold : 40 ,
92+ renderRightActions : ( progress , dragX ) => renderRightAction ( progress , dragX , false ) ,
93+ renderLeftActions : ( progress , dragX ) => renderRightAction ( progress , dragX , true ) ,
94+ ...others ,
95+ } ;
96+ return < Swipeable { ...commonProps } > { children && children } </ Swipeable > ;
12297} ;
12398
12499const styles = StyleSheet . create ( {
0 commit comments