types(Notify): use tsx (#8158)

This commit is contained in:
neverland 2021-02-14 19:14:47 +08:00 committed by GitHub
parent 8d379e0f00
commit c6e65f4fd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 24 deletions

View File

@ -1,17 +1,20 @@
import { createNamespace } from '../utils'; import { PropType } from 'vue';
import { createNamespace, UnknownProp } from '../utils';
import Popup, { popupSharedProps } from '../popup'; import Popup, { popupSharedProps } from '../popup';
const [createComponent, bem] = createNamespace('notify'); const [createComponent, bem] = createNamespace('notify');
export type NotifyType = 'primary' | 'success' | 'danger' | 'warning';
export default createComponent({ export default createComponent({
props: { props: {
...popupSharedProps, ...popupSharedProps,
color: String, color: String,
message: [Number, String], message: [Number, String],
className: null, className: UnknownProp,
background: String, background: String,
type: { type: {
type: String, type: String as PropType<NotifyType>,
default: 'danger', default: 'danger',
}, },
}, },

View File

@ -107,9 +107,9 @@ export default {
| color | Message color | _string_ | `white` | | | color | Message color | _string_ | `white` | |
| background | Background color | _string_ | - | | background | Background color | _string_ | - |
| className | Custom className | _string \| Array \| object_ | - | | className | Custom className | _string \| Array \| object_ | - |
| onClick | Callback function after click | _Function_ | - | | onClick | Callback function after click | _(event: MouseEvent) => void_ | - |
| onOpened | Callback function after opened | _Function_ | - | | onOpened | Callback function after opened | _() => void_ | - |
| onClose | Callback function after close | _Function_ | - | | onClose | Callback function after close | _() => void_ | - |
### Less Variables ### Less Variables

View File

@ -144,9 +144,9 @@ export default {
| color | 字体颜色 | _string_ | `white` | | color | 字体颜色 | _string_ | `white` |
| background | 背景颜色 | _string_ | - | | background | 背景颜色 | _string_ | - |
| className | 自定义类名 | _string \| Array \| object_ | - | | className | 自定义类名 | _string \| Array \| object_ | - |
| onClick | 点击时的回调函数 | _Function_ | - | | onClick | 点击时的回调函数 | _(event: MouseEvent): void_ | - |
| onOpened | 完全展示后的回调函数 | _Function_ | - | | onOpened | 完全展示后的回调函数 | _() => void_ | - |
| onClose | 关闭时的回调函数 | _Function_ | - | | onClose | 关闭时的回调函数 | _() => void_ | - |
### 样式变量 ### 样式变量

View File

@ -33,6 +33,7 @@
import { ref } from 'vue'; import { ref } from 'vue';
import { useTranslate } from '@demo/use-translate'; import { useTranslate } from '@demo/use-translate';
import Notify from '..'; import Notify from '..';
import { NotifyType } from '../Notify';
const i18n = { const i18n = {
'zh-CN': { 'zh-CN': {
@ -85,7 +86,7 @@ export default {
}); });
}; };
const showType = (type: string) => { const showType = (type: NotifyType) => {
Notify({ Notify({
message: t('content'), message: t('content'),
type, type,

View File

@ -1,11 +1,26 @@
import { isObject, inBrowser } from '../utils'; import { App } from 'vue';
import { isObject, inBrowser, ComponentInstance } from '../utils';
import { mountComponent, usePopupState } from '../utils/mount-component'; import { mountComponent, usePopupState } from '../utils/mount-component';
import VanNotify from './Notify'; import VanNotify, { NotifyType } from './Notify';
let timer; let timer: number;
let instance; let instance: ComponentInstance;
function parseOptions(message) { export type NotifyMessage = string | number;
export type NotifyOptions = {
type?: NotifyType;
color?: string;
message?: NotifyMessage;
duration?: number;
className?: unknown;
background?: string;
onClick?: (event: MouseEvent) => void;
onClose?: () => void;
onOpened?: () => void;
};
function parseOptions(message: NotifyMessage | NotifyOptions) {
return isObject(message) ? message : { message }; return isObject(message) ? message : { message };
} }
@ -25,7 +40,7 @@ function initInstance() {
})); }));
} }
function Notify(options) { function Notify(options: NotifyMessage | NotifyOptions) {
if (!inBrowser) { if (!inBrowser) {
return; return;
} }
@ -42,7 +57,7 @@ function Notify(options) {
instance.open(options); instance.open(options);
clearTimeout(timer); clearTimeout(timer);
if (options.duration > 0) { if (options.duration! > 0) {
timer = setTimeout(Notify.clear, options.duration); timer = setTimeout(Notify.clear, options.duration);
} }
@ -54,13 +69,13 @@ function defaultOptions() {
type: 'danger', type: 'danger',
color: undefined, color: undefined,
message: '', message: '',
onClose: null, onClose: undefined,
onClick: null, onClick: undefined,
onOpened: null, onOpened: undefined,
duration: 3000, duration: 3000,
className: '', className: '',
background: undefined, background: undefined,
}; } as NotifyOptions;
} }
Notify.clear = () => { Notify.clear = () => {
@ -71,7 +86,7 @@ Notify.clear = () => {
Notify.currentOptions = defaultOptions(); Notify.currentOptions = defaultOptions();
Notify.setDefaultOptions = (options) => { Notify.setDefaultOptions = (options: NotifyOptions) => {
Object.assign(Notify.currentOptions, options); Object.assign(Notify.currentOptions, options);
}; };
@ -79,8 +94,8 @@ Notify.resetDefaultOptions = () => {
Notify.currentOptions = defaultOptions(); Notify.currentOptions = defaultOptions();
}; };
Notify.install = (app) => { Notify.install = (app: App) => {
app.use(VanNotify); app.use(VanNotify as any);
app.config.globalProperties.$notify = Notify; app.config.globalProperties.$notify = Notify;
}; };