refactor: message to ts

pull/2992/head
Amour1688 2020-10-14 11:40:45 +08:00
parent bae31bc9a2
commit c732d09745
2 changed files with 64 additions and 25 deletions

View File

@ -1,3 +1,4 @@
import { VNodeTypes } from 'vue';
import Notification from '../vc-notification'; import Notification from '../vc-notification';
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined'; import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled'; import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled';
@ -6,15 +7,15 @@ import CheckCircleFilled from '@ant-design/icons-vue/CheckCircleFilled';
import InfoCircleFilled from '@ant-design/icons-vue/InfoCircleFilled'; import InfoCircleFilled from '@ant-design/icons-vue/InfoCircleFilled';
let defaultDuration = 3; let defaultDuration = 3;
let defaultTop; let defaultTop: number;
let messageInstance; let messageInstance: any;
let key = 1; let key = 1;
let prefixCls = 'ant-message'; let prefixCls = 'ant-message';
let transitionName = 'move-up'; let transitionName = 'move-up';
let getContainer = () => document.body; let getContainer = () => document.body;
let maxCount; let maxCount: number;
function getMessageInstance(callback) { function getMessageInstance(callback: (i: any) => void) {
if (messageInstance) { if (messageInstance) {
callback(messageInstance); callback(messageInstance);
return; return;
@ -27,7 +28,7 @@ function getMessageInstance(callback) {
getContainer, getContainer,
maxCount, maxCount,
}, },
instance => { (instance: any) => {
if (messageInstance) { if (messageInstance) {
callback(messageInstance); callback(messageInstance);
return; return;
@ -38,6 +39,12 @@ function getMessageInstance(callback) {
); );
} }
type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
export interface ThenableArgument {
(val: any): void;
}
const iconMap = { const iconMap = {
info: InfoCircleFilled, info: InfoCircleFilled,
success: CheckCircleFilled, success: CheckCircleFilled,
@ -46,7 +53,22 @@ const iconMap = {
loading: LoadingOutlined, loading: LoadingOutlined,
}; };
function notice(args) { export interface MessageType {
(): void;
then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<void>;
promise: Promise<void>;
}
export interface ArgsProps {
content: VNodeTypes;
duration: number | null;
type: NoticeType;
onClose?: () => void;
icon?: VNodeTypes;
key?: string | number;
}
function notice(args: ArgsProps): MessageType {
const duration = args.duration !== undefined ? args.duration : defaultDuration; const duration = args.duration !== undefined ? args.duration : defaultDuration;
const Icon = iconMap[args.type]; const Icon = iconMap[args.type];
const iconNode = Icon ? <Icon /> : ''; const iconNode = Icon ? <Icon /> : '';
@ -69,8 +91,8 @@ function notice(args) {
<div <div
class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`} class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}
> >
{args.icon ? (typeof args.icon === 'function' ? args.icon() : args.icon) : iconNode} {args.icon || iconNode}
<span>{typeof args.content === 'function' ? args.content() : args.content}</span> <span>{args.content}</span>
</div> </div>
); );
}, },
@ -78,35 +100,40 @@ function notice(args) {
}); });
}); });
}); });
const result = () => { const result: any = () => {
if (messageInstance) { if (messageInstance) {
messageInstance.removeNotice(target); messageInstance.removeNotice(target);
} }
}; };
result.then = (filled, rejected) => closePromise.then(filled, rejected); result.then = (filled: ThenableArgument, rejected: ThenableArgument) =>
closePromise.then(filled, rejected);
result.promise = closePromise; result.promise = closePromise;
return result; return result;
} }
// type ConfigContent = React.ReactNode | string; type ConfigDuration = number | (() => void);
// type ConfigDuration = number | (() => void); type JointContent = VNodeTypes | ArgsProps;
// export type ConfigOnClose = () => void; export type ConfigOnClose = () => void;
function isArgsProps(content) { function isArgsProps(content: JointContent): content is ArgsProps {
return Object.prototype.toString.call(content) === '[object Object]' && !!content.content; return (
Object.prototype.toString.call(content) === '[object Object]' &&
!!(content as ArgsProps).content
);
} }
// export interface ConfigOptions { export interface ConfigOptions {
// top?: number; top?: number;
// duration?: number; duration?: number;
// prefixCls?: string; prefixCls?: string;
// getContainer?: () => HTMLElement; getContainer?: () => HTMLElement;
// transitionName?: string; transitionName?: string;
// } maxCount?: number;
}
const api = { const api: any = {
open: notice, open: notice,
config(options) { config(options: ConfigOptions) {
if (options.top !== undefined) { if (options.top !== undefined) {
defaultTop = options.top; defaultTop = options.top;
messageInstance = null; // delete messageInstance for new defaultTop messageInstance = null; // delete messageInstance for new defaultTop
@ -138,7 +165,7 @@ const api = {
}; };
['success', 'info', 'warning', 'error', 'loading'].forEach(type => { ['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
api[type] = (content, duration, onClose) => { api[type] = (content: JointContent, duration: ConfigDuration, onClose?: ConfigOnClose) => {
if (isArgsProps(content)) { if (isArgsProps(content)) {
return api.open({ ...content, type }); return api.open({ ...content, type });
} }
@ -152,4 +179,16 @@ const api = {
api.warn = api.warning; api.warn = api.warning;
export interface MessageApi {
info(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
success(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
error(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warn(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warning(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
loading(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
open(args: ArgsProps): MessageType;
config(options: ConfigOptions): void;
destroy(): void;
}
export default api; export default api;