11import React , {
22 forwardRef ,
33 MouseEvent ,
4+ useCallback ,
45 useEffect ,
56 useImperativeHandle ,
7+ useMemo ,
68 useRef ,
79 useState ,
810} from 'react'
@@ -13,7 +15,6 @@ import { nextTick, useReady } from '@tarojs/taro'
1315import { useTouch } from '@/hooks/use-touch'
1416import { getRectInMultiPlatform } from '@/utils/taro/get-rect'
1517import { ComponentDefaults } from '@/utils/typings'
16- import { harmony } from '@/utils/taro/platform'
1718import { useRefState } from '@/hooks/use-ref-state'
1819import { useUuid } from '@/hooks/use-uuid'
1920import { PositionX , SwipeRef , TaroSwipeProps } from '@/types'
@@ -45,25 +46,23 @@ export const Swipe = forwardRef<
4546 const leftId = `swipe-left-${ uid } `
4647 const rightId = `swipe-right-${ uid } `
4748
49+ const getWidth = async ( ) => {
50+ if ( leftWrapper . current ) {
51+ const leftRect = await getRectInMultiPlatform ( leftWrapper . current , leftId )
52+ leftRect && setActionWidth ( ( v : any ) => ( { ...v , left : leftRect . width } ) )
53+ }
54+ if ( rightWrapper . current ) {
55+ const rightRect = await getRectInMultiPlatform (
56+ rightWrapper . current ,
57+ rightId
58+ )
59+ rightRect &&
60+ setActionWidth ( ( v : any ) => ( { ...v , right : rightRect . width } ) )
61+ }
62+ }
63+
4864 // 获取元素的时候要在页面 onReady 后,需要参考小程序的事件周期
4965 useReady ( ( ) => {
50- const getWidth = async ( ) => {
51- if ( leftWrapper . current ) {
52- const leftRect = await getRectInMultiPlatform (
53- leftWrapper . current ,
54- leftId
55- )
56- leftRect && setActionWidth ( ( v : any ) => ( { ...v , left : leftRect . width } ) )
57- }
58- if ( rightWrapper . current ) {
59- const rightRect = await getRectInMultiPlatform (
60- rightWrapper . current ,
61- rightId
62- )
63- rightRect &&
64- setActionWidth ( ( v : any ) => ( { ...v , right : rightRect . width } ) )
65- }
66- }
6766 nextTick ( ( ) => getWidth ( ) )
6867 } )
6968
@@ -78,7 +77,6 @@ export const Swipe = forwardRef<
7877 offset : 0 ,
7978 dragging : false ,
8079 } )
81-
8280 const [ actionWidth , updateState ] = useRefState ( {
8381 left : 0 ,
8482 right : 0 ,
@@ -98,57 +96,48 @@ export const Swipe = forwardRef<
9896 } )
9997 }
10098 }
101- const wrapperStyle = {
102- transform : `translate(${ state . offset } ${ ! harmony ( ) ? 'px' : '' } , 0)` ,
103- transitionDuration : state . dragging ? '0s' : '.6s' ,
104- }
105- const onTouchStart = async ( event : BaseEventOrig < HTMLDivElement > ) => {
106- if ( leftWrapper . current ) {
107- const leftRect = await getRectInMultiPlatform ( leftWrapper . current , leftId )
108- leftRect && setActionWidth ( ( v : any ) => ( { ...v , left : leftRect . width } ) )
109- }
110- if ( rightWrapper . current ) {
111- const rightRect = await getRectInMultiPlatform (
112- rightWrapper . current ,
113- rightId
114- )
115- rightRect &&
116- setActionWidth ( ( v : any ) => ( { ...v , right : rightRect . width } ) )
99+ const wrapperStyle = useMemo ( ( ) => {
100+ return {
101+ transform : `translate(${ state . offset } px, 0)` ,
102+ transitionDuration : state . dragging ? '0.01s' : '.6s' ,
117103 }
104+ } , [ state . offset , state . dragging ] )
105+
106+ const onTouchStart = async ( event : BaseEventOrig < HTMLDivElement > ) => {
118107 if ( ! props . disabled ) {
108+ getWidth ( )
119109 startOffset . current = state . offset
120110 touch . start ( event )
121111 props . onTouchStart ?.( event )
122112 }
123113 }
124114
125115 const onTouchMove = ( event : BaseEventOrig < HTMLDivElement > ) => {
126- if ( props . disabled ) {
127- return
128- }
129-
116+ if ( props . disabled ) return
130117 touch . move ( event )
131118 props . onTouchMove ?.( event )
132119 if ( touch . isHorizontal ( ) ) {
133120 lockClick . current = true
134- const newState = { ...state , dragging : true }
135121 const isEdge = ! opened || touch . deltaX . current * startOffset . current < 0
136122 if ( isEdge ) {
137123 preventDefault ( event , true )
138124 }
139-
140- newState . offset = rangeCalculation (
125+ const offset = rangeCalculation (
141126 touch . deltaX . current + startOffset . current ,
142127 - actionWidth . current . right || 0 ,
143128 actionWidth . current . left || 0
144129 )
145- setState ( newState )
130+ setState ( ( prevState ) => ( {
131+ ...prevState ,
132+ dragging : true ,
133+ offset : Number ( offset ) || 0 ,
134+ } ) )
146135 }
147136 }
148137
149138 const onTouchEnd = ( event : BaseEventOrig < HTMLDivElement > ) => {
150139 if ( state . dragging ) {
151- setState ( ( v ) => ( { ...v , dragging : false } ) )
140+ setState ( ( prevState ) => ( { ...prevState , dragging : false } ) )
152141 toggle ( state . offset > 0 ? 'left' : 'right' )
153142 setTimeout ( ( ) => {
154143 lockClick . current = false
@@ -175,20 +164,30 @@ export const Swipe = forwardRef<
175164 side === 'left' ? actionWidth . current . left : - actionWidth . current . right
176165 const name = props . name as number | string
177166 props . onOpen ?.( { name, position : side } )
178- setState ( ( v ) => ( { ...v , offset : Number ( offset ) || 0 } ) )
179- }
180167
181- const close = ( position ?: PositionX ) => {
182- if ( opened . current ) {
183- opened . current = false
184- props . onClose ?.( {
185- name : props . name as number | string ,
186- position : position || 'left' ,
187- } )
188- }
189- setState ( ( v ) => ( { ...v , offset : 0 } ) )
168+ setState ( ( prevState ) => ( {
169+ ...prevState ,
170+ offset : Number ( offset ) || 0 ,
171+ } ) )
190172 }
191173
174+ const close = useCallback (
175+ ( position ?: PositionX ) => {
176+ if ( opened . current ) {
177+ opened . current = false
178+ props . onClose ?.( {
179+ name : props . name as number | string ,
180+ position : position || 'left' ,
181+ } )
182+ }
183+ setState ( ( prevState ) => ( {
184+ ...prevState ,
185+ offset : 0 ,
186+ } ) )
187+ } ,
188+ [ props ]
189+ )
190+
192191 const rangeCalculation = (
193192 num : number | string ,
194193 min : number | string ,
@@ -232,8 +231,7 @@ export const Swipe = forwardRef<
232231 } ) )
233232
234233 useEffect ( ( ) => {
235- if ( harmony ( ) ) return
236-
234+ // 并没有生效
237235 const handler : any = ( event : { target : Node | null } ) => {
238236 const targets = [ root ]
239237 if (
@@ -248,11 +246,10 @@ export const Swipe = forwardRef<
248246 }
249247
250248 document . addEventListener ( 'touchstart' , handler )
251-
252249 return ( ) => {
253250 document . removeEventListener ( 'touchstart' , handler )
254251 }
255- } , [ ] )
252+ } , [ close ] )
256253
257254 return (
258255 < View
0 commit comments