feat(SwipeCell): add open event (#5324)

This commit is contained in:
neverland 2019-12-20 10:20:18 +08:00 committed by GitHub
parent ac00d78a46
commit c0b1460c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 21 deletions

View File

@ -94,6 +94,7 @@ export default {
|------|------|------| |------|------|------|
| click | Triggered when clicked | Click positon (`left` `right` `cell` `outside`) | | click | Triggered when clicked | Click positon (`left` `right` `cell` `outside`) |
| open | Triggered when opened | { position: 'left' \| 'right' , name: string } | | open | Triggered when opened | { position: 'left' \| 'right' , name: string } |
| close | Triggered when closed | { position: string , name: string } |
### beforeClose Params ### beforeClose Params

View File

@ -100,6 +100,7 @@ export default {
|------|------|------| |------|------|------|
| click | 点击时触发 | 关闭时的点击位置 (`left` `right` `cell` `outside`) | | click | 点击时触发 | 关闭时的点击位置 (`left` `right` `cell` `outside`) |
| open | 打开时触发 | { position: 'left' \| 'right' , name: string } | | open | 打开时触发 | { position: 'left' \| 'right' , name: string } |
| close | 关闭时触发 | { position: string , name: string } |
### beforeClose 参数 ### beforeClose 参数

View File

@ -63,29 +63,28 @@ export default createComponent({
const offset = const offset =
position === 'left' ? this.computedLeftWidth : -this.computedRightWidth; position === 'left' ? this.computedLeftWidth : -this.computedRightWidth;
this.swipeMove(offset);
this.opened = true; this.opened = true;
this.offset = offset;
this.$emit('open', { this.$emit('open', {
position, position,
name: this.name,
// @deprecated
// should be removed in next major version
detail: this.name detail: this.name
}); });
}, },
// @exposed-api // @exposed-api
close() { close(position) {
this.offset = 0; this.offset = 0;
},
swipeMove(offset = 0) { if (this.opened) {
this.offset = range(
offset,
-this.computedRightWidth,
this.computedLeftWidth
);
if (!this.offset) {
this.opened = false; this.opened = false;
this.$emit('close', {
position,
name: this.name
});
} }
}, },
@ -115,7 +114,11 @@ export default createComponent({
preventDefault(event, this.stopPropagation); preventDefault(event, this.stopPropagation);
} }
this.swipeMove(this.deltaX + this.startOffset); this.offset = range(
this.deltaX + this.startOffset,
-this.computedRightWidth,
this.computedLeftWidth
);
} }
}, },
@ -153,7 +156,7 @@ export default createComponent({
) { ) {
this.open('left'); this.open('left');
} else { } else {
this.swipeMove(0); this.close();
} }
}, },
@ -170,7 +173,7 @@ export default createComponent({
} else if (this.onClose) { } else if (this.onClose) {
this.onClose(position, this, { name: this.name }); this.onClose(position, this, { name: this.name });
} else { } else {
this.swipeMove(0); this.close(position);
} }
} }
}, },
@ -185,28 +188,32 @@ export default createComponent({
}, },
genLeftPart() { genLeftPart() {
if (this.slots('left')) { const content = this.slots('left');
if (content) {
return ( return (
<div <div
ref="left" ref="left"
class={bem('left')} class={bem('left')}
onClick={this.getClickHandler('left', true)} onClick={this.getClickHandler('left', true)}
> >
{this.slots('left')} {content}
</div> </div>
); );
} }
}, },
genRightPart() { genRightPart() {
if (this.slots('right')) { const content = this.slots('right');
if (content) {
return ( return (
<div <div
ref="right" ref="right"
class={bem('right')} class={bem('right')}
onClick={this.getClickHandler('right', true)} onClick={this.getClickHandler('right', true)}
> >
{this.slots('right')} {content}
</div> </div>
); );
} }

View File

@ -70,9 +70,10 @@ test('on-close prop', () => {
expect(position).toEqual('right'); expect(position).toEqual('right');
instance.close(); instance.close();
expect(wrapper.vm.offset).toEqual(0); expect(instance.offset).toEqual(0);
wrapper.setData({ offset: 100, onClose: null }); instance.open('left');
wrapper.setData({ onClose: null });
wrapper.trigger('click'); wrapper.trigger('click');
expect(wrapper.vm.offset).toEqual(0); expect(wrapper.vm.offset).toEqual(0);
}); });
@ -108,7 +109,8 @@ test('before-close prop', () => {
instance.close(); instance.close();
expect(wrapper.vm.offset).toEqual(0); expect(wrapper.vm.offset).toEqual(0);
wrapper.setData({ offset: 100, beforeClose: null }); instance.open('left');
wrapper.setData({ beforeClose: null });
wrapper.trigger('click'); wrapper.trigger('click');
expect(wrapper.vm.offset).toEqual(0); expect(wrapper.vm.offset).toEqual(0);
}); });
@ -188,6 +190,7 @@ test('trigger open event when open left side', () => {
triggerDrag(wrapper, 50, 0); triggerDrag(wrapper, 50, 0);
expect(wrapper.emitted('open')[0][0]).toEqual({ expect(wrapper.emitted('open')[0][0]).toEqual({
name: '',
detail: '', detail: '',
position: 'left' position: 'left'
}); });
@ -198,7 +201,29 @@ test('trigger open event when open right side', () => {
triggerDrag(wrapper, -50, 0); triggerDrag(wrapper, -50, 0);
expect(wrapper.emitted('open')[0][0]).toEqual({ expect(wrapper.emitted('open')[0][0]).toEqual({
name: '',
detail: '', detail: '',
position: 'right' position: 'right'
}); });
}); });
test('trigger close event when closed', () => {
const wrapper = mount(SwipeCell, defaultProps);
wrapper.vm.open('left');
wrapper.vm.close();
expect(wrapper.emitted('close')[0][0]).toEqual({
name: '',
position: undefined
});
});
test('should not trigger close event again when already closed', () => {
const wrapper = mount(SwipeCell, defaultProps);
wrapper.vm.open('left');
wrapper.vm.close();
wrapper.vm.close();
expect(wrapper.emitted('close').length).toEqual(1);
});