Skip to content

Commit

Permalink
fix: swipeCell组件垂直方向拖拽问题、开关操作交互优化(#438)、移除customWrapper(#247
Browse files Browse the repository at this point in the history
  • Loading branch information
kongjing@dian.so authored and kongjing@dian.so committed Dec 23, 2022
1 parent b654bda commit 3414f7d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
12 changes: 6 additions & 6 deletions packages/vantui-doc/src/swipe-cell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { SwipeCell } from '@antmjs/vantui'
function Demo() {
return (
<SwipeCell
rightWidth={65}
leftWidth={65}
rightWidth={75}
leftWidth={75}
renderLeft={<Button>选择</Button>}
renderRight={<Button>删除</Button>}
>
Expand Down Expand Up @@ -49,8 +49,8 @@ function Demo() {
return (
<SwipeCell
ref={it}
rightWidth={65}
leftWidth={65}
rightWidth={75}
leftWidth={75}
asyncClose
onClose={closeAction}
renderLeft={<Button>选择</Button>}
Expand All @@ -77,8 +77,8 @@ function Demo() {
return (
<SwipeCell
ref={it}
rightWidth={65}
leftWidth={65}
rightWidth={75}
leftWidth={75}
asyncClose
renderLeft={<Button>选择</Button>}
renderRight={<Button>删除</Button>}
Expand Down
86 changes: 65 additions & 21 deletions packages/vantui/src/swipe-cell/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { ITouchEvent, View, CustomWrapper } from '@tarojs/components'
import { ITouchEvent, View } from '@tarojs/components'
import {
useEffect,
useState,
useCallback,
forwardRef,
useImperativeHandle,
} from 'react'
import { nextTick } from '@tarojs/taro'
import * as utils from '../wxs/utils'
import { SwipeCellProps, ISwiperCellInstance } from '../../types/swipe-cell'
import { range } from '../common/utils'

const THRESHOLD = 0.3
let ARRAY: any[] = []
const MIN_DISTANCE = 10
const MIN_DISTANCE = 20
function getDirection(x: number, y: number) {
if (x > y && x > MIN_DISTANCE) {
return 'horizontal'
Expand All @@ -32,6 +32,10 @@ function Index(
const [instanceKey, setInstanceKey] = useState<any>({})
const [touchState, setTouchState] = useState<any>({})
const [startOffset, setStartOffset] = useState<number>(0)
const [THRESHOLD, setTHRESHOLD] = useState({
left: 0.2,
right: 0.2,
})

const {
leftWidth = 0,
Expand All @@ -54,6 +58,12 @@ function Index(
function (offset2 = 0, dragging?: boolean) {
const offset_ = range(offset2, -rightWidth, leftWidth)
setOffset(offset_)
if (offset2 === 0) {
setTHRESHOLD({
left: 0.2,
right: 0.2,
})
}
const transform = `translate3d(${offset_}px, 0, 0)`
const transition = dragging
? 'none'
Expand All @@ -72,7 +82,7 @@ function Index(

const close = useCallback(
function () {
swipeMove(0)
swipeMove(0, false)
},
[swipeMove],
)
Expand Down Expand Up @@ -110,6 +120,8 @@ function Index(
deltaY: 0,
offsetX: 0,
offsetY: 0,
startX: 0,
startY: 0,
})
},
[touchState],
Expand All @@ -122,7 +134,7 @@ function Index(
setTouchState({
...touchState,
startX: touch.clientX,
startY: touch.startY,
startY: touch.clientY,
})
},
[touchState, resetTouchStatus],
Expand All @@ -136,7 +148,10 @@ function Index(
...touchState,
direction:
touchState.direction ||
getDirection(touchState.offsetX, touchState.offsetY),
getDirection(
Math.abs(touch.clientX - (touchState.startX || 0)),
Math.abs(touch.clientY - (touchState.startY || 0)),
),
deltaX: touch.clientX - (touchState.startX || 0),
deltaY: touch.clientY - (touchState.startY || 0),
offsetX: Math.abs(touchState.deltaX),
Expand All @@ -151,7 +166,18 @@ function Index(
const open = useCallback(
function (position) {
const offset = position === 'left' ? leftWidth : -rightWidth
swipeMove(offset)
const THRESHOLD_ = {
left: 0.2,
right: 0.2,
}
if (position === 'left') {
THRESHOLD_.left = 0.8
}
if (position === 'right') {
THRESHOLD_.right = 0.8
}
setTHRESHOLD(THRESHOLD_)
swipeMove(offset, false)
if (onOpen) {
const e = {
detail: {
Expand All @@ -167,15 +193,31 @@ function Index(

const swipeLeaveTransition = useCallback(
function () {
if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
open('right')
} else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
open('left')
} else {
swipeMove(0)
if (offset > 0) {
if (leftWidth && offset > THRESHOLD.left * leftWidth) {
open('left')
} else {
swipeMove(0)
}
}

if (offset < 0) {
if (rightWidth && -offset > THRESHOLD.right * rightWidth) {
open('right')
} else {
swipeMove(0)
}
}
},
[leftWidth, offset, open, rightWidth, swipeMove],
[
THRESHOLD.left,
THRESHOLD.right,
leftWidth,
offset,
open,
rightWidth,
swipeMove,
],
)

const onClick_ = useCallback(
Expand Down Expand Up @@ -221,8 +263,11 @@ function Index(
if (disabled) return
event.preventDefault()
const touchState = touchMove(event)
if (!touchState.direction || touchState.direction === 'vertical') {
return
}
ARRAY.filter((item) => item.key !== instanceKey.key).forEach((item) =>
item.close(),
item.close(true),
)
swipeMove(startOffset + touchState.deltaX, true)
},
Expand All @@ -233,8 +278,11 @@ function Index(
function () {
if (disabled) return
swipeLeaveTransition()
nextTick(() => {
resetTouchStatus()
})
},
[disabled, swipeLeaveTransition],
[disabled, resetTouchStatus, swipeLeaveTransition],
)

useImperativeHandle(ref, function () {
Expand All @@ -244,7 +292,7 @@ function Index(
}
})

const MAIN_RENDER = (
return (
<View
className={`van-swipe-cell ${className}`}
data-key="cell"
Expand Down Expand Up @@ -285,10 +333,6 @@ function Index(
</View>
</View>
)

if (process.env.TARO_ENV === 'weapp') {
return <CustomWrapper>{MAIN_RENDER}</CustomWrapper>
} else return <>{MAIN_RENDER}</>
}

const SwipeCell = forwardRef(Index)
Expand Down

0 comments on commit 3414f7d

Please sign in to comment.