From 89b65077d3685208f1f1212740975b5c0f62bd95 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 9 Feb 2021 21:03:11 +0800 Subject: [PATCH] types(Dialog): dialog fn use ts (#8114) --- src/dialog/Dialog.tsx | 3 +- src/dialog/{index.js => index.tsx} | 54 ++++++++++++++++++++++++------ src/toast/index.tsx | 1 + 3 files changed, 47 insertions(+), 11 deletions(-) rename src/dialog/{index.js => index.tsx} (58%) diff --git a/src/dialog/Dialog.tsx b/src/dialog/Dialog.tsx index aee9c89a4..3caef0165 100644 --- a/src/dialog/Dialog.tsx +++ b/src/dialog/Dialog.tsx @@ -13,6 +13,7 @@ import ActionBarButton from '../action-bar-button'; const [createComponent, bem, t] = createNamespace('dialog'); +export type DialogTheme = 'default' | 'round-button'; export type DialogAction = 'confirm' | 'cancel'; export type DialogMessageAlign = 'left' | 'center' | 'right'; @@ -26,7 +27,7 @@ export default createComponent({ props: { ...popupSharedProps, title: String, - theme: String, + theme: String as PropType, width: [Number, String], message: String, callback: Function as PropType<(action?: DialogAction) => void>, diff --git a/src/dialog/index.js b/src/dialog/index.tsx similarity index 58% rename from src/dialog/index.js rename to src/dialog/index.tsx index dbe8350c0..224aa239d 100644 --- a/src/dialog/index.js +++ b/src/dialog/index.tsx @@ -1,8 +1,40 @@ +import { App, TeleportProps } from 'vue'; import { inBrowser } from '../utils'; +import { Interceptor } from '../utils/interceptor'; import { mountComponent, usePopupState } from '../utils/mount-component'; -import VanDialog from './Dialog'; +import VanDialog, { + DialogTheme, + DialogAction, + DialogMessageAlign, +} from './Dialog'; -let instance; +export type DialogOptions = { + title?: string; + width?: string | number; + theme?: DialogTheme; + message?: string; + overlay?: boolean; + teleport?: TeleportProps['to']; + className?: any; + allowHtml?: boolean; + lockScroll?: boolean; + transition?: string; + beforeClose?: Interceptor; + messageAlign?: DialogMessageAlign; + overlayClass?: string; + overlayStyle?: Record; + closeOnPopstate?: boolean; + cancelButtonText?: string; + showCancelButton?: boolean; + showConfirmButton?: boolean; + cancelButtonColor?: string; + confirmButtonText?: string; + confirmButtonColor?: string; + closeOnClickOverlay?: boolean; +}; + +// TODO remove any +let instance: any; function initInstance() { const Wrapper = { @@ -15,7 +47,7 @@ function initInstance() { ({ instance } = mountComponent(Wrapper)); } -function Dialog(options) { +function Dialog(options: DialogOptions) { /* istanbul ignore if */ if (!inBrowser) { return Promise.resolve(); @@ -29,7 +61,7 @@ function Dialog(options) { instance.open({ ...Dialog.currentOptions, ...options, - callback: (action) => { + callback: (action: DialogAction) => { (action === 'confirm' ? resolve : reject)(action); }, }); @@ -62,9 +94,13 @@ Dialog.defaultOptions = { closeOnClickOverlay: false, }; +Dialog.currentOptions = { + ...Dialog.defaultOptions, +}; + Dialog.alert = Dialog; -Dialog.confirm = (options) => +Dialog.confirm = (options: DialogOptions) => Dialog({ showCancelButton: true, ...options, @@ -76,7 +112,7 @@ Dialog.close = () => { } }; -Dialog.setDefaultOptions = (options) => { +Dialog.setDefaultOptions = (options: DialogOptions) => { Object.assign(Dialog.currentOptions, options); }; @@ -84,10 +120,8 @@ Dialog.resetDefaultOptions = () => { Dialog.currentOptions = { ...Dialog.defaultOptions }; }; -Dialog.resetDefaultOptions(); - -Dialog.install = (app) => { - app.use(VanDialog); +Dialog.install = (app: App) => { + app.use(VanDialog as any); app.config.globalProperties.$dialog = Dialog; }; diff --git a/src/toast/index.tsx b/src/toast/index.tsx index 073646c54..7bf1d6360 100644 --- a/src/toast/index.tsx +++ b/src/toast/index.tsx @@ -47,6 +47,7 @@ const defaultOptions: ToastOptions = { closeOnClickOverlay: false, }; +// TODO remove any let queue: any[] = []; let allowMultiple = false; let currentOptions = { ...defaultOptions };