feat(SwipeCell): add new event open & stop catch touchend (#2432)

fix #2402
This commit is contained in:
rex 2019-11-29 16:15:56 +08:00 committed by GitHub
parent 4a196aaaa7
commit c73bc5d3c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 42 deletions

View File

@ -1,14 +1,32 @@
import { Weapp } from 'definitions/weapp'; import { Weapp } from 'definitions/weapp';
const MIN_DISTANCE = 10;
function getDirection(x: number, y: number) {
if (x > y && x > MIN_DISTANCE) {
return 'horizontal';
}
if (y > x && y > MIN_DISTANCE) {
return 'vertical';
}
return '';
}
export const touch = Behavior({ export const touch = Behavior({
methods: { methods: {
touchStart(event: Weapp.TouchEvent) { resetTouchStatus() {
const touch = event.touches[0];
this.direction = ''; this.direction = '';
this.deltaX = 0; this.deltaX = 0;
this.deltaY = 0; this.deltaY = 0;
this.offsetX = 0; this.offsetX = 0;
this.offsetY = 0; this.offsetY = 0;
},
touchStart(event: Weapp.TouchEvent) {
this.resetTouchStatus();
const touch = event.touches[0];
this.startX = touch.clientX; this.startX = touch.clientX;
this.startY = touch.clientY; this.startY = touch.clientY;
}, },
@ -19,12 +37,7 @@ export const touch = Behavior({
this.deltaY = touch.clientY - this.startY; this.deltaY = touch.clientY - this.startY;
this.offsetX = Math.abs(this.deltaX); this.offsetX = Math.abs(this.deltaX);
this.offsetY = Math.abs(this.deltaY); this.offsetY = Math.abs(this.deltaY);
this.direction = this.direction = this.direction || getDirection(this.offsetX, this.offsetY);
this.offsetX > this.offsetY
? 'horizontal'
: this.offsetX < this.offsetY
? 'vertical'
: '';
} }
} }
}); });

View File

@ -82,7 +82,8 @@ Page({
| 事件名 | 说明 | 参数 | | 事件名 | 说明 | 参数 |
|------|------|------| |------|------|------|
| click | 点击时触发 | 关闭时的点击位置 (`left` `right` `cell` `outside`) | | click | 点击时触发 | 关闭时的点击位置 (`left` `right` `cell` `outside`) |
| close | 点击时触发 | 整体是一个 Object包含 `position`, `instance` 两个 key。 | | close | 关闭时触发 | { position: 'left' \| 'right' , instance , name: string } |
| open | 打开时触发 | { position: 'left' \| 'right' , name: string } |
### close 参数 ### close 参数
@ -90,6 +91,7 @@ Page({
|------|------|------| |------|------|------|
| position | *string* | 关闭时的点击位置 (`left` `right` `cell` `outside`) | | position | *string* | 关闭时的点击位置 (`left` `right` `cell` `outside`) |
| instance | *object* | SwipeCell 实例 | | instance | *object* | SwipeCell 实例 |
| name | 标识符 | *string* |
### 方法 ### 方法

View File

@ -1,6 +1,7 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch'; import { touch } from '../mixins/touch';
import { Weapp } from 'definitions/weapp'; import { Weapp } from 'definitions/weapp';
import { range } from '../common/utils';
const THRESHOLD = 0.3; const THRESHOLD = 0.3;
let ARRAY: WechatMiniprogram.Component.TrivialInstance[] = []; let ARRAY: WechatMiniprogram.Component.TrivialInstance[] = [];
@ -43,6 +44,11 @@ VantComponent({
const { leftWidth, rightWidth } = this.data; const { leftWidth, rightWidth } = this.data;
const offset = position === 'left' ? leftWidth : -rightWidth; const offset = position === 'left' ? leftWidth : -rightWidth;
this.swipeMove(offset); this.swipeMove(offset);
this.$emit('open', {
position,
name: this.data.name
});
}, },
close() { close() {
@ -50,10 +56,10 @@ VantComponent({
}, },
swipeMove(offset: number = 0) { swipeMove(offset: number = 0) {
this.offset = offset; this.offset = range(offset, -this.data.rightWidth, this.data.leftWidth);
const transform = `translate3d(${offset}px, 0, 0)`; const transform = `translate3d(${this.offset}px, 0, 0)`;
const transition = this.draging const transition = this.dragging
? 'none' ? 'none'
: 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)'; : 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)';
@ -86,15 +92,7 @@ VantComponent({
return; return;
} }
ARRAY.forEach(item => {
if (item !== this) {
item.close();
}
});
this.draging = true;
this.startOffset = this.offset; this.startOffset = this.offset;
this.firstDirection = '';
this.touchStart(event); this.touchStart(event);
}, },
@ -107,27 +105,14 @@ VantComponent({
this.touchMove(event); this.touchMove(event);
if (!this.firstDirection) { if (this.direction !== 'horizontal') {
this.firstDirection = this.direction;
this.setData({ catchMove: this.firstDirection === 'horizontal' });
}
if (this.firstDirection === 'vertical') {
return; return;
} }
const { leftWidth, rightWidth } = this.data; this.dragging = true;
ARRAY.filter(item => item !== this).forEach(item => item.close());
const offset = this.startOffset + this.deltaX; this.setData({ catchMove: true });
this.swipeMove(this.startOffset + this.deltaX);
if (
(rightWidth > 0 && -offset > rightWidth) ||
(leftWidth > 0 && offset > leftWidth)
) {
return;
}
this.swipeMove(offset);
}, },
endDrag() { endDrag() {
@ -135,7 +120,7 @@ VantComponent({
return; return;
} }
this.draging = false; this.dragging = false;
this.swipeLeaveTransition(); this.swipeLeaveTransition();
}, },
@ -148,7 +133,11 @@ VantComponent({
} }
if (this.data.asyncClose) { if (this.data.asyncClose) {
this.$emit('close', { position, instance: this, name: this.data.name }); this.$emit('close', {
position,
instance: this,
name: this.data.name
});
} else { } else {
this.swipeMove(0); this.swipeMove(0);
} }

View File

@ -5,8 +5,8 @@
bindtouchstart="startDrag" bindtouchstart="startDrag"
catchtouchmove="{{ catchMove ? 'noop' : '' }}" catchtouchmove="{{ catchMove ? 'noop' : '' }}"
capture-bind:touchmove="onDrag" capture-bind:touchmove="onDrag"
catchtouchend="endDrag" bindtouchend="endDrag"
catchtouchcancel="endDrag" bindtouchcancel="endDrag"
> >
<view style="{{ wrapperStyle }}"> <view style="{{ wrapperStyle }}">
<view wx:if="{{ leftWidth }}" class="van-swipe-cell__left" data-key="left" catch:tap="onClick"> <view wx:if="{{ leftWidth }}" class="van-swipe-cell__left" data-key="left" catch:tap="onClick">