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' ;
3
3
import { RectButton } from 'react-native-gesture-handler' ;
4
4
import Swipeable from 'react-native-gesture-handler/Swipeable' ;
5
5
@@ -12,8 +12,6 @@ export interface Column {
12
12
x ?: number ;
13
13
/** 点击元素触发 */
14
14
onPress ?: ( ) => void ;
15
- /** 宽度 */
16
- width ?: number | string ;
17
15
/** 自定义元素 */
18
16
render ?: ( text : string , record : Column , index : number ) => React . ReactNode ;
19
17
}
@@ -58,24 +56,25 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
58
56
const { children, right = [ ] , left = [ ] , swipeWidth = '20%' , ...others } = props ;
59
57
const swipeableRef : React . MutableRefObject < null > = useRef ( null ) ;
60
58
59
+ const renderRight = useCallback ( ( ) => {
60
+ return renderRightAction ;
61
+ } , [ right , swipeWidth ] ) ;
62
+
63
+ const renderLeft = useCallback ( ( ) => {
64
+ return renderLeftAction ;
65
+ } , [ left , swipeWidth ] ) ;
66
+
61
67
// 右侧滑出
62
68
const renderRightAction = ( progress : Animated . AnimatedInterpolation ) => {
63
69
return (
64
70
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 ) => {
67
72
const trans = progress . interpolate ( {
68
73
inputRange : [ 0 , 1 ] ,
69
74
outputRange : [ x , 0 ] ,
70
75
} ) ;
71
76
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 } ] } >
79
78
< Animated . View style = { { flex : 1 , transform : [ { translateX : trans } ] } } >
80
79
< RectButton
81
80
style = { [ styles . rightAction , { backgroundColor : color } ] }
@@ -95,22 +94,15 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
95
94
const renderLeftAction = ( progress : Animated . AnimatedInterpolation , dragX : any ) => {
96
95
return (
97
96
left &&
98
- left . length > 0 &&
99
- left . map ( ( { text, color, onPress, width, render } , idx ) => {
97
+ left . map ( ( { text, color, onPress, render } , idx ) => {
100
98
const trans = dragX . interpolate ( {
101
99
inputRange : [ 0 , 50 , 100 , 101 ] ,
102
100
outputRange : [ - 20 , 0 , 0 , 1 ] ,
103
101
extrapolate : 'clamp' ,
104
102
} ) ;
105
103
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 } ] } ] } >
114
106
< RectButton
115
107
style = { [ styles . rightAction , { backgroundColor : color } ] }
116
108
onPress = { ( ) => {
@@ -139,8 +131,8 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
139
131
rightThreshold = { 50 }
140
132
leftThreshold = { 50 }
141
133
overshootRight = { false }
142
- renderRightActions = { renderRightAction }
143
- renderLeftActions = { renderLeftAction }
134
+ renderRightActions = { renderRight ( ) }
135
+ renderLeftActions = { renderLeft ( ) }
144
136
{ ...others }
145
137
>
146
138
{ children && children }
@@ -164,6 +156,9 @@ const styles = StyleSheet.create({
164
156
flex : 1 ,
165
157
justifyContent : 'center' ,
166
158
} ,
159
+ viewActions : {
160
+ flexDirection : I18nManager . isRTL ? 'row-reverse' : 'row' ,
161
+ } ,
167
162
} ) ;
168
163
169
164
export default forwardRef ( SwipeAction ) ;
0 commit comments