From 3f56b7f1ae21e9a9c71f6f9d60d1d47664b9a8a6 Mon Sep 17 00:00:00 2001 From: 07akioni <07akioni2@gmail.com> Date: Thu, 11 Feb 2021 21:58:31 +0800 Subject: [PATCH] feat(message): tusimple theme danger type --- src/message/index.ts | 5 +++++ src/message/src/MessageProvider.ts | 10 +++++----- src/message/src/use-message.ts | 7 +++++-- themes/tusimple/src/index.ts | 1 + themes/tusimple/src/use-ts-message.ts | 28 +++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 themes/tusimple/src/use-ts-message.ts diff --git a/src/message/index.ts b/src/message/index.ts index d83a6feb7..d3af63eac 100644 --- a/src/message/index.ts +++ b/src/message/index.ts @@ -1,2 +1,7 @@ export { default as NMessagePrivider } from './src/MessageProvider' export { useMessage } from './src/use-message' +export type { + MessageOptions, + MessageApiInjection, + MessageReactive +} from './src/MessageProvider' diff --git a/src/message/src/MessageProvider.ts b/src/message/src/MessageProvider.ts index 9d72582af..959a8d334 100644 --- a/src/message/src/MessageProvider.ts +++ b/src/message/src/MessageProvider.ts @@ -23,11 +23,11 @@ export interface MessageOptions { } export interface MessageApiInjection { - info: (content: string, options: MessageOptions) => void - success: (content: string, options: MessageOptions) => void - warning: (content: string, options: MessageOptions) => void - error: (content: string, options: MessageOptions) => void - loading: (content: string, options: MessageOptions) => void + info: (content: string, options: MessageOptions) => MessageReactive + success: (content: string, options: MessageOptions) => MessageReactive + warning: (content: string, options: MessageOptions) => MessageReactive + error: (content: string, options: MessageOptions) => MessageReactive + loading: (content: string, options: MessageOptions) => MessageReactive } export interface MessageReactive { diff --git a/src/message/src/use-message.ts b/src/message/src/use-message.ts index 20eaf7cc9..84cf42eec 100644 --- a/src/message/src/use-message.ts +++ b/src/message/src/use-message.ts @@ -1,6 +1,9 @@ import { inject } from 'vue' +import { throwError } from '../../_utils' import type { MessageApiInjection } from './MessageProvider' -export function useMessage (): MessageApiInjection | undefined { - return inject('message') +export function useMessage (): MessageApiInjection { + const api = inject('message') + if (api === undefined) { throwError('use-message', 'No out founded.') } + return api } diff --git a/themes/tusimple/src/index.ts b/themes/tusimple/src/index.ts index 5d98d38a9..31908c3dc 100644 --- a/themes/tusimple/src/index.ts +++ b/themes/tusimple/src/index.ts @@ -1,2 +1,3 @@ export { default as TsConfigProvider } from './ts-config-provider' export { useDialog } from './use-ts-dialog' +export { useMessage } from './use-ts-message' diff --git a/themes/tusimple/src/use-ts-message.ts b/themes/tusimple/src/use-ts-message.ts new file mode 100644 index 000000000..5d66defec --- /dev/null +++ b/themes/tusimple/src/use-ts-message.ts @@ -0,0 +1,28 @@ +import { + useMessage as _useMessage, + MessageOptions, + MessageReactive, + MessageApiInjection +} from 'naive-ui' +import { icons } from './icons' + +export interface ExtendedApi { + danger: (content: string, options: MessageOptions) => MessageReactive +} + +export type TsMessageApi = MessageApiInjection & ExtendedApi + +function useMessage (): TsMessageApi { + const messageApi = _useMessage() + const extendedApi: ExtendedApi = { + danger: (content: string, options: MessageOptions) => { + return messageApi.error(content, { + ...options, + icon: icons.warning + }) + } + } + return Object.assign(extendedApi, messageApi) +} + +export { useMessage }