ant-design-vue
Version:
An enterprise-class UI design language and Vue-based implementation
195 lines (172 loc) • 6.01 kB
JavaScript
import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import { filterEmpty, initDefaultProps, isValidElement, getComponentFromProp } from '../_util/props-util';
import getTransitionProps from '../_util/getTransitionProps';
import { cloneElement } from '../_util/vnode';
export var SpinSize = PropTypes.oneOf(['small', 'default', 'large']);
export var SpinProps = function SpinProps() {
return {
prefixCls: PropTypes.string,
spinning: PropTypes.bool,
size: SpinSize,
wrapperClassName: PropTypes.string,
tip: PropTypes.string,
delay: PropTypes.number,
indicator: PropTypes.any
};
};
// Render indicator
var defaultIndicator = void 0;
export function setDefaultIndicator(content) {
defaultIndicator = typeof content.indicator === 'function' ? content.indicator : function (h) {
return h(content.indicator);
};
}
export default {
name: 'ASpin',
mixins: [BaseMixin],
props: initDefaultProps(SpinProps(), {
prefixCls: 'ant-spin',
size: 'default',
spinning: true,
wrapperClassName: ''
}),
data: function data() {
var spinning = this.spinning;
return {
stateSpinning: spinning,
debounceTimeout: null,
delayTimeout: null
};
},
methods: {
getChildren: function getChildren() {
if (this.$slots && this.$slots['default']) {
return filterEmpty(this.$slots['default']);
}
return null;
},
renderIndicator: function renderIndicator(h, props) {
// const h = this.$createElement
var prefixCls = props.prefixCls;
var dotClassName = prefixCls + '-dot';
var indicator = getComponentFromProp(this, 'indicator');
if (Array.isArray(indicator)) {
indicator = filterEmpty(indicator);
indicator = indicator.length === 1 ? indicator[0] : indicator;
}
if (isValidElement(indicator)) {
return cloneElement(indicator, { 'class': dotClassName });
}
if (defaultIndicator && isValidElement(defaultIndicator(h))) {
return cloneElement(defaultIndicator(h), { 'class': dotClassName });
}
return h(
'span',
{ 'class': dotClassName + ' ' + prefixCls + '-dot-spin' },
[h('i'), h('i'), h('i'), h('i')]
);
}
},
mounted: function mounted() {
var _this = this;
this.$nextTick(function () {
var spinning = _this.spinning,
delay = _this.delay;
if (spinning && delay && !isNaN(Number(delay))) {
_this.setState({ stateSpinning: false });
_this.delayTimeout = window.setTimeout(function () {
return _this.setState({ stateSpinning: spinning });
}, delay);
}
});
},
beforeDestroy: function beforeDestroy() {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
},
watch: {
spinning: function spinning() {
var _this2 = this;
var delay = this.delay,
spinning = this.spinning;
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
if (!spinning) {
this.debounceTimeout = window.setTimeout(function () {
return _this2.setState({ stateSpinning: spinning });
}, 200);
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
} else {
if (spinning && delay && !isNaN(Number(delay))) {
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
this.delayTimeout = window.setTimeout(function () {
return _this2.setState({ stateSpinning: spinning });
}, delay);
} else {
this.setState({ stateSpinning: spinning });
}
}
}
},
render: function render(h) {
var _spinClassName;
var _$props = this.$props,
size = _$props.size,
prefixCls = _$props.prefixCls,
tip = _$props.tip,
wrapperClassName = _$props.wrapperClassName,
restProps = _objectWithoutProperties(_$props, ['size', 'prefixCls', 'tip', 'wrapperClassName']);
var stateSpinning = this.stateSpinning;
var spinClassName = (_spinClassName = {}, _defineProperty(_spinClassName, prefixCls, true), _defineProperty(_spinClassName, prefixCls + '-sm', size === 'small'), _defineProperty(_spinClassName, prefixCls + '-lg', size === 'large'), _defineProperty(_spinClassName, prefixCls + '-spinning', stateSpinning), _defineProperty(_spinClassName, prefixCls + '-show-text', !!tip), _spinClassName);
var spinElement = h(
'div',
_mergeJSXProps([restProps, { 'class': spinClassName }]),
[this.renderIndicator(h, this.$props), tip ? h(
'div',
{ 'class': prefixCls + '-text' },
[tip]
) : null]
);
var children = this.getChildren();
if (children) {
var _containerClassName;
var animateClassName = prefixCls + '-nested-loading';
if (wrapperClassName) {
animateClassName += ' ' + wrapperClassName;
}
var containerClassName = (_containerClassName = {}, _defineProperty(_containerClassName, prefixCls + '-container', true), _defineProperty(_containerClassName, prefixCls + '-blur', stateSpinning), _containerClassName);
return h(
'transition-group',
_mergeJSXProps([getTransitionProps('fade', { appear: false }), {
attrs: {
tag: 'div'
},
'class': animateClassName
}]),
[stateSpinning && h(
'div',
{ key: 'loading' },
[spinElement]
), h(
'div',
{ 'class': containerClassName, key: 'container' },
[children]
)]
);
}
return spinElement;
}
};