2022-03-26 14:52:54 +00:00
|
|
|
import type { VNode, ExtractPropTypes, PropType } from 'vue';
|
2023-02-01 09:17:56 +00:00
|
|
|
import { cloneVNode, isVNode, defineComponent, shallowRef, watch } from 'vue';
|
2020-09-02 14:44:16 +00:00
|
|
|
import debounce from 'lodash-es/debounce';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2023-02-01 09:17:56 +00:00
|
|
|
import { filterEmpty, getPropsSlot } from '../_util/props-util';
|
2020-10-13 14:59:24 +00:00
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
2023-02-01 09:17:56 +00:00
|
|
|
import useStyle from './style';
|
|
|
|
import useConfigInject from '../config-provider/hooks/useConfigInject';
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type SpinSize = 'small' | 'default' | 'large';
|
2021-09-25 08:51:32 +00:00
|
|
|
export const spinProps = () => ({
|
2022-03-26 14:52:54 +00:00
|
|
|
prefixCls: String,
|
|
|
|
spinning: { type: Boolean, default: undefined },
|
|
|
|
size: String as PropType<SpinSize>,
|
|
|
|
wrapperClassName: String,
|
2022-03-12 01:56:32 +00:00
|
|
|
tip: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
delay: Number,
|
2018-06-16 14:57:11 +00:00
|
|
|
indicator: PropTypes.any,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-29 14:08:04 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
export type SpinProps = Partial<ExtractPropTypes<ReturnType<typeof spinProps>>>;
|
2021-06-07 09:35:03 +00:00
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
// Render indicator
|
2020-10-13 14:59:24 +00:00
|
|
|
let defaultIndicator: () => VNode = null;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-10-13 14:59:24 +00:00
|
|
|
function shouldDelay(spinning?: boolean, delay?: number): boolean {
|
2019-01-12 03:33:27 +00:00
|
|
|
return !!spinning && !!delay && !isNaN(Number(delay));
|
2018-11-30 03:56:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 14:59:24 +00:00
|
|
|
export function setDefaultIndicator(Content: any) {
|
2020-06-04 10:35:33 +00:00
|
|
|
const Indicator = Content.indicator;
|
2020-10-13 14:59:24 +00:00
|
|
|
defaultIndicator = typeof Indicator === 'function' ? Indicator : () => <Indicator />;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 14:59:24 +00:00
|
|
|
export default defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
compatConfig: { MODE: 3 },
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ASpin',
|
2020-06-04 10:35:33 +00:00
|
|
|
inheritAttrs: false,
|
2021-09-25 08:51:32 +00:00
|
|
|
props: initDefaultProps(spinProps(), {
|
2018-03-29 14:08:04 +00:00
|
|
|
size: 'default',
|
|
|
|
spinning: true,
|
|
|
|
wrapperClassName: '',
|
|
|
|
}),
|
2023-02-01 09:17:56 +00:00
|
|
|
setup(props, { attrs, slots }) {
|
|
|
|
const { prefixCls, size, direction } = useConfigInject('spin', props);
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
const sSpinning = shallowRef(props.spinning && shouldDelay(props.spinning, props.delay));
|
|
|
|
let updateSpinning: any;
|
|
|
|
function originalUpdateSpinning() {
|
|
|
|
if (sSpinning.value !== props.spinning) {
|
|
|
|
sSpinning.value = props.spinning;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
2023-02-01 09:17:56 +00:00
|
|
|
}
|
|
|
|
function cancelExistingSpin() {
|
|
|
|
if (updateSpinning && updateSpinning.cancel) {
|
|
|
|
updateSpinning.cancel();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2023-02-01 09:17:56 +00:00
|
|
|
}
|
|
|
|
function debouncifyUpdateSpinning() {
|
|
|
|
const { delay } = props;
|
|
|
|
if (delay) {
|
|
|
|
cancelExistingSpin();
|
|
|
|
updateSpinning = debounce(originalUpdateSpinning, delay);
|
|
|
|
} else {
|
|
|
|
updateSpinning = originalUpdateSpinning;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2023-02-01 09:17:56 +00:00
|
|
|
}
|
|
|
|
watch(
|
|
|
|
() => [props.spinning, props.delay],
|
|
|
|
() => {
|
|
|
|
debouncifyUpdateSpinning();
|
|
|
|
updateSpinning?.();
|
|
|
|
},
|
|
|
|
{
|
|
|
|
immediate: true,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2023-02-01 09:17:56 +00:00
|
|
|
return () => {
|
|
|
|
const { class: cls, ...divProps } = attrs;
|
|
|
|
const { tip = slots.tip?.() } = props;
|
|
|
|
const children = slots.default?.();
|
|
|
|
const spinClassName = {
|
|
|
|
[hashId.value]: true,
|
|
|
|
[prefixCls.value]: true,
|
|
|
|
[`${prefixCls.value}-sm`]: size.value === 'small',
|
|
|
|
[`${prefixCls.value}-lg`]: size.value === 'large',
|
|
|
|
[`${prefixCls.value}-spinning`]: sSpinning.value,
|
|
|
|
[`${prefixCls.value}-show-text`]: !!tip,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
[cls as string]: !!cls,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-02-09 09:09:50 +00:00
|
|
|
|
2023-02-01 09:17:56 +00:00
|
|
|
function renderIndicator(prefixCls: string) {
|
|
|
|
const dotClassName = `${prefixCls}-dot`;
|
|
|
|
let indicator = getPropsSlot(slots, props, 'indicator');
|
|
|
|
// should not be render default indicator when indicator value is null
|
|
|
|
if (indicator === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (Array.isArray(indicator)) {
|
|
|
|
indicator = indicator.length === 1 ? indicator[0] : indicator;
|
|
|
|
}
|
|
|
|
if (isVNode(indicator)) {
|
|
|
|
return cloneVNode(indicator, { class: dotClassName });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultIndicator && isVNode(defaultIndicator())) {
|
|
|
|
return cloneVNode(defaultIndicator(), { class: dotClassName });
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span class={`${dotClassName} ${prefixCls}-dot-spin`}>
|
|
|
|
<i class={`${prefixCls}-dot-item`} />
|
|
|
|
<i class={`${prefixCls}-dot-item`} />
|
|
|
|
<i class={`${prefixCls}-dot-item`} />
|
|
|
|
<i class={`${prefixCls}-dot-item`} />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const spinElement = (
|
|
|
|
<div {...divProps} class={spinClassName} aria-live="polite" aria-busy={sSpinning.value}>
|
|
|
|
{renderIndicator(prefixCls.value)}
|
|
|
|
{tip ? <div class={`${prefixCls.value}-text`}>{tip}</div> : null}
|
2019-01-05 03:24:25 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2023-02-01 09:17:56 +00:00
|
|
|
if (children && filterEmpty(children).length) {
|
|
|
|
const containerClassName = {
|
|
|
|
[`${prefixCls.value}-container`]: true,
|
|
|
|
[`${prefixCls.value}-blur`]: sSpinning.value,
|
|
|
|
};
|
|
|
|
return wrapSSR(
|
|
|
|
<div class={[`${prefixCls.value}-nested-loading`, props.wrapperClassName, hashId.value]}>
|
|
|
|
{sSpinning.value && <div key="loading">{spinElement}</div>}
|
|
|
|
<div class={containerClassName} key="container">
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return wrapSSR(spinElement);
|
|
|
|
};
|
2018-02-09 09:09:50 +00:00
|
|
|
},
|
2020-10-13 14:59:24 +00:00
|
|
|
});
|