make Message and Notice support render

This commit is contained in:
zhigang.li 2017-12-20 16:19:25 +08:00
parent 8a0cb3ce80
commit b24be35a7e
8 changed files with 99 additions and 16 deletions

View file

@ -13,12 +13,19 @@
info () { info () {
// this.$Message.info(''); // this.$Message.info('');
this.$Message.success({ this.$Message.success({
content: '这是一条普通提示2', // content: '2',
duration: 500, duration: 500,
onClose () { onClose () {
// console.log(123) // console.log(123)
}, },
closable: true closable: true,
render (h) {
return h('Button',{
props: {
type: 'primary'
}
}, '这是render出来的');
}
}) })
}, },
success () { success () {

View file

@ -1,6 +1,7 @@
<template> <template>
<div> <div>
<p>带描述信息</p> <p>带描述信息</p>
<Button @click="normal(false)">普通</Button>
<Button @click="info(false)">消息</Button> <Button @click="info(false)">消息</Button>
<Button @click="success(false)">成功</Button> <Button @click="success(false)">成功</Button>
<Button @click="warning(false)">警告</Button> <Button @click="warning(false)">警告</Button>
@ -16,15 +17,30 @@
<script> <script>
export default { export default {
methods: { methods: {
normal (nodesc) {
this.$Notice.open({
title: 'google',
duration: 0,
desc: nodesc ? '' : '这里是通知描述这里,是通知描述这里是通知描述这里,是通知描述这里,是通知描述这里是通知描述这里是通知描述',
render (h) {
return h('span', {}, 'sdsdfsdf');
}
});
},
info (nodesc) { info (nodesc) {
this.$Notice.info({ this.$Notice.info({
title: '这是通知标题', // title: '',
desc: nodesc ? '' : '这里是通知描述这里,是通知描述这里是通知描述这里,是通知描述这里,是通知描述这里是通知描述这里是通知描述' duration: 0,
desc: nodesc ? '' : '这里是通知描述这里,是通知描述这里是通知描述这里,是通知描述这里,是通知描述这里是通知描述这里是通知描述',
render (h) {
return h('span', {}, 'sdsdfsdf');
}
}); });
}, },
success (nodesc) { success (nodesc) {
this.$Notice.success({ this.$Notice.success({
title: '这是通知标题', title: '这是通知标题',
duration: 0,
desc: nodesc ? '' : '这里是通知描述这里,是通知描述这里是通知描述这里,是通知描述这里,是通知描述这里是通知描述这里是通知描述' desc: nodesc ? '' : '这里是通知描述这里,是通知描述这里是通知描述这里,是通知描述这里,是通知描述这里是通知描述这里是通知描述'
}); });
}, },

View file

@ -5,7 +5,6 @@ Notification.newInstance = properties => {
const _props = properties || {}; const _props = properties || {};
const Instance = new Vue({ const Instance = new Vue({
data: _props,
render (h) { render (h) {
return h(Notification, { return h(Notification, {
props: _props props: _props

View file

@ -2,7 +2,12 @@
<transition :name="transitionName" @enter="handleEnter" @leave="handleLeave"> <transition :name="transitionName" @enter="handleEnter" @leave="handleLeave">
<div :class="classes" :style="styles"> <div :class="classes" :style="styles">
<template v-if="type === 'notice'"> <template v-if="type === 'notice'">
<div :class="[baseClass + '-content']" ref="content" v-html="content"></div> <div :class="contentClasses" ref="content" v-html="content"></div>
<div :class="contentWithIcon" ref="content">
<render-cell
:render="renderFunc"
></render-cell>
</div>
<a :class="[baseClass + '-close']" @click="close" v-if="closable"> <a :class="[baseClass + '-close']" @click="close" v-if="closable">
<i class="ivu-icon ivu-icon-ios-close-empty"></i> <i class="ivu-icon ivu-icon-ios-close-empty"></i>
</a> </a>
@ -10,6 +15,11 @@
<template v-if="type === 'message'"> <template v-if="type === 'message'">
<div :class="[baseClass + '-content']" ref="content"> <div :class="[baseClass + '-content']" ref="content">
<div :class="[baseClass + '-content-text']" v-html="content"></div> <div :class="[baseClass + '-content-text']" v-html="content"></div>
<div :class="[baseClass + '-content-text']">
<render-cell
:render="renderFunc"
></render-cell>
</div>
<a :class="[baseClass + '-close']" @click="close" v-if="closable"> <a :class="[baseClass + '-close']" @click="close" v-if="closable">
<i class="ivu-icon ivu-icon-ios-close-empty"></i> <i class="ivu-icon ivu-icon-ios-close-empty"></i>
</a> </a>
@ -19,7 +29,11 @@
</transition> </transition>
</template> </template>
<script> <script>
import RenderCell from '../render';
export default { export default {
components: {
RenderCell
},
props: { props: {
prefixCls: { prefixCls: {
type: String, type: String,
@ -36,6 +50,10 @@
type: String, type: String,
default: '' default: ''
}, },
withIcon: Boolean,
render: {
type: Function
},
styles: { styles: {
type: Object, type: Object,
default: function() { default: function() {
@ -71,6 +89,9 @@
baseClass () { baseClass () {
return `${this.prefixCls}-notice`; return `${this.prefixCls}-notice`;
}, },
renderFunc () {
return this.render || function () {};
},
classes () { classes () {
return [ return [
this.baseClass, this.baseClass,
@ -82,7 +103,22 @@
]; ];
}, },
contentClasses () { contentClasses () {
return `${this.baseClass}-content`; return [
`${this.baseClass}-content`,
this.render !== undefined ? `${this.baseClass}-content-with-render` : ''
];
},
contentWithIcon () {
return [
this.withIcon ? `${this.prefixCls}-content-with-icon` : '',
this.render && !this.title && this.withIcon ? `${this.prefixCls}-content-with-render-notitle` : ''
];
},
messageClasses () {
return [
`${this.baseClass}-content`,
this.render !== undefined ? `${this.baseClass}-content-with-render` : ''
];
} }
}, },
methods: { methods: {
@ -124,7 +160,8 @@
// check if with desc in Notice component // check if with desc in Notice component
if (this.prefixCls === 'ivu-notice') { if (this.prefixCls === 'ivu-notice') {
this.withDesc = this.$refs.content.querySelectorAll(`.${this.prefixCls}-desc`)[0].innerHTML !== ''; let desc = this.$refs.content.querySelectorAll(`.${this.prefixCls}-desc`)[0];
this.withDesc = this.render ? true : (desc ? desc.innerHTML !== '' : false);
} }
}, },
beforeDestroy () { beforeDestroy () {

View file

@ -8,6 +8,8 @@
:type="notice.type" :type="notice.type"
:content="notice.content" :content="notice.content"
:duration="notice.duration" :duration="notice.duration"
:render="notice.render"
:withIcon="notice.withIcon"
:closable="notice.closable" :closable="notice.closable"
:name="notice.name" :name="notice.name"
:transition-name="notice.transitionName" :transition-name="notice.transitionName"

View file

@ -31,7 +31,7 @@ function getMessageInstance () {
return messageInstance; return messageInstance;
} }
function notice (content = '', duration = defaults.duration, type, onClose = function () {}, closable = false) { function notice (content = '', duration = defaults.duration, type, onClose = function () {}, closable = false, render = function () {}) {
const iconType = iconTypes[type]; const iconType = iconTypes[type];
// if loading // if loading
@ -50,6 +50,7 @@ function notice (content = '', duration = defaults.duration, type, onClose = fun
<span>${content}</span> <span>${content}</span>
</div> </div>
`, `,
render: render,
onClose: onClose, onClose: onClose,
closable: closable, closable: closable,
type: 'message' type: 'message'
@ -89,7 +90,7 @@ export default {
content: options content: options
}; };
} }
return notice(options.content, options.duration, type, options.onClose, options.closable); return notice(options.content, options.duration, type, options.onClose, options.closable, options.render);
}, },
config (options) { config (options) {
if (options.top || options.top === 0) { if (options.top || options.top === 0) {

View file

@ -33,6 +33,7 @@ function notice (type, options) {
const desc = options.desc || ''; const desc = options.desc || '';
const noticeKey = options.name || `${prefixKey}${name}`; const noticeKey = options.name || `${prefixKey}${name}`;
const onClose = options.onClose || function () {}; const onClose = options.onClose || function () {};
const render = options.render;
// todo const btn = options.btn || null; // todo const btn = options.btn || null;
const duration = (options.duration === 0) ? 0 : options.duration || defaultDuration; const duration = (options.duration === 0) ? 0 : options.duration || defaultDuration;
@ -42,9 +43,12 @@ function notice (type, options) {
let content; let content;
const with_desc = desc === '' ? '' : ` ${prefixCls}-with-desc`; let withIcon;
const with_desc = (options.render && !title) ? '' : desc === '' ? '' : ` ${prefixCls}-with-desc`;
if (type == 'normal') { if (type == 'normal') {
withIcon = false;
content = ` content = `
<div class="${prefixCls}-custom-content ${prefixCls}-with-normal${with_desc}"> <div class="${prefixCls}-custom-content ${prefixCls}-with-normal${with_desc}">
<div class="${prefixCls}-title">${title}</div> <div class="${prefixCls}-title">${title}</div>
@ -53,6 +57,7 @@ function notice (type, options) {
`; `;
} else { } else {
const iconType = iconTypes[type]; const iconType = iconTypes[type];
withIcon = true;
content = ` content = `
<div class="${prefixCls}-custom-content ${prefixCls}-with-icon ${prefixCls}-with-${type}${with_desc}"> <div class="${prefixCls}-custom-content ${prefixCls}-with-icon ${prefixCls}-with-${type}${with_desc}">
<span class="${prefixCls}-icon ${prefixCls}-icon-${type}"> <span class="${prefixCls}-icon ${prefixCls}-icon-${type}">
@ -63,13 +68,14 @@ function notice (type, options) {
</div> </div>
`; `;
} }
instance.notice({ instance.notice({
name: noticeKey.toString(), name: noticeKey.toString(),
duration: duration, duration: duration,
styles: {}, styles: {},
transitionName: 'move-notice', transitionName: 'move-notice',
content: content, content: content,
withIcon: withIcon,
render: render,
onClose: onClose, onClose: onClose,
closable: true, closable: true,
type: 'notice' type: 'notice'

View file

@ -11,10 +11,17 @@
position: fixed; position: fixed;
z-index: @zindex-notification; z-index: @zindex-notification;
&-content-with-icon{
margin-left: 51px;
}
&-with-desc&-with-icon &-title{
margin-left: 51px;
}
&-notice { &-notice {
margin-bottom: @notice-margin-bottom; margin-bottom: @notice-margin-bottom;
padding: @notice-padding; padding: @notice-padding;
//border: 1px solid @border-color-split; border: 1px solid @border-color-split;
border-radius: @border-radius-small; border-radius: @border-radius-small;
box-shadow: @shadow-base; box-shadow: @shadow-base;
background: #fff; background: #fff;
@ -34,6 +41,12 @@
} }
} }
&-content-with-render{
.ivu-notice-desc{
display: none;
}
}
&-with-desc{ &-with-desc{
.@{notice-prefix-cls}-notice-close{ .@{notice-prefix-cls}-notice-close{
top: 11px; top: 11px;
@ -41,8 +54,13 @@
} }
} }
&-content-with-render-notitle{
margin-left: 26px;
}
&-title { &-title {
font-size: @font-size-base; font-size: @font-size-base;
line-height: @font-size-base + 3; //fixed the bug that the bottom of some letters were hidden just like 'g'
color: @title-color; color: @title-color;
padding-right: 10px; padding-right: 10px;
overflow: hidden; overflow: hidden;
@ -53,9 +71,6 @@
font-weight: bold; font-weight: bold;
margin-bottom: 8px; margin-bottom: 8px;
} }
&-with-desc&-with-icon &-title{
margin-left: 51px;
}
&-desc { &-desc {
font-size: 12px; font-size: 12px;