mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(ImagePreview): close event triggered twice (#5411)
This commit is contained in:
parent
93e6c81c49
commit
8dea26db95
@ -1,7 +1,7 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { emit, inherit } from '../utils/functional';
|
import { emit, inherit } from '../utils/functional';
|
||||||
import { BORDER_TOP, BORDER_BOTTOM } from '../utils/constant';
|
import { BORDER_TOP, BORDER_BOTTOM } from '../utils/constant';
|
||||||
import { PopupMixin } from '../mixins/popup';
|
import { popupMixinProps } from '../mixins/popup';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
import Popup from '../popup';
|
import Popup from '../popup';
|
||||||
import Loading from '../loading';
|
import Loading from '../loading';
|
||||||
@ -152,7 +152,7 @@ function ActionSheet(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ActionSheet.props = {
|
ActionSheet.props = {
|
||||||
...PopupMixin.props,
|
...popupMixinProps,
|
||||||
title: String,
|
title: String,
|
||||||
actions: Array,
|
actions: Array,
|
||||||
duration: Number,
|
duration: Number,
|
||||||
|
@ -6,7 +6,7 @@ import Button from '../button';
|
|||||||
const [createComponent, bem, t] = createNamespace('dialog');
|
const [createComponent, bem, t] = createNamespace('dialog');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [PopupMixin],
|
mixins: [PopupMixin()],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
title: String,
|
title: String,
|
||||||
|
@ -18,7 +18,12 @@ function getDistance(touches) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [PopupMixin, TouchMixin],
|
mixins: [
|
||||||
|
PopupMixin({
|
||||||
|
skipToggleEvent: true
|
||||||
|
}),
|
||||||
|
TouchMixin
|
||||||
|
],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
className: null,
|
className: null,
|
||||||
@ -83,8 +88,8 @@ export default createComponent({
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (scale !== 1) {
|
if (scale !== 1) {
|
||||||
style.transform = `scale3d(${scale}, ${scale}, 1) translate(${this.moveX /
|
style.transform = `scale3d(${scale}, ${scale}, 1) translate(${this
|
||||||
scale}px, ${this.moveY / scale}px)`;
|
.moveX / scale}px, ${this.moveY / scale}px)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return style;
|
return style;
|
||||||
@ -92,8 +97,15 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
value() {
|
value(val) {
|
||||||
this.setActive(this.startPosition);
|
this.setActive(this.startPosition);
|
||||||
|
|
||||||
|
if (!val) {
|
||||||
|
this.$emit('close', {
|
||||||
|
index: this.active,
|
||||||
|
url: this.images[this.active]
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
startPosition(active) {
|
startPosition(active) {
|
||||||
@ -116,14 +128,7 @@ export default createComponent({
|
|||||||
if (deltaTime < 300 && offsetX < 10 && offsetY < 10) {
|
if (deltaTime < 300 && offsetX < 10 && offsetY < 10) {
|
||||||
if (!this.doubleClickTimer) {
|
if (!this.doubleClickTimer) {
|
||||||
this.doubleClickTimer = setTimeout(() => {
|
this.doubleClickTimer = setTimeout(() => {
|
||||||
const index = this.active;
|
|
||||||
|
|
||||||
if (!this.asyncClose) {
|
if (!this.asyncClose) {
|
||||||
this.$emit('close', {
|
|
||||||
index,
|
|
||||||
url: this.images[index]
|
|
||||||
});
|
|
||||||
|
|
||||||
this.$emit('input', false);
|
this.$emit('input', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,7 +253,8 @@ export default createComponent({
|
|||||||
if (this.showIndex) {
|
if (this.showIndex) {
|
||||||
return (
|
return (
|
||||||
<div class={bem('index')}>
|
<div class={bem('index')}>
|
||||||
{this.slots('index') || `${this.active + 1} / ${this.images.length}`}
|
{this.slots('index') ||
|
||||||
|
`${this.active + 1} / ${this.images.length}`}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,25 @@
|
|||||||
<template>
|
<template>
|
||||||
<demo-section>
|
<demo-section>
|
||||||
<demo-block :title="$t('basicUsage')">
|
<demo-block :title="$t('basicUsage')">
|
||||||
<van-button
|
<van-button type="primary" @click="showImagePreview">
|
||||||
type="primary"
|
|
||||||
@click="showImagePreview"
|
|
||||||
>
|
|
||||||
{{ $t('button1') }}
|
{{ $t('button1') }}
|
||||||
</van-button>
|
</van-button>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block :title="$t('button2')">
|
<demo-block :title="$t('button2')">
|
||||||
<van-button
|
<van-button type="primary" @click="showImagePreview(1)">
|
||||||
type="primary"
|
|
||||||
@click="showImagePreview(1)"
|
|
||||||
>
|
|
||||||
{{ $t('button2') }}
|
{{ $t('button2') }}
|
||||||
</van-button>
|
</van-button>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block :title="$t('button3')">
|
<demo-block :title="$t('button3')">
|
||||||
<van-button
|
<van-button type="primary" @click="showImagePreview(0, 3000)">
|
||||||
type="primary"
|
|
||||||
@click="showImagePreview(0, 1000)"
|
|
||||||
>
|
|
||||||
{{ $t('button3') }}
|
{{ $t('button3') }}
|
||||||
</van-button>
|
</van-button>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block :title="$t('componentCall')">
|
<demo-block :title="$t('componentCall')">
|
||||||
<van-button
|
<van-button type="primary" @click="componentCall">
|
||||||
type="primary"
|
|
||||||
@click="componentCall"
|
|
||||||
>
|
|
||||||
{{ $t('componentCall') }}
|
{{ $t('componentCall') }}
|
||||||
</van-button>
|
</van-button>
|
||||||
<van-image-preview
|
<van-image-preview
|
||||||
@ -112,7 +100,7 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="less">
|
<style lang="less">
|
||||||
@import "../../style/var";
|
@import '../../style/var';
|
||||||
|
|
||||||
.demo-image-preview {
|
.demo-image-preview {
|
||||||
background-color: @white;
|
background-color: @white;
|
||||||
|
@ -92,7 +92,7 @@ test('double click', async done => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('onClose option', async done => {
|
test('onClose option', () => {
|
||||||
const onClose = jest.fn();
|
const onClose = jest.fn();
|
||||||
const instance = ImagePreview({
|
const instance = ImagePreview({
|
||||||
images,
|
images,
|
||||||
@ -100,37 +100,10 @@ test('onClose option', async done => {
|
|||||||
onClose
|
onClose
|
||||||
});
|
});
|
||||||
|
|
||||||
instance.$emit('input', true);
|
|
||||||
expect(onClose).toHaveBeenCalledTimes(0);
|
|
||||||
|
|
||||||
await later(300);
|
|
||||||
|
|
||||||
const wrapper = document.querySelector('.van-image-preview');
|
|
||||||
const swipe = wrapper.querySelector('.van-swipe__track');
|
|
||||||
triggerDrag(swipe, 0, 0);
|
|
||||||
expect(onClose).toHaveBeenCalledTimes(1);
|
|
||||||
expect(onClose).toHaveBeenCalledWith({ index: 0, url: 'https://img.yzcdn.cn/1.png' });
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('onClose should only trigger once', async done => {
|
|
||||||
const onClose = jest.fn();
|
|
||||||
const instance = ImagePreview({
|
|
||||||
images,
|
|
||||||
startPostion: 1,
|
|
||||||
onClose
|
|
||||||
});
|
|
||||||
|
|
||||||
ImagePreview({
|
|
||||||
images,
|
|
||||||
startPostion: 1,
|
|
||||||
onClose
|
|
||||||
});
|
|
||||||
|
|
||||||
instance.close();
|
instance.close();
|
||||||
|
|
||||||
expect(onClose).toHaveBeenCalledTimes(1);
|
expect(onClose).toHaveBeenCalledTimes(1);
|
||||||
done();
|
expect(onClose).toHaveBeenCalledWith({ index: 0, url: 'https://img.yzcdn.cn/1.png' });
|
||||||
});
|
});
|
||||||
|
|
||||||
test('onChange option', async done => {
|
test('onChange option', async done => {
|
||||||
|
@ -6,20 +6,7 @@ import { on, off, preventDefault } from '../../utils/dom/event';
|
|||||||
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
||||||
import { getScrollEventTarget } from '../../utils/dom/scroll';
|
import { getScrollEventTarget } from '../../utils/dom/scroll';
|
||||||
|
|
||||||
export const PopupMixin = {
|
export const popupMixinProps = {
|
||||||
mixins: [
|
|
||||||
TouchMixin,
|
|
||||||
CloseOnPopstateMixin,
|
|
||||||
PortalMixin({
|
|
||||||
afterPortal() {
|
|
||||||
if (this.overlay) {
|
|
||||||
updateOverlay();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
],
|
|
||||||
|
|
||||||
props: {
|
|
||||||
// whether to show popup
|
// whether to show popup
|
||||||
value: Boolean,
|
value: Boolean,
|
||||||
// whether to show overlay
|
// whether to show overlay
|
||||||
@ -42,7 +29,23 @@ export const PopupMixin = {
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true
|
default: true
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
|
export function PopupMixin(options = {}) {
|
||||||
|
return {
|
||||||
|
mixins: [
|
||||||
|
TouchMixin,
|
||||||
|
CloseOnPopstateMixin,
|
||||||
|
PortalMixin({
|
||||||
|
afterPortal() {
|
||||||
|
if (this.overlay) {
|
||||||
|
updateOverlay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
],
|
||||||
|
|
||||||
|
props: popupMixinProps,
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -61,7 +64,10 @@ export const PopupMixin = {
|
|||||||
const type = val ? 'open' : 'close';
|
const type = val ? 'open' : 'close';
|
||||||
this.inited = this.inited || this.value;
|
this.inited = this.inited || this.value;
|
||||||
this[type]();
|
this[type]();
|
||||||
|
|
||||||
|
if (!options.skipToggleEvent) {
|
||||||
this.$emit(type);
|
this.$emit(type);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
overlay: 'renderOverlay'
|
overlay: 'renderOverlay'
|
||||||
@ -193,3 +199,4 @@ export const PopupMixin = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
@ -31,8 +31,7 @@ function onClickOverlay(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateOverlay(): void {
|
function mountOverlay() {
|
||||||
if (!overlay) {
|
|
||||||
overlay = mount(Overlay, {
|
overlay = mount(Overlay, {
|
||||||
on: {
|
on: {
|
||||||
click: onClickOverlay
|
click: onClickOverlay
|
||||||
@ -40,6 +39,11 @@ export function updateOverlay(): void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updateOverlay(): void {
|
||||||
|
if (!overlay) {
|
||||||
|
mountOverlay();
|
||||||
|
}
|
||||||
|
|
||||||
if (context.top) {
|
if (context.top) {
|
||||||
const { vm, config } = context.top;
|
const { vm, config } = context.top;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { WHITE } from '../utils/constant';
|
import { WHITE } from '../utils/constant';
|
||||||
import { inherit } from '../utils/functional';
|
import { inherit } from '../utils/functional';
|
||||||
import { PopupMixin } from '../mixins/popup';
|
import { popupMixinProps } from '../mixins/popup';
|
||||||
import Popup from '../popup';
|
import Popup from '../popup';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
@ -48,7 +48,7 @@ function Notify(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Notify.props = {
|
Notify.props = {
|
||||||
...PopupMixin.props,
|
...popupMixinProps,
|
||||||
background: String,
|
background: String,
|
||||||
className: null as any,
|
className: null as any,
|
||||||
message: [Number, String],
|
message: [Number, String],
|
||||||
|
@ -5,7 +5,7 @@ import Icon from '../icon';
|
|||||||
const [createComponent, bem] = createNamespace('popup');
|
const [createComponent, bem] = createNamespace('popup');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [PopupMixin],
|
mixins: [PopupMixin()],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
round: Boolean,
|
round: Boolean,
|
||||||
|
@ -7,7 +7,7 @@ import Loading from '../loading';
|
|||||||
const [createComponent, bem] = createNamespace('toast');
|
const [createComponent, bem] = createNamespace('toast');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [PopupMixin],
|
mixins: [PopupMixin()],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
icon: String,
|
icon: String,
|
||||||
|
2
types/image-preview.d.ts
vendored
2
types/image-preview.d.ts
vendored
@ -19,7 +19,9 @@ export type ImagePreviewOptions = string[] | {
|
|||||||
|
|
||||||
export class VanImagePreview extends VanPopupMixin {
|
export class VanImagePreview extends VanPopupMixin {
|
||||||
images: string[];
|
images: string[];
|
||||||
|
|
||||||
showIndex: boolean;
|
showIndex: boolean;
|
||||||
|
|
||||||
startPosition: number;
|
startPosition: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user