mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore: adjust callInterceptor params (#9674)
* chore: adjust callInterceptor params * chore: upd
This commit is contained in:
parent
2bd6bc48c2
commit
66cf1dabba
@ -74,10 +74,7 @@ export default defineComponent({
|
||||
|
||||
const close = (action: DialogAction) => {
|
||||
updateShow(false);
|
||||
|
||||
if (props.callback) {
|
||||
props.callback(action);
|
||||
}
|
||||
props.callback?.(action);
|
||||
};
|
||||
|
||||
const getActionHandler = (action: DialogAction) => () => {
|
||||
@ -90,8 +87,7 @@ export default defineComponent({
|
||||
|
||||
if (props.beforeClose) {
|
||||
loading[action] = true;
|
||||
callInterceptor({
|
||||
interceptor: props.beforeClose,
|
||||
callInterceptor(props.beforeClose, {
|
||||
args: [action],
|
||||
done() {
|
||||
close(action);
|
||||
|
@ -49,7 +49,7 @@ Dialog.defaultOptions = {
|
||||
className: '',
|
||||
allowHtml: false,
|
||||
lockScroll: true,
|
||||
transition: 'van-dialog-bounce',
|
||||
transition: undefined,
|
||||
beforeClose: null,
|
||||
overlayClass: '',
|
||||
overlayStyle: undefined,
|
||||
|
@ -100,8 +100,7 @@ export default defineComponent({
|
||||
const updateShow = (show: boolean) => emit('update:show', show);
|
||||
|
||||
const emitClose = () => {
|
||||
callInterceptor({
|
||||
interceptor: props.beforeClose,
|
||||
callInterceptor(props.beforeClose, {
|
||||
args: [state.active],
|
||||
done: () => updateShow(false),
|
||||
});
|
||||
|
@ -113,8 +113,7 @@ export default defineComponent({
|
||||
|
||||
const close = () => {
|
||||
if (opened) {
|
||||
callInterceptor({
|
||||
interceptor: props.beforeClose,
|
||||
callInterceptor(props.beforeClose, {
|
||||
done() {
|
||||
opened = false;
|
||||
emit('close');
|
||||
|
@ -128,9 +128,8 @@ export default defineComponent({
|
||||
|
||||
const setValue = (value: string | number) => {
|
||||
if (props.beforeChange) {
|
||||
callInterceptor({
|
||||
callInterceptor(props.beforeChange, {
|
||||
args: [value],
|
||||
interceptor: props.beforeChange,
|
||||
done() {
|
||||
current.value = value;
|
||||
},
|
||||
|
@ -162,8 +162,7 @@ export default defineComponent({
|
||||
emit('click', position);
|
||||
|
||||
if (opened && !lockClick) {
|
||||
callInterceptor({
|
||||
interceptor: props.beforeClose,
|
||||
callInterceptor(props.beforeClose, {
|
||||
args: [
|
||||
{
|
||||
name: props.name,
|
||||
|
@ -83,8 +83,7 @@ export default defineComponent({
|
||||
|
||||
const setActive = (active: number | string) => {
|
||||
if (active !== props.modelValue) {
|
||||
callInterceptor({
|
||||
interceptor: props.beforeChange,
|
||||
callInterceptor(props.beforeChange, {
|
||||
args: [active],
|
||||
done() {
|
||||
emit('update:modelValue', active);
|
||||
|
@ -307,8 +307,7 @@ export default defineComponent({
|
||||
// should be removed in next major version
|
||||
emit('disabled', name, title);
|
||||
} else {
|
||||
callInterceptor({
|
||||
interceptor: props.beforeChange,
|
||||
callInterceptor(props.beforeChange, {
|
||||
args: [name],
|
||||
done: () => {
|
||||
setCurrentIndex(index);
|
||||
|
@ -60,8 +60,7 @@ export default defineComponent({
|
||||
const onDelete = (event: MouseEvent) => {
|
||||
const { name, item, index, beforeDelete } = props;
|
||||
event.stopPropagation();
|
||||
callInterceptor({
|
||||
interceptor: beforeDelete,
|
||||
callInterceptor(beforeDelete, {
|
||||
args: [item, { name, index }],
|
||||
done: () => emit('delete'),
|
||||
});
|
||||
|
@ -3,17 +3,21 @@ import { isPromise } from './validate';
|
||||
|
||||
export type Interceptor = (...args: any[]) => Promise<boolean> | boolean;
|
||||
|
||||
export function callInterceptor(options: {
|
||||
interceptor?: Interceptor;
|
||||
args?: any[];
|
||||
done: () => void;
|
||||
canceled?: () => void;
|
||||
}) {
|
||||
const { interceptor, args, done, canceled } = options;
|
||||
|
||||
export function callInterceptor(
|
||||
interceptor: Interceptor | undefined,
|
||||
{
|
||||
args = [],
|
||||
done,
|
||||
canceled,
|
||||
}: {
|
||||
args?: any[];
|
||||
done: () => void;
|
||||
canceled?: () => void;
|
||||
}
|
||||
) {
|
||||
if (interceptor) {
|
||||
// eslint-disable-next-line prefer-spread
|
||||
const returnVal = interceptor.apply(null, args || []);
|
||||
const returnVal = interceptor.apply(null, args);
|
||||
|
||||
if (isPromise(returnVal)) {
|
||||
returnVal
|
||||
|
@ -3,50 +3,47 @@ import { callInterceptor } from '../interceptor';
|
||||
|
||||
test('callInterceptor', async () => {
|
||||
const done = jest.fn();
|
||||
callInterceptor({ done });
|
||||
callInterceptor(undefined, { done });
|
||||
expect(done).toHaveBeenCalledTimes(1);
|
||||
|
||||
callInterceptor({
|
||||
interceptor: () => false,
|
||||
callInterceptor(() => false, {
|
||||
done,
|
||||
});
|
||||
expect(done).toHaveBeenCalledTimes(1);
|
||||
|
||||
callInterceptor({
|
||||
interceptor: () => true,
|
||||
callInterceptor(() => true, {
|
||||
done,
|
||||
});
|
||||
expect(done).toHaveBeenCalledTimes(2);
|
||||
|
||||
callInterceptor({
|
||||
interceptor: () => Promise.resolve(false),
|
||||
callInterceptor(() => Promise.resolve(false), {
|
||||
done,
|
||||
});
|
||||
await later();
|
||||
expect(done).toHaveBeenCalledTimes(2);
|
||||
|
||||
callInterceptor({
|
||||
interceptor: () => Promise.resolve(true),
|
||||
callInterceptor(() => Promise.resolve(true), {
|
||||
done,
|
||||
});
|
||||
await later();
|
||||
expect(done).toHaveBeenCalledTimes(3);
|
||||
|
||||
callInterceptor({
|
||||
interceptor: () => Promise.reject(),
|
||||
callInterceptor(() => Promise.reject(), {
|
||||
done,
|
||||
});
|
||||
await later();
|
||||
expect(done).toHaveBeenCalledTimes(3);
|
||||
|
||||
callInterceptor({
|
||||
interceptor: (...args) => {
|
||||
callInterceptor(
|
||||
(...args) => {
|
||||
expect(args).toEqual(['foo']);
|
||||
return false;
|
||||
},
|
||||
args: ['foo'],
|
||||
done,
|
||||
});
|
||||
{
|
||||
args: ['foo'],
|
||||
done,
|
||||
}
|
||||
);
|
||||
|
||||
expect(done).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user