fix: spin error #6222

pull/6226/head
tangjinzhou 2023-02-01 17:33:59 +08:00
parent 7939eb1718
commit 8099391e97
2 changed files with 13 additions and 24 deletions

View File

@ -1,6 +1,6 @@
import type { VNode, ExtractPropTypes, PropType } from 'vue'; import type { VNode, ExtractPropTypes, PropType } from 'vue';
import { cloneVNode, isVNode, defineComponent, shallowRef, watch } from 'vue'; import { onBeforeUnmount, cloneVNode, isVNode, defineComponent, shallowRef, watch } from 'vue';
import debounce from 'lodash-es/debounce'; import { debounce } from 'throttle-debounce';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { filterEmpty, getPropsSlot } from '../_util/props-util'; import { filterEmpty, getPropsSlot } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps'; import initDefaultProps from '../_util/props-util/initDefaultProps';
@ -44,37 +44,25 @@ export default defineComponent({
setup(props, { attrs, slots }) { setup(props, { attrs, slots }) {
const { prefixCls, size, direction } = useConfigInject('spin', props); const { prefixCls, size, direction } = useConfigInject('spin', props);
const [wrapSSR, hashId] = useStyle(prefixCls); const [wrapSSR, hashId] = useStyle(prefixCls);
const sSpinning = shallowRef(props.spinning && shouldDelay(props.spinning, props.delay)); const sSpinning = shallowRef(props.spinning && !shouldDelay(props.spinning, props.delay));
let updateSpinning: any; let updateSpinning: any;
function originalUpdateSpinning() {
if (sSpinning.value !== props.spinning) {
sSpinning.value = props.spinning;
}
}
function cancelExistingSpin() {
if (updateSpinning && updateSpinning.cancel) {
updateSpinning.cancel();
}
}
function debouncifyUpdateSpinning() {
const { delay } = props;
if (delay) {
cancelExistingSpin();
updateSpinning = debounce(originalUpdateSpinning, delay);
} else {
updateSpinning = originalUpdateSpinning;
}
}
watch( watch(
() => [props.spinning, props.delay], [() => props.spinning, () => props.delay],
() => { () => {
debouncifyUpdateSpinning(); updateSpinning?.cancel();
updateSpinning = debounce(props.delay, () => {
sSpinning.value = props.spinning;
});
updateSpinning?.(); updateSpinning?.();
}, },
{ {
immediate: true, immediate: true,
flush: 'post',
}, },
); );
onBeforeUnmount(() => {
updateSpinning?.cancel();
});
return () => { return () => {
const { class: cls, ...divProps } = attrs; const { class: cls, ...divProps } = attrs;
const { tip = slots.tip?.() } = props; const { tip = slots.tip?.() } = props;

View File

@ -292,6 +292,7 @@
"scroll-into-view-if-needed": "^2.2.25", "scroll-into-view-if-needed": "^2.2.25",
"shallow-equal": "^1.0.0", "shallow-equal": "^1.0.0",
"stylis": "^4.1.3", "stylis": "^4.1.3",
"throttle-debounce": "^5.0.0",
"vue-types": "^3.0.0", "vue-types": "^3.0.0",
"warning": "^4.0.0" "warning": "^4.0.0"
}, },