11import 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'
43import { BasicComponent , ComponentDefaults } from '@/utils/typings'
54import { getRectByTaro } from '@/utils/get-rect-by-taro'
65import { DragState } from './drag'
6+ import { getWindowInfo } from '@/utils/get-system-info'
7+ import { web } from '@/utils/platform-taro'
78
89export interface DragProps extends BasicComponent {
910 attract : boolean
@@ -43,7 +44,6 @@ export const Drag: FunctionComponent<
4344 children,
4445 className,
4546 style,
46- ...reset
4747 } = {
4848 ...defaultProps ,
4949 ...props ,
@@ -67,22 +67,22 @@ export const Drag: FunctionComponent<
6767 const { top, left, bottom, right } = boundary
6868 const { screenWidth, windowHeight } = getWindowInfo ( )
6969
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+ } )
8183
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
8686 }
8787 }
8888
@@ -123,7 +123,7 @@ export const Drag: FunctionComponent<
123123 }
124124 }
125125
126- const touchEnd = ( e : React . TouchEvent ) => {
126+ const touchEnd = ( ) => {
127127 onDragEnd ?.( {
128128 offset : [ translateX . current , translateY . current ] ,
129129 } )
@@ -141,34 +141,36 @@ export const Drag: FunctionComponent<
141141 }
142142
143143 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+ }
148153 return ( ) => {
149154 clearTimeout ( timer . current )
150155 }
151- } , [ ] )
156+ } , [ dragRef . current ] )
152157
153158 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
161161 className = { `${ classPrefix } -inner` }
162- onTouchStart = { ( event ) => touchStart ( event ) }
163162 ref = { dragRef }
163+ catchMove
164+ // @ts -ignore
165+ onTouchStart = { touchStart }
166+ // @ts -ignore
164167 onTouchMove = { touchMove }
165168 onTouchEnd = { touchEnd }
166- // eslint-disable-next-line react/no-unknown-property
167169 style = { currstyle }
168170 >
169171 { children }
170- </ div >
171- </ div >
172+ </ View >
173+ </ View >
172174 )
173175}
174176
0 commit comments