Skip to content

Commit 98fa4a0

Browse files
committed
fix: 解决Drag组件拖拽后会在原地留一个遮挡元素问题+解决weapp/taroh5多个demo拖拽位置不正确问题
1 parent ad7c890 commit 98fa4a0

File tree

8 files changed

+31
-10
lines changed

8 files changed

+31
-10
lines changed

src/packages/drag/demo.taro.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const DragDemo = () => {
3535
return (
3636
<>
3737
<Header />
38-
<div className={`demo full ${isTaroWeb ? 'web' : ''}`}>
38+
<div className={`demo ${isTaroWeb ? 'web' : 'full'}`}>
3939
<h2>{translated.basic}</h2>
4040
<Demo1 />
4141

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Drag, Button } from '@nutui/nutui-react-taro'
33

44
const Demo1 = () => {
55
return (
6-
<Drag style={{ left: '8px' }}>
6+
<Drag style={{ left: '8px' }} className="drag-demo1">
77
<Button type="primary">drag</Button>
88
</Drag>
99
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Demo2 = () => {
1010
top: '200px',
1111
left: '8px',
1212
}}
13+
className="drag-demo21"
1314
>
1415
<Button type="primary">X</Button>
1516
</Drag>
@@ -19,6 +20,7 @@ const Demo2 = () => {
1920
top: '200px',
2021
right: '50px',
2122
}}
23+
className="drag-demo22"
2224
>
2325
<Button type="primary">Y</Button>
2426
</Drag>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Demo3 = () => {
1010
top: '275px',
1111
left: '0px',
1212
}}
13+
className="drag-demo3"
1314
>
1415
<Button type="primary">attract</Button>
1516
</Drag>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Demo4 = () => {
2727
}}
2828
/>
2929
<Drag
30+
className="drag-demo4"
3031
boundary={{ top: 361, left: 9, bottom: bottom(), right: right() }}
3132
style={{ top: '400px', left: '50px' }}
3233
>

src/packages/drag/drag.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
.nut-drag {
22
position: fixed;
3-
display: inline-flex;
43
z-index: 9997 !important;
5-
width: fit-content;
6-
height: fit-content;
4+
width: 0;
5+
height: 0;
76
touch-action: none;
87
user-select: none;
98
font-size: 0;
9+
&-inner {
10+
display: inline-flex;
11+
width: fit-content;
12+
height: fit-content;
13+
}
1014
}

src/packages/drag/drag.taro.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { FunctionComponent, useState, useEffect, useRef } from 'react'
22
import { getSystemInfoSync, createSelectorQuery } from '@tarojs/taro'
33
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
4+
import { getRectByTaro } from '@/utils/get-rect-by-taro'
45

56
export interface DragProps extends BasicComponent {
67
attract: boolean
@@ -45,20 +46,24 @@ export const Drag: FunctionComponent<
4546
const translateY = useRef(0)
4647
const middleLine = useRef(0)
4748

48-
const getInfo = () => {
49+
const getInfo = async () => {
4950
const el = myDrag.current
5051
if (el) {
5152
const { top, left, bottom, right } = boundary
5253
const { screenWidth, windowHeight } = getSystemInfoSync()
5354

55+
const { width, height } = await getRectByTaro(dragRef.current)
56+
dragRef.current && dragRef.current.getBoundingClientRect()
57+
58+
console.log('width', width, 'height', height)
5459
createSelectorQuery()
5560
.select(`.${className}`)
5661
.boundingClientRect((rec: any) => {
5762
setBoundaryState({
5863
top: -rec.top + top,
5964
left: -rec.left + left,
60-
bottom: windowHeight - rec.height - rec.top - bottom,
61-
right: screenWidth - rec.width - rec.left - right,
65+
bottom: windowHeight - rec.top - bottom - Math.ceil(height),
66+
right: screenWidth - rec.left - right - Math.ceil(width),
6267
})
6368

6469
middleLine.current =
@@ -135,6 +140,7 @@ export const Drag: FunctionComponent<
135140
ref={myDrag}
136141
>
137142
<div
143+
className={`${classPrefix}-inner`}
138144
onTouchStart={(event) => touchStart(event)}
139145
ref={dragRef}
140146
onTouchMove={touchMove}

src/packages/drag/drag.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ export const Drag: FunctionComponent<
4545
const getInfo = () => {
4646
const el = myDrag.current
4747
if (el) {
48-
const { offsetWidth, offsetHeight, offsetTop, offsetLeft } = el
48+
const { offsetTop, offsetLeft } = el
49+
const { offsetWidth, offsetHeight } = el.querySelector(
50+
`.${classPrefix}-inner`
51+
) as HTMLDivElement
4952
const { clientWidth, clientHeight } = document.documentElement
5053
const { top, left, bottom, right } = boundary
5154
setBoundaryState({
@@ -94,7 +97,11 @@ export const Drag: FunctionComponent<
9497
{...reset}
9598
ref={myDrag}
9699
>
97-
<animated.div style={currstyle} {...bind()}>
100+
<animated.div
101+
style={currstyle}
102+
{...bind()}
103+
className={`${classPrefix}-inner`}
104+
>
98105
{children}
99106
</animated.div>
100107
</div>

0 commit comments

Comments
 (0)