ant-design-vue/components/spin/Spin.jsx

137 lines
3.6 KiB
React
Raw Normal View History

2018-03-19 02:16:27 +00:00
2018-02-09 09:09:50 +00:00
import PropTypes from '../_util/vue-types'
import BaseMixin from '../_util/BaseMixin'
import isCssAnimationSupported from '../_util/isCssAnimationSupported'
2018-03-29 14:08:04 +00:00
import { filterEmpty, initDefaultProps } from '../_util/props-util'
2018-02-11 03:15:12 +00:00
import getTransitionProps from '../_util/getTransitionProps'
2018-02-09 09:09:50 +00:00
2018-03-29 14:08:04 +00:00
export const SpinProps = () => ({
prefixCls: PropTypes.string,
spinning: PropTypes.bool,
size: PropTypes.oneOf(['small', 'default', 'large']),
wrapperClassName: PropTypes.string,
tip: PropTypes.string,
delay: PropTypes.number,
})
2018-02-09 09:09:50 +00:00
export default {
2018-04-08 13:17:20 +00:00
name: 'ASpin',
2018-02-09 09:09:50 +00:00
mixins: [BaseMixin],
2018-03-29 14:08:04 +00:00
props: initDefaultProps(SpinProps(), {
prefixCls: 'ant-spin',
size: 'default',
spinning: true,
wrapperClassName: '',
}),
2018-02-09 09:09:50 +00:00
data () {
const { spinning } = this
return {
stateSpinning: spinning,
debounceTimeout: null,
delayTimeout: null,
notCssAnimationSupported: false,
}
},
methods: {
2018-02-11 03:15:12 +00:00
getChildren () {
if (this.$slots && this.$slots.default) {
return filterEmpty(this.$slots.default)
}
return null
2018-02-09 09:09:50 +00:00
},
},
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: {
2018-02-27 04:14:29 +00:00
spinning () {
const { delay, spinning } = this
2018-02-09 09:09:50 +00:00
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout)
}
2018-02-27 04:14:29 +00:00
if (!spinning) {
2018-02-09 09:09:50 +00:00
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>
)
2018-02-11 03:15:12 +00:00
const children = this.getChildren()
if (children) {
2018-02-09 09:09:50 +00:00
let animateClassName = prefixCls + '-nested-loading'
if (wrapperClassName) {
animateClassName += ' ' + wrapperClassName
}
const containerClassName = {
[`${prefixCls}-container`]: true,
[`${prefixCls}-blur`]: stateSpinning,
}
2018-02-11 03:15:12 +00:00
return (
<transition-group
{...getTransitionProps('fade')}
tag='div'
class={animateClassName}
>
2018-02-09 09:09:50 +00:00
{stateSpinning && <div key='loading'>{spinElement}</div>}
<div class={containerClassName} key='container'>
2018-02-11 03:15:12 +00:00
{children}
2018-02-09 09:09:50 +00:00
</div>
2018-02-11 03:15:12 +00:00
</transition-group>
2018-02-09 09:09:50 +00:00
)
}
return spinElement
},
}
2018-03-19 02:16:27 +00:00