Skip to content

Commit 2690af4

Browse files
authored
fix(Drag): 解决taro不能滑动问题 (#3054)
1 parent 3d467b1 commit 2690af4

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

packages/nutui-taro-demo/src/app.scss

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ body {
2626

2727
&.full {
2828
padding: 0 0 0 0;
29-
3029
.h2 {
3130
padding-left: 17px;
3231
}
3332
}
3433

3534
&.drag-full {
3635
padding: 57px 0 0 0;
36+
min-height: 100vh;
37+
.jd-scroll-view {
38+
min-height: calc(100vh - 57px);
39+
}
3740
}
3841

3942
&-full {

src/packages/drag/demos/taro/demo2.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const Demo2 = () => {
1010
top: '200px',
1111
left: '8px',
1212
}}
13-
className="drag-demo21"
1413
>
1514
<Button type="primary">X</Button>
1615
</Drag>
@@ -20,7 +19,6 @@ const Demo2 = () => {
2019
top: '200px',
2120
right: '50px',
2221
}}
23-
className="drag-demo22"
2422
>
2523
<Button type="primary">Y</Button>
2624
</Drag>

src/packages/drag/demos/taro/demo3.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const Demo3 = () => {
1010
top: '275px',
1111
left: '0px',
1212
}}
13-
className="drag-demo3"
1413
>
1514
<Button type="primary">attract</Button>
1615
</Drag>

src/packages/drag/demos/taro/demo4.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@ const Demo4 = () => {
1111
return screenWidth - 300 - 9
1212
}
1313
const bottom = () => {
14-
return windowHeight - 501 - 57
14+
return windowHeight - 559
1515
}
1616
return (
1717
<>
1818
<div
1919
className="drag-boundary"
2020
style={{
21-
position: 'absolute',
21+
position: 'fixed',
2222
top: '360px',
2323
left: '8px',
2424
width: '300px',
2525
height: '200px',
2626
border: '1px solid var(--nutui-color-primary)',
27+
boxSizing: 'border-box',
2728
}}
2829
/>
2930
<Drag
30-
className="drag-demo4"
31-
boundary={{ top: 361, left: 9, bottom: bottom(), right: right() }}
31+
boundary={{
32+
top: 361,
33+
left: 9,
34+
bottom: bottom(),
35+
right: right(),
36+
}}
3237
style={{ top: '400px', left: '50px' }}
3338
>
3439
<Button type="primary">boundary</Button>

src/packages/drag/drag.scss

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
display: inline-flex;
1111
width: fit-content;
1212
height: fit-content;
13+
touch-action: none;
1314
}
1415
}

src/packages/drag/drag.taro.tsx

+37-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
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'
43
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
54
import { getRectByTaro } from '@/utils/get-rect-by-taro'
65
import { DragState } from './drag'
6+
import { getWindowInfo } from '@/utils/get-system-info'
7+
import { web } from '@/utils/platform-taro'
78

89
export 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

Comments
 (0)