From df29c56d71a38fc9f2dee5a3211515e8779b08b7 Mon Sep 17 00:00:00 2001
From: tangjinzhou <415800467@qq.com>
Date: Tue, 6 Feb 2018 16:43:59 +0800
Subject: [PATCH] add message
---
components/index.js | 2 +
components/message/demo/duration.md | 26 ++++++
components/message/demo/index.vue | 38 +++++++++
components/message/demo/info.md | 26 ++++++
components/message/demo/loading.md | 27 ++++++
components/message/demo/other.md | 36 ++++++++
components/message/index.en-US.md | 36 ++++++++
components/message/index.js | 126 ++++++++++++++++++++++++++++
components/message/index.zh-CN.md | 36 ++++++++
components/message/style/index.js | 2 +
components/message/style/index.less | 74 ++++++++++++++++
components/style.js | 1 +
examples/demo.js | 1 +
examples/routes.js | 2 +-
webpack.base.config.js | 10 ++-
15 files changed, 439 insertions(+), 4 deletions(-)
create mode 100644 components/message/demo/duration.md
create mode 100644 components/message/demo/index.vue
create mode 100644 components/message/demo/info.md
create mode 100644 components/message/demo/loading.md
create mode 100644 components/message/demo/other.md
create mode 100644 components/message/index.en-US.md
create mode 100644 components/message/index.js
create mode 100644 components/message/index.zh-CN.md
create mode 100644 components/message/style/index.js
create mode 100644 components/message/style/index.less
diff --git a/components/index.js b/components/index.js
index f5cb081d8..534188583 100644
--- a/components/index.js
+++ b/components/index.js
@@ -61,8 +61,10 @@ const CollapsePanel = Collapse.Panel
export { Collapse, CollapsePanel }
import notification from './notification'
+import message from './message'
const api = {
notification,
+ message,
}
export { api }
diff --git a/components/message/demo/duration.md b/components/message/demo/duration.md
new file mode 100644
index 000000000..53f086364
--- /dev/null
+++ b/components/message/demo/duration.md
@@ -0,0 +1,26 @@
+
+
+#### 修改延时
+自定义时长 `10s`,默认时长为 `3s`。
+
+
+
+#### Customize duration
+Customize message display duration from default `3s` to `10s`.
+
+
+```html
+
+ Customized display duration
+
+
+```
+
diff --git a/components/message/demo/index.vue b/components/message/demo/index.vue
new file mode 100644
index 000000000..d76dc0d5f
--- /dev/null
+++ b/components/message/demo/index.vue
@@ -0,0 +1,38 @@
+
diff --git a/components/message/demo/info.md b/components/message/demo/info.md
new file mode 100644
index 000000000..88a7e5763
--- /dev/null
+++ b/components/message/demo/info.md
@@ -0,0 +1,26 @@
+
+
+#### 普通提示
+信息提醒反馈。
+
+
+
+#### Normal prompt
+Normal messages as feedbacks.
+
+
+```html
+
+ Display normal message
+
+
+```
+
diff --git a/components/message/demo/loading.md b/components/message/demo/loading.md
new file mode 100644
index 000000000..e849b30c1
--- /dev/null
+++ b/components/message/demo/loading.md
@@ -0,0 +1,27 @@
+
+
+#### 加载中
+进行全局 loading,异步自行移除。
+
+
+
+#### Message of loading
+Display a global loading indicator, which is dismissed by itself asynchronously.
+
+
+```html
+
+ Display a loading indicator
+
+
+```
+
diff --git a/components/message/demo/other.md b/components/message/demo/other.md
new file mode 100644
index 000000000..b2fc8f3a8
--- /dev/null
+++ b/components/message/demo/other.md
@@ -0,0 +1,36 @@
+
+
+#### 其他提示类型
+包括成功、失败、警告。
+
+
+
+#### Other types of message
+Messages of success, error and warning types.
+
+
+```html
+
+
+
Success
+
Error
+
Warning
+
+
+
+```
+
diff --git a/components/message/index.en-US.md b/components/message/index.en-US.md
new file mode 100644
index 000000000..d7e0bb122
--- /dev/null
+++ b/components/message/index.en-US.md
@@ -0,0 +1,36 @@
+## API
+
+This components provides some static methods, with usage and arguments as following:
+
+- `message.success(content, [duration], onClose)`
+- `message.error(content, [duration], onClose)`
+- `message.info(content, [duration], onClose)`
+- `message.warning(content, [duration], onClose)`
+- `message.warn(content, [duration], onClose)` // alias of warning
+- `message.loading(content, [duration], onClose)`
+
+| Argument | Description | Type | Default |
+| -------- | ----------- | ---- | ------- |
+| content | content of the message | string\|vueNode \|function(h) | - |
+| duration | time before auto-dismiss, in seconds | number | 1.5 |
+| onClose | Specify a function that will be called when the message is closed | Function | - |
+
+Methods for global configuration and destruction are also provided:
+
+- `message.config(options)`
+- `message.destroy()`
+
+### message.config
+
+```js
+message.config({
+ top: '100px',
+ duration: 2,
+});
+```
+
+| Argument | Description | Type | Default |
+| -------- | ----------- | ---- | ------- |
+| duration | time before auto-dismiss, in seconds | number | 1.5 |
+| getContainer | Return the mount node for Message | () => HTMLElement | () => document.body |
+| top | distance from top | string | `24px` |
diff --git a/components/message/index.js b/components/message/index.js
new file mode 100644
index 000000000..86536b150
--- /dev/null
+++ b/components/message/index.js
@@ -0,0 +1,126 @@
+import Notification from '../vc-notification'
+import Icon from '../icon'
+
+let defaultDuration = 3
+let defaultTop = null
+let messageInstance = null
+let key = 1
+let prefixCls = 'ant-message'
+let getContainer = () => document.body
+
+function getMessageInstance (callback) {
+ if (messageInstance) {
+ callback(messageInstance)
+ return
+ }
+ Notification.newInstance({
+ prefixCls,
+ transitionName: 'move-up',
+ style: { top: defaultTop }, // 覆盖原来的样式
+ getContainer,
+ }, (instance) => {
+ if (messageInstance) {
+ callback(messageInstance)
+ return
+ }
+ messageInstance = instance
+ callback(instance)
+ })
+}
+
+// type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
+
+function notice (
+ content,
+ duration,
+ type,
+ onClose,
+) {
+ const iconType = ({
+ info: 'info-circle',
+ success: 'check-circle',
+ error: 'cross-circle',
+ warning: 'exclamation-circle',
+ loading: 'loading',
+ })[type]
+
+ if (typeof duration === 'function') {
+ onClose = duration
+ duration = defaultDuration
+ }
+
+ const target = key++
+ getMessageInstance((instance) => {
+ instance.notice({
+ key: target,
+ duration,
+ style: {},
+ content: (h) => (
+
+
+ {typeof content === 'function' ? content(h) : content}
+
+ ),
+ onClose,
+ })
+ })
+ return () => {
+ if (messageInstance) {
+ messageInstance.removeNotice(target)
+ }
+ }
+}
+
+// type ConfigContent = React.ReactNode | string;
+// type ConfigDuration = number | (() => void);
+// export type ConfigOnClose = () => void;
+
+// export interface ConfigOptions {
+// top?: number;
+// duration?: number;
+// prefixCls?: string;
+// getContainer?: () => HTMLElement;
+// }
+
+export default {
+ info (content, duration, onClose) {
+ return notice(content, duration, 'info', onClose)
+ },
+ success (content, duration, onClose) {
+ return notice(content, duration, 'success', onClose)
+ },
+ error (content, duration, onClose) {
+ return notice(content, duration, 'error', onClose)
+ },
+ // Departed usage, please use warning()
+ warn (content, duration, onClose) {
+ return notice(content, duration, 'warning', onClose)
+ },
+ warning (content, duration, onClose) {
+ return notice(content, duration, 'warning', onClose)
+ },
+ loading (content, duration, onClose) {
+ return notice(content, duration, 'loading', onClose)
+ },
+ 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
+ }
+ },
+ destroy () {
+ if (messageInstance) {
+ messageInstance.destroy()
+ messageInstance = null
+ }
+ },
+}
diff --git a/components/message/index.zh-CN.md b/components/message/index.zh-CN.md
new file mode 100644
index 000000000..b83f7fef1
--- /dev/null
+++ b/components/message/index.zh-CN.md
@@ -0,0 +1,36 @@
+## API
+
+组件提供了一些静态方法,使用方式和参数如下:
+
+- `message.success(content, [duration], onClose)`
+- `message.error(content, [duration], onClose)`
+- `message.info(content, [duration], onClose)`
+- `message.warning(content, [duration], onClose)`
+- `message.warn(content, [duration], onClose)` // alias of warning
+- `message.loading(content, [duration], onClose)`
+
+| 参数 | 说明 | 类型 | 默认值 |
+| --- | --- | --- | --- |
+| content | 提示内容 | string\|vueNode \|function(h) | - |
+| duration | 自动关闭的延时,单位秒 | number | 3 |
+| onClose | 关闭时触发的回调函数 | Function | - |
+
+还提供了全局配置和全局销毁方法:
+
+- `message.config(options)`
+- `message.destroy()`
+
+### message.config
+
+```js
+message.config({
+ top: `100px`,
+ duration: 2,
+});
+```
+
+| 参数 | 说明 | 类型 | 默认值 |
+| --- | --- | --- | --- |
+| duration | 默认自动关闭延时,单位秒 | number | 3 |
+| getContainer | 配置渲染节点的输出位置 | () => HTMLElement | () => document.body |
+| top | 消息距离顶部的位置 | string | `24px` |
diff --git a/components/message/style/index.js b/components/message/style/index.js
new file mode 100644
index 000000000..cf31ed80f
--- /dev/null
+++ b/components/message/style/index.js
@@ -0,0 +1,2 @@
+import '../../style/index.less'
+import './index.less'
diff --git a/components/message/style/index.less b/components/message/style/index.less
new file mode 100644
index 000000000..a5209d262
--- /dev/null
+++ b/components/message/style/index.less
@@ -0,0 +1,74 @@
+@import "../../style/themes/default";
+@import "../../style/mixins/index";
+
+@message-prefix-cls: ~"@{ant-prefix}-message";
+
+.@{message-prefix-cls} {
+ .reset-component;
+ position: fixed;
+ z-index: @zindex-message;
+ width: 100%;
+ top: 16px;
+ left: 0;
+ pointer-events: none;
+
+ &-notice {
+ padding: 8px;
+ text-align: center;
+ &:first-child {
+ margin-top: -8px;
+ }
+ }
+
+ &-notice-content {
+ padding: 10px 16px;
+ border-radius: @border-radius-base;
+ box-shadow: @shadow-2;
+ background: @component-background;
+ display: inline-block;
+ pointer-events: all;
+ }
+
+ &-success .@{iconfont-css-prefix} {
+ color: @success-color;
+ }
+
+ &-error .@{iconfont-css-prefix} {
+ color: @error-color;
+ }
+
+ &-warning .@{iconfont-css-prefix} {
+ color: @warning-color;
+ }
+
+ &-info .@{iconfont-css-prefix},
+ &-loading .@{iconfont-css-prefix} {
+ color: @info-color;
+ }
+
+ .@{iconfont-css-prefix} {
+ margin-right: 8px;
+ font-size: @font-size-lg;
+ top: 1px;
+ position: relative;
+ }
+
+ &-notice.move-up-leave.move-up-leave-active {
+ animation-name: MessageMoveOut;
+ overflow: hidden;
+ animation-duration: .3s;
+ }
+}
+
+@keyframes MessageMoveOut {
+ 0% {
+ opacity: 1;
+ max-height: 150px;
+ padding: 8px;
+ }
+ 100% {
+ opacity: 0;
+ max-height: 0;
+ padding: 0;
+ }
+}
diff --git a/components/style.js b/components/style.js
index 275b52db9..fc3973e1d 100644
--- a/components/style.js
+++ b/components/style.js
@@ -19,3 +19,4 @@ import './divider/style'
import './card/style'
import './collapse/style'
import './notification/style'
+import './message/style'
diff --git a/examples/demo.js b/examples/demo.js
index 70349ef1d..0e8d60654 100644
--- a/examples/demo.js
+++ b/examples/demo.js
@@ -20,4 +20,5 @@ export { default as dropdown } from 'antd/dropdown/demo/index.vue'
export { default as divider } from 'antd/divider/demo/index.vue'
export { default as collapse } from 'antd/collapse/demo/index.vue'
export { default as notification } from 'antd/notification/demo/index.vue'
+export { default as message } from 'antd/message/demo/index.vue'
diff --git a/examples/routes.js b/examples/routes.js
index 0878c06c2..469049595 100644
--- a/examples/routes.js
+++ b/examples/routes.js
@@ -3,7 +3,7 @@ const AsyncComp = () => {
const hashs = window.location.hash.split('/')
const d = hashs[hashs.length - 1]
return {
- component: import(`../components/notification/demo/${d}.md`),
+ component: import(`../components/message/demo/${d}.md`),
}
}
export default [
diff --git a/webpack.base.config.js b/webpack.base.config.js
index 35ca4a425..4d85e4249 100644
--- a/webpack.base.config.js
+++ b/webpack.base.config.js
@@ -79,7 +79,7 @@ md.core.ruler.push('update_template', function replace ({ tokens }) {
jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle))
const codeHtml = code ? md.render(code) : ''
const cnHtml = cn ? md.render(cn) : ''
- const newContent = `
+ let newContent = `
${template}
@@ -87,13 +87,17 @@ md.core.ruler.push('update_template', function replace ({ tokens }) {
${us ? md.render(us) : ''}
${codeHtml}
-
+ `
+ newContent += script ? `
+ ` : ''
+ newContent += style ? `
`
+
+ ` : ''
const t = new Token('html_block', '', 0)
t.content = newContent
tokens.push(t)