1- import React , { useImperativeHandle , forwardRef , useRef } from 'react' ;
2- import { Animated , StyleSheet , View , Text , I18nManager , StyleProp , ViewStyle , Dimensions } from 'react-native' ;
1+ import React , { useImperativeHandle , forwardRef , useRef , useCallback } from 'react' ;
2+ import { Animated , StyleSheet , View , Text , I18nManager , StyleProp , ViewStyle } from 'react-native' ;
33import { RectButton } from 'react-native-gesture-handler' ;
44import Swipeable from 'react-native-gesture-handler/Swipeable' ;
55
@@ -12,8 +12,6 @@ export interface Column {
1212 x ?: number ;
1313 /** 点击元素触发 */
1414 onPress ?: ( ) => void ;
15- /** 宽度 */
16- width ?: number | string ;
1715 /** 自定义元素 */
1816 render ?: ( text : string , record : Column , index : number ) => React . ReactNode ;
1917}
@@ -58,24 +56,25 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
5856 const { children, right = [ ] , left = [ ] , swipeWidth = '20%' , ...others } = props ;
5957 const swipeableRef : React . MutableRefObject < null > = useRef ( null ) ;
6058
59+ const renderRight = useCallback ( ( ) => {
60+ return renderRightAction ;
61+ } , [ right , swipeWidth ] ) ;
62+
63+ const renderLeft = useCallback ( ( ) => {
64+ return renderLeftAction ;
65+ } , [ left , swipeWidth ] ) ;
66+
6167 // 右侧滑出
6268 const renderRightAction = ( progress : Animated . AnimatedInterpolation ) => {
6369 return (
6470 right &&
65- right . length > 0 &&
66- right . map ( ( { x = 1 , text, color, onPress, width = '20%' , render } , idx ) => {
71+ right . map ( ( { x = 1 , text, color, onPress, render } , idx ) => {
6772 const trans = progress . interpolate ( {
6873 inputRange : [ 0 , 1 ] ,
6974 outputRange : [ x , 0 ] ,
7075 } ) ;
7176 return (
72- < View
73- key = { idx }
74- style = { {
75- width : width ,
76- flexDirection : I18nManager . isRTL ? 'row-reverse' : 'row' ,
77- } }
78- >
77+ < View key = { idx } style = { [ styles . viewActions , { width : swipeWidth } ] } >
7978 < Animated . View style = { { flex : 1 , transform : [ { translateX : trans } ] } } >
8079 < RectButton
8180 style = { [ styles . rightAction , { backgroundColor : color } ] }
@@ -95,22 +94,15 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
9594 const renderLeftAction = ( progress : Animated . AnimatedInterpolation , dragX : any ) => {
9695 return (
9796 left &&
98- left . length > 0 &&
99- left . map ( ( { text, color, onPress, width, render } , idx ) => {
97+ left . map ( ( { text, color, onPress, render } , idx ) => {
10098 const trans = dragX . interpolate ( {
10199 inputRange : [ 0 , 50 , 100 , 101 ] ,
102100 outputRange : [ - 20 , 0 , 0 , 1 ] ,
103101 extrapolate : 'clamp' ,
104102 } ) ;
105103 return (
106- < View
107- style = { {
108- width : swipeWidth ,
109- flexDirection : I18nManager . isRTL ? 'row-reverse' : 'row' ,
110- } }
111- key = { idx }
112- >
113- < Animated . View style = { { flex : 1 , transform : [ { translateX : trans } ] } } >
104+ < View style = { [ styles . viewActions , { width : swipeWidth } ] } key = { idx } >
105+ < Animated . View style = { [ { flex : 1 , transform : [ { translateX : trans } ] } ] } >
114106 < RectButton
115107 style = { [ styles . rightAction , { backgroundColor : color } ] }
116108 onPress = { ( ) => {
@@ -139,8 +131,8 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
139131 rightThreshold = { 50 }
140132 leftThreshold = { 50 }
141133 overshootRight = { false }
142- renderRightActions = { renderRightAction }
143- renderLeftActions = { renderLeftAction }
134+ renderRightActions = { renderRight ( ) }
135+ renderLeftActions = { renderLeft ( ) }
144136 { ...others }
145137 >
146138 { children && children }
@@ -164,6 +156,9 @@ const styles = StyleSheet.create({
164156 flex : 1 ,
165157 justifyContent : 'center' ,
166158 } ,
159+ viewActions : {
160+ flexDirection : I18nManager . isRTL ? 'row-reverse' : 'row' ,
161+ } ,
167162} ) ;
168163
169164export default forwardRef ( SwipeAction ) ;
0 commit comments