2018-02-06 08:43:59 +00:00
|
|
|
import Notification from '../vc-notification'
|
|
|
|
import Icon from '../icon'
|
|
|
|
|
|
|
|
let defaultDuration = 3
|
2018-09-05 13:28:54 +00:00
|
|
|
let defaultTop
|
|
|
|
let messageInstance
|
2018-02-06 08:43:59 +00:00
|
|
|
let key = 1
|
|
|
|
let prefixCls = 'ant-message'
|
2018-04-07 06:29:59 +00:00
|
|
|
let transitionName = 'move-up'
|
2018-02-06 08:43:59 +00:00
|
|
|
let getContainer = () => document.body
|
2018-09-05 13:28:54 +00:00
|
|
|
let maxCount
|
2018-02-06 08:43:59 +00:00
|
|
|
|
|
|
|
function getMessageInstance (callback) {
|
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Notification.newInstance({
|
|
|
|
prefixCls,
|
2018-04-07 06:29:59 +00:00
|
|
|
transitionName,
|
2018-02-06 08:43:59 +00:00
|
|
|
style: { top: defaultTop }, // 覆盖原来的样式
|
|
|
|
getContainer,
|
2018-09-05 13:28:54 +00:00
|
|
|
maxCount,
|
2018-02-06 08:43:59 +00:00
|
|
|
}, (instance) => {
|
|
|
|
if (messageInstance) {
|
|
|
|
callback(messageInstance)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
messageInstance = instance
|
|
|
|
callback(instance)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
|
|
|
|
|
2018-12-09 10:37:41 +00:00
|
|
|
function notice (args) {
|
|
|
|
const duration = args.duration !== undefined ? args.duration : defaultDuration
|
2018-02-06 08:43:59 +00:00
|
|
|
const iconType = ({
|
|
|
|
info: 'info-circle',
|
|
|
|
success: 'check-circle',
|
2018-12-09 10:37:41 +00:00
|
|
|
error: 'close-circle',
|
2018-02-06 08:43:59 +00:00
|
|
|
warning: 'exclamation-circle',
|
|
|
|
loading: 'loading',
|
2018-12-09 10:37:41 +00:00
|
|
|
})[args.type]
|
2018-02-06 08:43:59 +00:00
|
|
|
|
|
|
|
const target = key++
|
2018-09-05 13:28:54 +00:00
|
|
|
const closePromise = new Promise((resolve) => {
|
|
|
|
const callback = () => {
|
2018-12-09 10:37:41 +00:00
|
|
|
if (typeof args.onClose === 'function') {
|
|
|
|
args.onClose()
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
return resolve(true)
|
|
|
|
}
|
|
|
|
getMessageInstance((instance) => {
|
|
|
|
instance.notice({
|
|
|
|
key: target,
|
|
|
|
duration,
|
|
|
|
style: {},
|
|
|
|
content: (h) => (
|
2018-12-09 10:37:41 +00:00
|
|
|
<div class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}>
|
|
|
|
{args.icon
|
|
|
|
? (typeof args.icon === 'function' ? args.icon(h) : args.icon)
|
|
|
|
: (iconType ? <Icon type={iconType} theme={iconType === 'loading' ? 'outlined' : 'filled'} /> : '')}
|
|
|
|
<span>{typeof content === 'function' ? args.content(h) : args.content}</span>
|
2018-09-05 13:28:54 +00:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
onClose: callback,
|
|
|
|
})
|
2018-02-06 08:43:59 +00:00
|
|
|
})
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
const result = () => {
|
2018-02-06 08:43:59 +00:00
|
|
|
if (messageInstance) {
|
|
|
|
messageInstance.removeNotice(target)
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
result.then = (filled, rejected) => closePromise.then(filled, rejected)
|
|
|
|
result.promise = closePromise
|
|
|
|
return result
|
2018-02-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// type ConfigContent = React.ReactNode | string;
|
|
|
|
// type ConfigDuration = number | (() => void);
|
|
|
|
// export type ConfigOnClose = () => void;
|
|
|
|
|
|
|
|
// export interface ConfigOptions {
|
|
|
|
// top?: number;
|
|
|
|
// duration?: number;
|
|
|
|
// prefixCls?: string;
|
|
|
|
// getContainer?: () => HTMLElement;
|
2018-04-07 06:29:59 +00:00
|
|
|
// transitionName?: string;
|
2018-02-06 08:43:59 +00:00
|
|
|
// }
|
|
|
|
|
2018-12-09 10:37:41 +00:00
|
|
|
const api = {
|
|
|
|
open: notice,
|
2018-02-06 08:43:59 +00:00
|
|
|
config (options) {
|
|
|
|
if (options.top !== undefined) {
|
|
|
|
defaultTop = options.top
|
|
|
|
messageInstance = null // delete messageInstance for new defaultTop
|
|
|
|
}
|
|
|
|
if (options.duration !== undefined) {
|
|
|
|
defaultDuration = options.duration
|
|
|
|
}
|
|
|
|
if (options.prefixCls !== undefined) {
|
|
|
|
prefixCls = options.prefixCls
|
|
|
|
}
|
|
|
|
if (options.getContainer !== undefined) {
|
|
|
|
getContainer = options.getContainer
|
|
|
|
}
|
2018-04-07 06:29:59 +00:00
|
|
|
if (options.transitionName !== undefined) {
|
|
|
|
transitionName = options.transitionName
|
|
|
|
messageInstance = null // delete messageInstance for new transitionName
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
if (options.maxCount !== undefined) {
|
|
|
|
maxCount = options.maxCount
|
|
|
|
messageInstance = null
|
|
|
|
}
|
2018-02-06 08:43:59 +00:00
|
|
|
},
|
|
|
|
destroy () {
|
|
|
|
if (messageInstance) {
|
|
|
|
messageInstance.destroy()
|
|
|
|
messageInstance = null
|
|
|
|
}
|
|
|
|
},
|
2018-12-09 10:37:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
['success', 'info', 'warning', 'error', 'loading'].forEach((type) => {
|
|
|
|
api[type] = (content, duration, onClose) => {
|
|
|
|
if (typeof duration === 'function') {
|
|
|
|
onClose = duration
|
|
|
|
duration = undefined
|
|
|
|
}
|
|
|
|
return api.open({ content, duration: duration, type, onClose })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
api.warn = api.warning
|
|
|
|
|
|
|
|
export default api
|