mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore: split function call (#8424)
This commit is contained in:
parent
eec186ac19
commit
62eda87c99
130
src/dialog/function-call.tsx
Normal file
130
src/dialog/function-call.tsx
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
import { App, CSSProperties, TeleportProps } from 'vue';
|
||||||
|
import { inBrowser, ComponentInstance, withInstall } from '../utils';
|
||||||
|
import { Interceptor } from '../utils/interceptor';
|
||||||
|
import { mountComponent, usePopupState } from '../utils/mount-component';
|
||||||
|
import VanDialog, {
|
||||||
|
DialogTheme,
|
||||||
|
DialogAction,
|
||||||
|
DialogMessage,
|
||||||
|
DialogMessageAlign,
|
||||||
|
} from './Dialog';
|
||||||
|
|
||||||
|
export type DialogOptions = {
|
||||||
|
title?: string;
|
||||||
|
width?: string | number;
|
||||||
|
theme?: DialogTheme;
|
||||||
|
message?: DialogMessage;
|
||||||
|
overlay?: boolean;
|
||||||
|
teleport?: TeleportProps['to'];
|
||||||
|
className?: unknown;
|
||||||
|
allowHtml?: boolean;
|
||||||
|
lockScroll?: boolean;
|
||||||
|
transition?: string;
|
||||||
|
beforeClose?: Interceptor;
|
||||||
|
messageAlign?: DialogMessageAlign;
|
||||||
|
overlayClass?: string;
|
||||||
|
overlayStyle?: CSSProperties;
|
||||||
|
closeOnPopstate?: boolean;
|
||||||
|
cancelButtonText?: string;
|
||||||
|
showCancelButton?: boolean;
|
||||||
|
showConfirmButton?: boolean;
|
||||||
|
cancelButtonColor?: string;
|
||||||
|
confirmButtonText?: string;
|
||||||
|
confirmButtonColor?: string;
|
||||||
|
closeOnClickOverlay?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
let instance: ComponentInstance;
|
||||||
|
|
||||||
|
function initInstance() {
|
||||||
|
const Wrapper = {
|
||||||
|
setup() {
|
||||||
|
const { state, toggle } = usePopupState();
|
||||||
|
return () => <VanDialog {...{ ...state, 'onUpdate:show': toggle }} />;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
({ instance } = mountComponent(Wrapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
function Dialog(options: DialogOptions) {
|
||||||
|
/* istanbul ignore if */
|
||||||
|
if (!inBrowser) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!instance) {
|
||||||
|
initInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.open({
|
||||||
|
...Dialog.currentOptions,
|
||||||
|
...options,
|
||||||
|
callback: (action: DialogAction) => {
|
||||||
|
(action === 'confirm' ? resolve : reject)(action);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Dialog.defaultOptions = {
|
||||||
|
title: '',
|
||||||
|
width: '',
|
||||||
|
theme: null,
|
||||||
|
message: '',
|
||||||
|
overlay: true,
|
||||||
|
callback: null,
|
||||||
|
teleport: 'body',
|
||||||
|
className: '',
|
||||||
|
allowHtml: false,
|
||||||
|
lockScroll: true,
|
||||||
|
transition: 'van-dialog-bounce',
|
||||||
|
beforeClose: null,
|
||||||
|
overlayClass: '',
|
||||||
|
overlayStyle: undefined,
|
||||||
|
messageAlign: '',
|
||||||
|
cancelButtonText: '',
|
||||||
|
cancelButtonColor: null,
|
||||||
|
confirmButtonText: '',
|
||||||
|
confirmButtonColor: null,
|
||||||
|
showConfirmButton: true,
|
||||||
|
showCancelButton: false,
|
||||||
|
closeOnPopstate: true,
|
||||||
|
closeOnClickOverlay: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
Dialog.currentOptions = {
|
||||||
|
...Dialog.defaultOptions,
|
||||||
|
};
|
||||||
|
|
||||||
|
Dialog.alert = Dialog;
|
||||||
|
|
||||||
|
Dialog.confirm = (options: DialogOptions) =>
|
||||||
|
Dialog({
|
||||||
|
showCancelButton: true,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
|
||||||
|
Dialog.close = () => {
|
||||||
|
if (instance) {
|
||||||
|
instance.toggle(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Dialog.setDefaultOptions = (options: DialogOptions) => {
|
||||||
|
Object.assign(Dialog.currentOptions, options);
|
||||||
|
};
|
||||||
|
|
||||||
|
Dialog.resetDefaultOptions = () => {
|
||||||
|
Dialog.currentOptions = { ...Dialog.defaultOptions };
|
||||||
|
};
|
||||||
|
|
||||||
|
Dialog.install = (app: App) => {
|
||||||
|
app.use(withInstall<typeof VanDialog>(VanDialog));
|
||||||
|
app.config.globalProperties.$dialog = Dialog;
|
||||||
|
};
|
||||||
|
|
||||||
|
Dialog.Component = withInstall<typeof VanDialog>(VanDialog);
|
||||||
|
|
||||||
|
export { Dialog };
|
@ -1,132 +1,6 @@
|
|||||||
import { App, CSSProperties, TeleportProps } from 'vue';
|
import { Dialog, DialogOptions } from './function-call';
|
||||||
import { inBrowser, ComponentInstance, withInstall } from '../utils';
|
import type { DialogTheme, DialogMessage, DialogMessageAlign } from './Dialog';
|
||||||
import { Interceptor } from '../utils/interceptor';
|
|
||||||
import { mountComponent, usePopupState } from '../utils/mount-component';
|
|
||||||
import VanDialog, {
|
|
||||||
DialogTheme,
|
|
||||||
DialogAction,
|
|
||||||
DialogMessage,
|
|
||||||
DialogMessageAlign,
|
|
||||||
} from './Dialog';
|
|
||||||
|
|
||||||
export type DialogOptions = {
|
|
||||||
title?: string;
|
|
||||||
width?: string | number;
|
|
||||||
theme?: DialogTheme;
|
|
||||||
message?: DialogMessage;
|
|
||||||
overlay?: boolean;
|
|
||||||
teleport?: TeleportProps['to'];
|
|
||||||
className?: unknown;
|
|
||||||
allowHtml?: boolean;
|
|
||||||
lockScroll?: boolean;
|
|
||||||
transition?: string;
|
|
||||||
beforeClose?: Interceptor;
|
|
||||||
messageAlign?: DialogMessageAlign;
|
|
||||||
overlayClass?: string;
|
|
||||||
overlayStyle?: CSSProperties;
|
|
||||||
closeOnPopstate?: boolean;
|
|
||||||
cancelButtonText?: string;
|
|
||||||
showCancelButton?: boolean;
|
|
||||||
showConfirmButton?: boolean;
|
|
||||||
cancelButtonColor?: string;
|
|
||||||
confirmButtonText?: string;
|
|
||||||
confirmButtonColor?: string;
|
|
||||||
closeOnClickOverlay?: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
let instance: ComponentInstance;
|
|
||||||
|
|
||||||
function initInstance() {
|
|
||||||
const Wrapper = {
|
|
||||||
setup() {
|
|
||||||
const { state, toggle } = usePopupState();
|
|
||||||
return () => <VanDialog {...{ ...state, 'onUpdate:show': toggle }} />;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
({ instance } = mountComponent(Wrapper));
|
|
||||||
}
|
|
||||||
|
|
||||||
function Dialog(options: DialogOptions) {
|
|
||||||
/* istanbul ignore if */
|
|
||||||
if (!inBrowser) {
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (!instance) {
|
|
||||||
initInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
instance.open({
|
|
||||||
...Dialog.currentOptions,
|
|
||||||
...options,
|
|
||||||
callback: (action: DialogAction) => {
|
|
||||||
(action === 'confirm' ? resolve : reject)(action);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Dialog.defaultOptions = {
|
|
||||||
title: '',
|
|
||||||
width: '',
|
|
||||||
theme: null,
|
|
||||||
message: '',
|
|
||||||
overlay: true,
|
|
||||||
callback: null,
|
|
||||||
teleport: 'body',
|
|
||||||
className: '',
|
|
||||||
allowHtml: false,
|
|
||||||
lockScroll: true,
|
|
||||||
transition: 'van-dialog-bounce',
|
|
||||||
beforeClose: null,
|
|
||||||
overlayClass: '',
|
|
||||||
overlayStyle: undefined,
|
|
||||||
messageAlign: '',
|
|
||||||
cancelButtonText: '',
|
|
||||||
cancelButtonColor: null,
|
|
||||||
confirmButtonText: '',
|
|
||||||
confirmButtonColor: null,
|
|
||||||
showConfirmButton: true,
|
|
||||||
showCancelButton: false,
|
|
||||||
closeOnPopstate: true,
|
|
||||||
closeOnClickOverlay: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
Dialog.currentOptions = {
|
|
||||||
...Dialog.defaultOptions,
|
|
||||||
};
|
|
||||||
|
|
||||||
Dialog.alert = Dialog;
|
|
||||||
|
|
||||||
Dialog.confirm = (options: DialogOptions) =>
|
|
||||||
Dialog({
|
|
||||||
showCancelButton: true,
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
|
|
||||||
Dialog.close = () => {
|
|
||||||
if (instance) {
|
|
||||||
instance.toggle(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Dialog.setDefaultOptions = (options: DialogOptions) => {
|
|
||||||
Object.assign(Dialog.currentOptions, options);
|
|
||||||
};
|
|
||||||
|
|
||||||
Dialog.resetDefaultOptions = () => {
|
|
||||||
Dialog.currentOptions = { ...Dialog.defaultOptions };
|
|
||||||
};
|
|
||||||
|
|
||||||
Dialog.install = (app: App) => {
|
|
||||||
app.use(withInstall<typeof VanDialog>(VanDialog));
|
|
||||||
app.config.globalProperties.$dialog = Dialog;
|
|
||||||
};
|
|
||||||
|
|
||||||
Dialog.Component = withInstall<typeof VanDialog>(VanDialog);
|
|
||||||
|
|
||||||
export default Dialog;
|
export default Dialog;
|
||||||
export { Dialog };
|
export { Dialog };
|
||||||
export type { DialogTheme, DialogMessage, DialogMessageAlign };
|
export type { DialogTheme, DialogMessage, DialogOptions, DialogMessageAlign };
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { createApp } from 'vue';
|
import { createApp } from 'vue';
|
||||||
import { later } from '../../../test';
|
import { later } from '../../../test';
|
||||||
import { Dialog } from '..';
|
import { Dialog } from '../function-call';
|
||||||
import DialogComponent from '../Dialog';
|
import DialogComponent from '../Dialog';
|
||||||
|
|
||||||
test('should update default options when calling setDefaultOptions method', () => {
|
test('should update default options when calling setDefaultOptions method', () => {
|
||||||
|
@ -103,5 +103,4 @@ ImagePreview.install = (app: App) => {
|
|||||||
app.use(withInstall<typeof VanImagePreview>(VanImagePreview));
|
app.use(withInstall<typeof VanImagePreview>(VanImagePreview));
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ImagePreview;
|
|
||||||
export { ImagePreview };
|
export { ImagePreview };
|
5
src/image-preview/index.ts
Normal file
5
src/image-preview/index.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { ImagePreview, ImagePreviewOptions } from './function-call';
|
||||||
|
|
||||||
|
export default ImagePreview;
|
||||||
|
export { ImagePreview };
|
||||||
|
export type { ImagePreviewOptions };
|
@ -1,5 +1,5 @@
|
|||||||
import { createApp } from 'vue';
|
import { createApp } from 'vue';
|
||||||
import { ImagePreview } from '..';
|
import { ImagePreview } from '../function-call';
|
||||||
import ImagePreviewComponent from '../ImagePreview';
|
import ImagePreviewComponent from '../ImagePreview';
|
||||||
|
|
||||||
test('should expose ImagePreviewComponent in ImagePreview.Component', () => {
|
test('should expose ImagePreviewComponent in ImagePreview.Component', () => {
|
@ -7,7 +7,7 @@ import {
|
|||||||
triggerDrag,
|
triggerDrag,
|
||||||
mockGetBoundingClientRect,
|
mockGetBoundingClientRect,
|
||||||
} from '../../../test';
|
} from '../../../test';
|
||||||
import { ImagePreview } from '..';
|
import { ImagePreview } from '../function-call';
|
||||||
import ImagePreviewComponent from '../ImagePreview';
|
import ImagePreviewComponent from '../ImagePreview';
|
||||||
|
|
||||||
const images = [
|
const images = [
|
||||||
|
@ -103,5 +103,4 @@ Notify.install = (app: App) => {
|
|||||||
|
|
||||||
Notify.Component = withInstall<typeof VanNotify>(VanNotify);
|
Notify.Component = withInstall<typeof VanNotify>(VanNotify);
|
||||||
|
|
||||||
export default Notify;
|
|
||||||
export { Notify };
|
export { Notify };
|
6
src/notify/index.ts
Normal file
6
src/notify/index.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { Notify, NotifyOptions } from './function-call';
|
||||||
|
import type { NotifyType } from './Notify';
|
||||||
|
|
||||||
|
export default Notify;
|
||||||
|
export { Notify };
|
||||||
|
export type { NotifyType, NotifyOptions };
|
@ -1,7 +1,7 @@
|
|||||||
import { createApp } from 'vue';
|
import { createApp } from 'vue';
|
||||||
import { later } from '../../../test';
|
import { later } from '../../../test';
|
||||||
import { trigger } from '../../utils';
|
import { trigger } from '../../utils';
|
||||||
import { Notify } from '..';
|
import { Notify } from '../function-call';
|
||||||
import NotifyComponent from '../Notify';
|
import NotifyComponent from '../Notify';
|
||||||
|
|
||||||
test('should not throw error if calling clear method before render notify', () => {
|
test('should not throw error if calling clear method before render notify', () => {
|
||||||
|
@ -185,6 +185,4 @@ Toast.install = (app: App) => {
|
|||||||
app.config.globalProperties.$toast = Toast;
|
app.config.globalProperties.$toast = Toast;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Toast;
|
|
||||||
export { Toast };
|
export { Toast };
|
||||||
export type { ToastType, ToastPosition };
|
|
6
src/toast/index.ts
Normal file
6
src/toast/index.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { Toast, ToastOptions } from './function-call';
|
||||||
|
import type { ToastType, ToastPosition } from './Toast';
|
||||||
|
|
||||||
|
export default Toast;
|
||||||
|
export { Toast };
|
||||||
|
export type { ToastType, ToastOptions, ToastPosition };
|
@ -1,7 +1,7 @@
|
|||||||
import { later } from '../../../test';
|
|
||||||
import Toast from '../index';
|
|
||||||
import ToastComponent from '../Toast';
|
|
||||||
import { createApp } from 'vue';
|
import { createApp } from 'vue';
|
||||||
|
import { later } from '../../../test';
|
||||||
|
import { Toast } from '../function-call';
|
||||||
|
import ToastComponent from '../Toast';
|
||||||
|
|
||||||
test('toast disappeared after duration', async () => {
|
test('toast disappeared after duration', async () => {
|
||||||
const onClose = jest.fn();
|
const onClose = jest.fn();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user