@@ -95,27 +95,69 @@ Component(
9595 transformOptions ( {
9696 props : SwipeActionDefaultProps ,
9797 didMount ( ) {
98- const { defaultSwiped, elasticity } = this . getProps ( ) ;
99- this . setButtonItemWidth ( ) ;
98+ const { defaultSwiped, swiped, elasticity } = this . getProps ( ) ;
10099 this . setData ( { inertiaWidth : ! isOldVersion && elasticity ? 20 : 0 } ) ;
101- if ( defaultSwiped ) {
102- this . initWidth ( ( maxSwipe : any ) => {
103- maxSwipe &&
104- this . setData ( {
105- swipeX : ( maxSwipe + 0.01 ) * ( defaultSwiped === 'right' ? - 1 : 1 ) ,
106- swipedR : defaultSwiped === 'right' ,
107- swipedL : defaultSwiped === 'left' ,
108- } ) ;
109- } ) ;
100+
101+ // 优先使用 swiped 属性(受控),其次使用 defaultSwiped(非受控)
102+ const initialSwiped = swiped || defaultSwiped ;
103+
104+ // 先设置按钮宽度,然后在回调中处理初始展开状态
105+ this . setButtonItemWidth ( ) ;
106+
107+ if ( initialSwiped ) {
108+ // 需要等待按钮宽度设置完成后再初始化展开状态
109+ setTimeout ( ( ) => {
110+ this . initWidth ( ( maxSwipe : any ) => {
111+ if ( maxSwipe ) {
112+ const direction = initialSwiped === 'left' ? 'left' : 'right' ;
113+ this . setData ( {
114+ swipeX : ( maxSwipe + 0.01 ) * ( direction === 'right' ? - 1 : 1 ) ,
115+ swipedR : direction === 'right' ,
116+ swipedL : direction === 'left' ,
117+ } ) ;
118+ }
119+ } ) ;
120+ } , 50 ) ;
110121 }
111122 } ,
112123 didUpdate ( prevProp ) {
113124 const { swiped, damping, elasticity } = this . getProps ( ) ;
114125 // 设置不同的滑动位置时需要重置
115- const rs = prevProp . swiped !== swiped && ! swiped ;
126+ const swipedChanged = prevProp . swiped !== swiped ;
116127 const is = prevProp . elasticity !== elasticity ;
117128 const ds = prevProp . damping !== damping ;
118- if ( rs || is || ds ) {
129+
130+ // 处理 swiped 变化
131+ if ( swipedChanged ) {
132+ if ( ! swiped ) {
133+ // swiped 变为 false/空,需要关闭
134+ this . setData ( {
135+ swipeX : 0 ,
136+ swipedR : false ,
137+ swipedL : false ,
138+ tapTypeL : '' ,
139+ tapTypeR : '' ,
140+ } ) ;
141+ } else {
142+ // swiped 变为 true/'left'/'right',需要打开
143+ // 需要等待下一个事件循环,确保按钮宽度已经渲染
144+ setTimeout ( ( ) => {
145+ this . initWidth ( ( maxSwipe : any ) => {
146+ if ( maxSwipe ) {
147+ const direction = swiped === 'left' ? 'left' : 'right' ;
148+ this . setData ( {
149+ swipeX : ( maxSwipe + 0.01 ) * ( direction === 'right' ? - 1 : 1 ) ,
150+ swipedR : direction === 'right' ,
151+ swipedL : direction === 'left' ,
152+ tapTypeL : '' ,
153+ tapTypeR : '' ,
154+ } ) ;
155+ }
156+ } ) ;
157+ } , 50 ) ;
158+ }
159+ } else if ( is || ds ) {
160+ // 只有 elasticity 或 damping 变化时,才需要重置
119161 this . setData ( {
120162 swipeX : 0 ,
121163 swipedR : false ,
@@ -124,6 +166,7 @@ Component(
124166 tapTypeR : '' ,
125167 } ) ;
126168 }
169+
127170 if ( is ) {
128171 this . setData ( { inertiaWidth : elasticity ? 20 : 0 } ) ;
129172 }
0 commit comments