You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/spin/Spin.jsx

138 lines
3.6 KiB

7 years ago
import PropTypes from '../_util/vue-types'
import BaseMixin from '../_util/BaseMixin'
import isCssAnimationSupported from '../_util/isCssAnimationSupported'
7 years ago
import { filterEmpty, initDefaultProps } from '../_util/props-util'
7 years ago
import getTransitionProps from '../_util/getTransitionProps'
7 years ago
7 years ago
export const SpinProps = () => ({
prefixCls: PropTypes.string,
spinning: PropTypes.bool,
size: PropTypes.oneOf(['small', 'default', 'large']),
wrapperClassName: PropTypes.string,
tip: PropTypes.string,
delay: PropTypes.number,
})
7 years ago
export default {
name: 'Spin',
mixins: [BaseMixin],
7 years ago
props: initDefaultProps(SpinProps(), {
prefixCls: 'ant-spin',
size: 'default',
spinning: true,
wrapperClassName: '',
}),
7 years ago
data () {
const { spinning } = this
return {
stateSpinning: spinning,
debounceTimeout: null,
delayTimeout: null,
notCssAnimationSupported: false,
}
},
methods: {
7 years ago
getChildren () {
if (this.$slots && this.$slots.default) {
return filterEmpty(this.$slots.default)
}
return null
7 years ago
},
},
mounted () {
if (!isCssAnimationSupported()) {
// Show text in IE9
this.setState({
notCssAnimationSupported: true,
})
}
},
beforeDestroy () {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout)
}
if (this.delayTimeout) {
clearTimeout(this.delayTimeout)
}
},
watch: {
spinning () {
const { delay, spinning } = this
7 years ago
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout)
}
if (!spinning) {
7 years ago
this.debounceTimeout = window.setTimeout(() => this.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(() => this.setState({ stateSpinning: spinning }), delay)
} else {
this.setState({ stateSpinning: spinning })
}
}
},
},
render () {
const { size, prefixCls, tip, wrapperClassName, ...restProps } = this.$props
const { notCssAnimationSupported, $slots, stateSpinning } = this
const spinClassName = {
[prefixCls]: true,
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-spinning`]: stateSpinning,
[`${prefixCls}-show-text`]: !!tip || notCssAnimationSupported,
}
const spinIndicator = $slots.indicator ? $slots.indicator : (
<span class={`${prefixCls}-dot`}>
<i />
<i />
<i />
<i />
</span>
)
const spinElement = (
<div {...restProps} class={spinClassName} >
{spinIndicator}
{tip ? <div class={`${prefixCls}-text`}>{tip}</div> : null}
</div>
)
7 years ago
const children = this.getChildren()
if (children) {
7 years ago
let animateClassName = prefixCls + '-nested-loading'
if (wrapperClassName) {
animateClassName += ' ' + wrapperClassName
}
const containerClassName = {
[`${prefixCls}-container`]: true,
[`${prefixCls}-blur`]: stateSpinning,
}
7 years ago
return (
<transition-group
{...getTransitionProps('fade')}
tag='div'
class={animateClassName}
>
7 years ago
{stateSpinning && <div key='loading'>{spinElement}</div>}
<div class={containerClassName} key='container'>
7 years ago
{children}
7 years ago
</div>
7 years ago
</transition-group>
7 years ago
)
}
return spinElement
},
}