mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(SwipeCell): Fix triggered close by clickAway when in running beforeClose (#12309)
This commit is contained in:
parent
1f0e3a1f2a
commit
829afd5dbd
@ -91,7 +91,9 @@ export default {
|
|||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
showConfirmDialog({
|
showConfirmDialog({
|
||||||
title: 'Are you sure to delete?',
|
title: 'Are you sure to delete?',
|
||||||
}).then(resolve);
|
})
|
||||||
|
.then(resolve)
|
||||||
|
.catch(resolve);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -98,7 +98,9 @@ export default {
|
|||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
showConfirmDialog({
|
showConfirmDialog({
|
||||||
title: '确定删除吗?',
|
title: '确定删除吗?',
|
||||||
}).then(resolve);
|
})
|
||||||
|
.then(resolve)
|
||||||
|
.catch(resolve);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -56,6 +56,7 @@ export default defineComponent({
|
|||||||
let opened: boolean;
|
let opened: boolean;
|
||||||
let lockClick: boolean;
|
let lockClick: boolean;
|
||||||
let startOffset: number;
|
let startOffset: number;
|
||||||
|
let isInBeforeClosing: boolean;
|
||||||
|
|
||||||
const root = ref<HTMLElement>();
|
const root = ref<HTMLElement>();
|
||||||
const leftRef = ref<HTMLElement>();
|
const leftRef = ref<HTMLElement>();
|
||||||
@ -161,9 +162,12 @@ export default defineComponent({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onClick = (position: SwipeCellPosition = 'outside') => {
|
const onClick = (position: SwipeCellPosition = 'outside') => {
|
||||||
|
if (isInBeforeClosing) return;
|
||||||
|
|
||||||
emit('click', position);
|
emit('click', position);
|
||||||
|
|
||||||
if (opened && !lockClick) {
|
if (opened && !lockClick) {
|
||||||
|
isInBeforeClosing = true;
|
||||||
callInterceptor(props.beforeClose, {
|
callInterceptor(props.beforeClose, {
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
@ -171,7 +175,12 @@ export default defineComponent({
|
|||||||
position,
|
position,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
done: () => close(position),
|
done: () => {
|
||||||
|
isInBeforeClosing = false;
|
||||||
|
close(position);
|
||||||
|
},
|
||||||
|
canceled: () => (isInBeforeClosing = false),
|
||||||
|
error: () => (isInBeforeClosing = false),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -38,13 +38,9 @@ const beforeClose = ({ position }: { position: string }) => {
|
|||||||
case 'outside':
|
case 'outside':
|
||||||
return true;
|
return true;
|
||||||
case 'right':
|
case 'right':
|
||||||
return new Promise<boolean>((resolve) => {
|
return showConfirmDialog({
|
||||||
showConfirmDialog({
|
title: t('confirm'),
|
||||||
title: t('confirm'),
|
}) as Promise<boolean>;
|
||||||
}).then(() => {
|
|
||||||
resolve(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
@ -37,15 +37,29 @@ test('should allow to drag to show right part', async () => {
|
|||||||
expect(track.style.transform).toEqual('translate3d(-100px, 0, 0)');
|
expect(track.style.transform).toEqual('translate3d(-100px, 0, 0)');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should call beforeClose before closing', () => {
|
test('should call beforeClose before closing', async () => {
|
||||||
let position;
|
let position;
|
||||||
|
let clickPosition;
|
||||||
|
let usePromise;
|
||||||
|
let promiseRet;
|
||||||
|
|
||||||
const wrapper = mount(SwipeCell, {
|
const wrapper = mount(SwipeCell, {
|
||||||
...defaultProps,
|
...defaultProps,
|
||||||
props: {
|
props: {
|
||||||
...defaultProps.props,
|
...defaultProps.props,
|
||||||
|
onClick(position) {
|
||||||
|
clickPosition = position;
|
||||||
|
},
|
||||||
beforeClose(params) {
|
beforeClose(params) {
|
||||||
({ position } = params);
|
if (usePromise) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
resolve(promiseRet);
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
({ position } = params);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -66,6 +80,17 @@ test('should call beforeClose before closing', () => {
|
|||||||
|
|
||||||
wrapper.vm.close();
|
wrapper.vm.close();
|
||||||
expect(track.style.transform).toEqual('translate3d(0px, 0, 0)');
|
expect(track.style.transform).toEqual('translate3d(0px, 0, 0)');
|
||||||
|
|
||||||
|
usePromise = true;
|
||||||
|
promiseRet = false;
|
||||||
|
wrapper.vm.open('right');
|
||||||
|
wrapper.find('.van-swipe-cell__right').trigger('click');
|
||||||
|
expect(clickPosition).toEqual('right');
|
||||||
|
wrapper.trigger('click');
|
||||||
|
expect(clickPosition).toEqual('right');
|
||||||
|
await later(200);
|
||||||
|
wrapper.trigger('click');
|
||||||
|
expect(clickPosition).toEqual('cell');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should close swipe cell after clicked', async () => {
|
test('should close swipe cell after clicked', async () => {
|
||||||
|
@ -10,10 +10,12 @@ export function callInterceptor(
|
|||||||
args = [],
|
args = [],
|
||||||
done,
|
done,
|
||||||
canceled,
|
canceled,
|
||||||
|
error,
|
||||||
}: {
|
}: {
|
||||||
args?: unknown[];
|
args?: unknown[];
|
||||||
done: () => void;
|
done: () => void;
|
||||||
canceled?: () => void;
|
canceled?: () => void;
|
||||||
|
error?: () => void;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
if (interceptor) {
|
if (interceptor) {
|
||||||
@ -29,7 +31,7 @@ export function callInterceptor(
|
|||||||
canceled();
|
canceled();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(noop);
|
.catch(error || noop);
|
||||||
} else if (returnVal) {
|
} else if (returnVal) {
|
||||||
done();
|
done();
|
||||||
} else if (canceled) {
|
} else if (canceled) {
|
||||||
|
@ -3,36 +3,61 @@ import { callInterceptor } from '../interceptor';
|
|||||||
|
|
||||||
test('callInterceptor', async () => {
|
test('callInterceptor', async () => {
|
||||||
const done = vi.fn();
|
const done = vi.fn();
|
||||||
callInterceptor(undefined, { done });
|
const canceled = vi.fn();
|
||||||
|
const error = vi.fn();
|
||||||
|
|
||||||
|
callInterceptor(undefined, { done, canceled, error });
|
||||||
expect(done).toHaveBeenCalledTimes(1);
|
expect(done).toHaveBeenCalledTimes(1);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(0);
|
||||||
|
expect(error).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
callInterceptor(() => false, {
|
callInterceptor(() => false, {
|
||||||
done,
|
done,
|
||||||
|
canceled,
|
||||||
|
error,
|
||||||
});
|
});
|
||||||
expect(done).toHaveBeenCalledTimes(1);
|
expect(done).toHaveBeenCalledTimes(1);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(1);
|
||||||
|
expect(error).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
callInterceptor(() => true, {
|
callInterceptor(() => true, {
|
||||||
done,
|
done,
|
||||||
|
canceled,
|
||||||
|
error,
|
||||||
});
|
});
|
||||||
expect(done).toHaveBeenCalledTimes(2);
|
expect(done).toHaveBeenCalledTimes(2);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(1);
|
||||||
|
expect(error).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
callInterceptor(() => Promise.resolve(false), {
|
callInterceptor(() => Promise.resolve(false), {
|
||||||
done,
|
done,
|
||||||
|
canceled,
|
||||||
|
error,
|
||||||
});
|
});
|
||||||
await later();
|
await later();
|
||||||
expect(done).toHaveBeenCalledTimes(2);
|
expect(done).toHaveBeenCalledTimes(2);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(2);
|
||||||
|
expect(error).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
callInterceptor(() => Promise.resolve(true), {
|
callInterceptor(() => Promise.resolve(true), {
|
||||||
done,
|
done,
|
||||||
|
canceled,
|
||||||
|
error,
|
||||||
});
|
});
|
||||||
await later();
|
await later();
|
||||||
expect(done).toHaveBeenCalledTimes(3);
|
expect(done).toHaveBeenCalledTimes(3);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(2);
|
||||||
|
expect(error).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
callInterceptor(() => Promise.reject(), {
|
callInterceptor(() => Promise.reject(), {
|
||||||
done,
|
done,
|
||||||
|
canceled,
|
||||||
|
error,
|
||||||
});
|
});
|
||||||
await later();
|
await later();
|
||||||
expect(done).toHaveBeenCalledTimes(3);
|
expect(done).toHaveBeenCalledTimes(3);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(2);
|
||||||
|
expect(error).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
callInterceptor(
|
callInterceptor(
|
||||||
(...args) => {
|
(...args) => {
|
||||||
@ -42,8 +67,12 @@ test('callInterceptor', async () => {
|
|||||||
{
|
{
|
||||||
args: ['foo'],
|
args: ['foo'],
|
||||||
done,
|
done,
|
||||||
|
canceled,
|
||||||
|
error,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(done).toHaveBeenCalledTimes(3);
|
expect(done).toHaveBeenCalledTimes(3);
|
||||||
|
expect(canceled).toHaveBeenCalledTimes(3);
|
||||||
|
expect(error).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user