ant-design-vue
Version:
An enterprise-class UI design language and Vue-based implementation
170 lines (161 loc) • 4.61 kB
JavaScript
import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import PropTypes from '../_util/vue-types';
import Icon from '../icon';
import getTransitionProps from '../_util/getTransitionProps';
import omit from 'omit.js';
import Wave from '../_util/wave';
import { hasProp, getOptionProps } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
export default {
name: 'ATag',
mixins: [BaseMixin],
props: {
prefixCls: PropTypes.string.def('ant-tag'),
color: PropTypes.string,
closable: PropTypes.bool,
visible: PropTypes.bool,
afterClose: PropTypes.func
},
model: {
prop: 'visible',
event: 'close.visible'
},
data: function data() {
var props = getOptionProps(this);
var state = {};
if ('visible' in props) {
state = {
_visible: props.visible,
_closed: !props.visible
};
}
state = _extends({
_closing: false,
_closed: false,
_visible: true
}, state);
this.pre_visible = state._visible;
return state;
},
watch: {
visible: function visible(val) {
this.setState({
_visible: val
});
}
},
updated: function updated() {
var _this = this;
this.$nextTick(function () {
var preVisible = _this.pre_visible;
_this.pre_visible = _this.$data._visible;
if (preVisible && !_this.$data._visible) {
_this.close();
} else if (!preVisible && _this.$data._visible) {
_this.show();
}
});
},
methods: {
handleIconClick: function handleIconClick(e) {
this.$emit('close', e);
this.$emit('close.visible', false);
if (e.defaultPrevented || hasProp(this, 'visible')) {
return;
}
this.setState({ _visible: false });
this.$forceUpdate();
},
close: function close() {
if (this.$data._closing || this.$data._closed) {
return;
}
var dom = this.$el;
dom.style.width = dom.getBoundingClientRect().width + 'px';
// It's Magic Code, don't know why
dom.style.width = dom.getBoundingClientRect().width + 'px';
this.setState({
_closing: true
});
},
show: function show() {
this.setState({
_closed: false
});
},
animationEnd: function animationEnd(_, existed) {
if (!existed && !this.$data._closed) {
this.setState({
_closed: true,
_closing: false
});
var afterClose = this.afterClose;
if (afterClose) {
afterClose();
}
} else {
this.setState({
_closed: false
});
}
},
isPresetColor: function isPresetColor(color) {
if (!color) {
return false;
}
return (/^(pink|red|yellow|orange|cyan|green|blue|purple|geekblue|magenta|volcano|gold|lime)(-inverse)?$/.test(color)
);
}
},
render: function render() {
var _cls,
_this2 = this;
var h = arguments[0];
var _$props = this.$props,
prefixCls = _$props.prefixCls,
closable = _$props.closable,
color = _$props.color;
var closeIcon = closable ? h(Icon, {
attrs: { type: 'close' },
on: {
'click': this.handleIconClick
}
}) : '';
var isPresetColor = this.isPresetColor(color);
var cls = (_cls = {}, _defineProperty(_cls, '' + prefixCls, true), _defineProperty(_cls, prefixCls + '-' + color, isPresetColor), _defineProperty(_cls, prefixCls + '-has-color', color && !isPresetColor), _defineProperty(_cls, prefixCls + '-close', this.$data._closing), _cls);
var tagStyle = {
backgroundColor: color && !isPresetColor ? color : null
};
var tag = h(
'div',
_mergeJSXProps([{
directives: [{
name: 'show',
value: !this.$data._closing
}],
attrs: {
'data-show': !this.$data._closing
}
}, { on: omit(this.$listeners, ['close']) }, {
'class': cls,
style: tagStyle
}]),
[this.$slots['default'], closeIcon]
);
var transitionProps = getTransitionProps(prefixCls + '-zoom', {
afterLeave: function afterLeave() {
return _this2.animationEnd(undefined, false);
},
afterEnter: function afterEnter() {
return _this2.animationEnd(undefined, true);
}
});
return h(Wave, [this.$data._closed ? h('span') : h(
'transition',
transitionProps,
[tag]
)]);
}
};