feat(SwipeCell): optimize code (#5076)

This commit is contained in:
neverland 2019-11-22 10:10:28 +08:00 committed by GitHub
parent 4e5bc170e1
commit aba9e3f009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,10 +55,13 @@ export default createComponent({
return 0; return 0;
}, },
// @exposed-api
open(position) { open(position) {
const offset = position === 'left' ? this.computedLeftWidth : -this.computedRightWidth; const offset =
position === 'left' ? this.computedLeftWidth : -this.computedRightWidth;
this.swipeMove(offset); this.swipeMove(offset);
this.resetSwipeStatus(); this.opened = true;
this.$emit('open', { this.$emit('open', {
position, position,
@ -66,50 +69,24 @@ export default createComponent({
}); });
}, },
// @exposed-api
close() { close() {
this.offset = 0; this.offset = 0;
}, },
resetSwipeStatus() {
this.swiping = false;
this.opened = true;
},
swipeMove(offset = 0) { swipeMove(offset = 0) {
this.offset = range(offset, -this.computedRightWidth, this.computedLeftWidth); this.offset = range(
offset,
-this.computedRightWidth,
this.computedLeftWidth
);
if (this.offset) { if (!this.offset) {
this.swiping = true;
} else {
this.opened = false; this.opened = false;
} }
}, },
swipeLeaveTransition(direction) { onTouchStart(event) {
const { offset, computedLeftWidth, computedRightWidth } = this;
const threshold = this.opened ? 1 - THRESHOLD : THRESHOLD;
// right
if (
direction === 'right' &&
-offset > computedRightWidth * threshold &&
computedRightWidth > 0
) {
this.open('right');
// left
} else if (
direction === 'left' &&
offset > computedLeftWidth * threshold &&
computedLeftWidth > 0
) {
this.open('left');
// reset
} else {
this.swipeMove(0);
}
},
startDrag(event) {
if (this.disabled) { if (this.disabled) {
return; return;
} }
@ -119,7 +96,7 @@ export default createComponent({
this.touchStart(event); this.touchStart(event);
}, },
onDrag(event) { onTouchMove(event) {
if (this.disabled) { if (this.disabled) {
return; return;
} }
@ -127,9 +104,9 @@ export default createComponent({
this.touchMove(event); this.touchMove(event);
if (this.direction === 'horizontal') { if (this.direction === 'horizontal') {
const shouldPrevent = !this.opened || this.deltaX * this.startOffset < 0; const isPrevent = !this.opened || this.deltaX * this.startOffset < 0;
if (shouldPrevent) { if (isPrevent) {
preventDefault(event, this.stopPropagation); preventDefault(event, this.stopPropagation);
} }
@ -137,14 +114,37 @@ export default createComponent({
} }
}, },
endDrag() { onTouchEnd() {
if (this.disabled) { if (this.disabled) {
return; return;
} }
if (this.dragging) {
this.toggle(this.offset > 0 ? 'left' : 'right');
}
this.dragging = false; this.dragging = false;
if (this.swiping) { },
this.swipeLeaveTransition(this.offset > 0 ? 'left' : 'right');
toggle(direction) {
const offset = Math.abs(this.offset);
const threshold = this.opened ? 1 - THRESHOLD : THRESHOLD;
const { computedLeftWidth, computedRightWidth } = this;
if (
computedRightWidth &&
direction === 'right' &&
offset > computedRightWidth * threshold
) {
this.open('right');
} else if (
computedLeftWidth &&
direction === 'left' &&
offset > computedLeftWidth * threshold
) {
this.open('left');
} else {
this.swipeMove(0);
} }
}, },
@ -160,17 +160,47 @@ export default createComponent({
} else { } else {
this.swipeMove(0); this.swipeMove(0);
} }
}
}, },
render() { getClickHandler(position, stop) {
const onClick = (position, stop) => event => { return event => {
if (stop) { if (stop) {
event.stopPropagation(); event.stopPropagation();
} }
this.onClick(position); this.onClick(position);
}; };
},
genLeftPart() {
if (this.slots('left')) {
return (
<div
ref="left"
class={bem('left')}
onClick={this.getClickHandler('left', true)}
>
{this.slots('left')}
</div>
);
}
},
genRightPart() {
if (this.slots('right')) {
return (
<div
ref="right"
class={bem('right')}
onClick={this.getClickHandler('right', true)}
>
{this.slots('right')}
</div>
);
}
}
},
render() {
const wrapperStyle = { const wrapperStyle = {
transform: `translate3d(${this.offset}px, 0, 0)`, transform: `translate3d(${this.offset}px, 0, 0)`,
transitionDuration: this.dragging ? '0s' : '.6s' transitionDuration: this.dragging ? '0s' : '.6s'
@ -179,30 +209,16 @@ export default createComponent({
return ( return (
<div <div
class={bem()} class={bem()}
onClick={onClick('cell')} onClick={this.getClickHandler('cell')}
onTouchstart={this.startDrag} onTouchstart={this.onTouchStart}
onTouchmove={this.onDrag} onTouchmove={this.onTouchMove}
onTouchend={this.endDrag} onTouchend={this.onTouchEnd}
onTouchcancel={this.endDrag} onTouchcancel={this.onTouchEnd}
> >
<div <div class={bem('wrapper')} style={wrapperStyle}>
class={bem('wrapper')} {this.genLeftPart()}
style={wrapperStyle}
onTransitionend={() => {
this.swiping = false;
}}
>
{this.slots('left') && (
<div ref="left" class={bem('left')} onClick={onClick('left', true)}>
{this.slots('left')}
</div>
)}
{this.slots()} {this.slots()}
{this.slots('right') && ( {this.genRightPart()}
<div ref="right" class={bem('right')} onClick={onClick('right', true)}>
{this.slots('right')}
</div>
)}
</div> </div>
</div> </div>
); );