1
1
import React , { FunctionComponent , useState , useEffect , useRef } from 'react'
2
- import { createSelectorQuery } from '@tarojs/taro'
3
- import { getWindowInfo } from '@/utils/get-system-info'
2
+ import { View } from '@tarojs/components'
4
3
import { BasicComponent , ComponentDefaults } from '@/utils/typings'
5
4
import { getRectByTaro } from '@/utils/get-rect-by-taro'
6
5
import { DragState } from './drag'
6
+ import { getWindowInfo } from '@/utils/get-system-info'
7
+ import { web } from '@/utils/platform-taro'
7
8
8
9
export interface DragProps extends BasicComponent {
9
10
attract : boolean
@@ -43,7 +44,6 @@ export const Drag: FunctionComponent<
43
44
children,
44
45
className,
45
46
style,
46
- ...reset
47
47
} = {
48
48
...defaultProps ,
49
49
...props ,
@@ -67,22 +67,22 @@ export const Drag: FunctionComponent<
67
67
const { top, left, bottom, right } = boundary
68
68
const { screenWidth, windowHeight } = getWindowInfo ( )
69
69
70
- const { width, height } = await getRectByTaro ( dragRef . current )
71
- dragRef . current ?. getBoundingClientRect ( )
72
- createSelectorQuery ( )
73
- . select ( `.${ className } ` )
74
- . boundingClientRect ( ( rec : any ) => {
75
- setBoundaryState ( {
76
- top : - rec . top + top ,
77
- left : - rec . left + left ,
78
- bottom : windowHeight - rec . top - bottom - Math . ceil ( height ) ,
79
- right : screenWidth - rec . left - right - Math . ceil ( width ) ,
80
- } )
70
+ const {
71
+ width,
72
+ height,
73
+ top : dragTop ,
74
+ left : dragLeft ,
75
+ } = await getRectByTaro ( dragRef . current )
76
+
77
+ setBoundaryState ( {
78
+ top : - dragTop + top ,
79
+ left : - dragLeft + left ,
80
+ bottom : windowHeight - dragTop - bottom - Math . ceil ( height ) ,
81
+ right : screenWidth - dragLeft - right - Math . ceil ( width ) ,
82
+ } )
81
83
82
- middleLine . current =
83
- screenWidth - rec . width - rec . left - ( screenWidth - rec . width ) / 2
84
- } )
85
- . exec ( )
84
+ middleLine . current =
85
+ screenWidth - width - dragLeft - ( screenWidth - width ) / 2
86
86
}
87
87
}
88
88
@@ -123,7 +123,7 @@ export const Drag: FunctionComponent<
123
123
}
124
124
}
125
125
126
- const touchEnd = ( e : React . TouchEvent ) => {
126
+ const touchEnd = ( ) => {
127
127
onDragEnd ?.( {
128
128
offset : [ translateX . current , translateY . current ] ,
129
129
} )
@@ -141,34 +141,36 @@ export const Drag: FunctionComponent<
141
141
}
142
142
143
143
useEffect ( ( ) => {
144
- timer . current = window . setTimeout ( ( ) => {
145
- getInfo ( )
146
- } , 300 )
147
-
144
+ if ( dragRef . current ) {
145
+ if ( web ( ) ) {
146
+ timer . current = window . setTimeout ( ( ) => {
147
+ getInfo ( )
148
+ } , 300 )
149
+ } else {
150
+ getInfo ( )
151
+ }
152
+ }
148
153
return ( ) => {
149
154
clearTimeout ( timer . current )
150
155
}
151
- } , [ ] )
156
+ } , [ dragRef . current ] )
152
157
153
158
return (
154
- < div
155
- style = { style }
156
- className = { `${ classPrefix } ${ className } ` }
157
- { ...reset }
158
- ref = { myDrag }
159
- >
160
- < div
159
+ < View style = { style } className = { `${ classPrefix } ${ className } ` } ref = { myDrag } >
160
+ < View
161
161
className = { `${ classPrefix } -inner` }
162
- onTouchStart = { ( event ) => touchStart ( event ) }
163
162
ref = { dragRef }
163
+ catchMove
164
+ // @ts -ignore
165
+ onTouchStart = { touchStart }
166
+ // @ts -ignore
164
167
onTouchMove = { touchMove }
165
168
onTouchEnd = { touchEnd }
166
- // eslint-disable-next-line react/no-unknown-property
167
169
style = { currstyle }
168
170
>
169
171
{ children }
170
- </ div >
171
- </ div >
172
+ </ View >
173
+ </ View >
172
174
)
173
175
}
174
176
0 commit comments