-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathSpin.tsx
166 lines (155 loc) · 4.89 KB
/
Spin.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type { VNode, ExtractPropTypes, PropType } from 'vue';
import { inject, cloneVNode, isVNode, defineComponent, nextTick } from 'vue';
import debounce from 'lodash-es/debounce';
import PropTypes from '../_util/vue-types';
import { getComponent, getSlot } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { defaultConfigProvider } from '../config-provider';
export type SpinSize = 'small' | 'default' | 'large';
export const spinProps = () => ({
prefixCls: String,
spinning: { type: Boolean, default: undefined },
size: String as PropType<SpinSize>,
wrapperClassName: String,
tip: PropTypes.any,
delay: Number,
indicator: PropTypes.any,
});
export type SpinProps = Partial<ExtractPropTypes<ReturnType<typeof spinProps>>>;
// Render indicator
let defaultIndicator: () => VNode = null;
function shouldDelay(spinning?: boolean, delay?: number): boolean {
return !!spinning && !!delay && !isNaN(Number(delay));
}
export function setDefaultIndicator(Content: any) {
const Indicator = Content.indicator;
defaultIndicator = typeof Indicator === 'function' ? Indicator : () => <Indicator />;
}
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'ASpin',
inheritAttrs: false,
props: initDefaultProps(spinProps(), {
size: 'default',
spinning: true,
wrapperClassName: '',
}),
setup() {
return {
originalUpdateSpinning: null,
configProvider: inject('configProvider', defaultConfigProvider),
};
},
data() {
const { spinning, delay } = this;
const shouldBeDelayed = shouldDelay(spinning, delay);
return {
sSpinning: spinning && !shouldBeDelayed,
};
},
created() {
this.originalUpdateSpinning = this.updateSpinning;
this.debouncifyUpdateSpinning(this.$props);
},
mounted() {
this.updateSpinning();
},
updated() {
nextTick(() => {
this.debouncifyUpdateSpinning();
this.updateSpinning();
});
},
beforeUnmount() {
this.cancelExistingSpin();
},
methods: {
debouncifyUpdateSpinning(props?: any) {
const { delay } = props || this.$props;
if (delay) {
this.cancelExistingSpin();
this.updateSpinning = debounce(this.originalUpdateSpinning, delay);
}
},
updateSpinning() {
const { spinning, sSpinning } = this;
if (sSpinning !== spinning) {
this.sSpinning = spinning;
}
},
cancelExistingSpin() {
const { updateSpinning } = this;
if (updateSpinning && (updateSpinning as any).cancel) {
(updateSpinning as any).cancel();
}
},
renderIndicator(prefixCls: string) {
const dotClassName = `${prefixCls}-dot`;
let indicator = getComponent(this, '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>
);
},
},
render() {
const {
size,
prefixCls: customizePrefixCls,
tip = this.$slots.tip?.(),
wrapperClassName,
} = this.$props;
const { class: cls, style, ...divProps } = this.$attrs;
const { getPrefixCls, direction } = this.configProvider;
const prefixCls = getPrefixCls('spin', customizePrefixCls);
const { sSpinning } = this;
const spinClassName = {
[prefixCls]: true,
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-spinning`]: sSpinning,
[`${prefixCls}-show-text`]: !!tip,
[`${prefixCls}-rtl`]: direction === 'rtl',
[cls as string]: !!cls,
};
const spinElement = (
<div {...divProps} style={style} class={spinClassName}>
{this.renderIndicator(prefixCls)}
{tip ? <div class={`${prefixCls}-text`}>{tip}</div> : null}
</div>
);
const children = getSlot(this);
if (children && children.length) {
const containerClassName = {
[`${prefixCls}-container`]: true,
[`${prefixCls}-blur`]: sSpinning,
};
return (
<div class={[`${prefixCls}-nested-loading`, wrapperClassName]}>
{sSpinning && <div key="loading">{spinElement}</div>}
<div class={containerClassName} key="container">
{children}
</div>
</div>
);
}
return spinElement;
},
});