Skip to content

Commit 70a2130

Browse files
authored
chore(loading): 修复eslint问题 (DevCloudFE#392)
1 parent 3a16383 commit 70a2130

File tree

4 files changed

+55
-43
lines changed

4 files changed

+55
-43
lines changed

packages/devui-vue/devui/loading/src/directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const handleProps = (el: TargetHTMLElement, vprops: LoadingProps) => {
107107
el.style.position = props.positionType!
108108
el.options = props
109109
el.instance = loadingInstance
110-
el.mask = loadingInstance.proxy.$el
110+
el.mask = loadingInstance?.proxy?.$el
111111
}
112112

113113
const loadingDirective = {
Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,63 @@
1-
/* eslint-disable */
2-
import { defineComponent } from 'vue'
3-
import { createComponent } from '../../shared/scripts/component'
4-
import Loading from './loading'
1+
import type { CSSProperties, VNode, ComponentInternalInstance } from 'vue';
2+
import { defineComponent } from 'vue';
3+
import { createComponent } from '../../shared/scripts/component';
4+
import Loading from './loading';
55

6-
import { LoadingProps } from './types'
6+
import { LoadingProps } from './types';
77

8-
const loadingConstructor = defineComponent(Loading)
8+
const loadingConstructor = defineComponent(Loading);
99

1010
interface TargetElement extends Element {
11-
style ?: any
11+
style?: CSSProperties;
1212
}
1313

14-
const cacheTarget = new WeakMap()
14+
type IMargeVNodeComponent = VNode['component'] & {
15+
loadingInstance?: ComponentInternalInstance['proxy'];
16+
} | null;
17+
18+
const cacheTarget = new WeakMap();
1519

1620
const loading = {
17-
open(options: LoadingProps = {}) {
21+
open(options: LoadingProps = {}): IMargeVNodeComponent {
1822

19-
const parent: TargetElement = options.target || document.body
23+
const parent: TargetElement = options.target || document.body;
2024

2125
if (cacheTarget.has(parent)) {
22-
return cacheTarget.get(parent)
26+
return cacheTarget.get(parent);
27+
}
28+
if (parent.style) {
29+
parent.style.position = options.positionType;
2330
}
2431

25-
parent.style.position = options.positionType
26-
27-
const isFull = document.body === parent
32+
const isFull = document.body === parent;
2833

29-
options = {...new LoadingProps(), ...options}
34+
options = { ...new LoadingProps(), ...options };
3035

31-
const instance = createComponent(loadingConstructor, {
36+
const instance: IMargeVNodeComponent = createComponent(loadingConstructor, {
3237
...options,
3338
isFull
34-
}, options.loadingTemplateRef ? () => options.loadingTemplateRef : null)
35-
36-
cacheTarget.set(parent, instance)
37-
38-
instance.proxy.open()
39-
parent.appendChild(instance.proxy.$el)
40-
41-
const close = instance.proxy.close
42-
instance.loadingInstance = instance.proxy
43-
instance.loadingInstance.close = (...args: any[]) => {
44-
cacheTarget.delete(parent)
45-
// 1. 箭头函数内部并没有内置arguments对象 @mrundef-210810
46-
// 2. 如果没有上下文要求`apply(null)`,不必使用apply/call
47-
// close.apply(null, arguments)
48-
close(...args)
39+
}, options.loadingTemplateRef ? () => options.loadingTemplateRef : null);
40+
41+
cacheTarget.set(parent, instance);
42+
43+
instance?.proxy?.open();
44+
parent.appendChild(instance?.proxy?.$el);
45+
46+
const close = instance?.proxy?.close;
47+
if (instance) {
48+
instance.loadingInstance = instance?.proxy;
49+
if (instance.loadingInstance) {
50+
instance.loadingInstance.close = (...args: unknown[]) => {
51+
cacheTarget.delete(parent);
52+
// 1. 箭头函数内部并没有内置arguments对象 @mrundef-210810
53+
// 2. 如果没有上下文要求`apply(null)`,不必使用apply/call
54+
// close.apply(null, arguments)
55+
close?.(...args);
56+
};
57+
}
4958
}
50-
51-
return instance
59+
return instance;
5260
}
53-
}
61+
};
5462

55-
export default loading
63+
export default loading;

packages/devui-vue/devui/loading/src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { CSSProperties } from 'vue';
12
import { ExtractPropTypes, PropType, VNode } from 'vue';
23

3-
type PositionType = 'static' | 'relative' | 'absolute' | 'fixed' |'sticky';
4+
type PositionType = CSSProperties['position'];
45

56
export interface LoadingType {
67
value: Promise<unknown> | Array<Promise<unknown>> | undefined;
@@ -30,7 +31,7 @@ export const componentProps = {
3031
export class LoadingProps {
3132
target?: Element | null;
3233
message?: string;
33-
loadingTemplateRef?: unknown;
34+
loadingTemplateRef?: VNode['component'];
3435
backdrop?: boolean = true;
3536
positionType?: PositionType = 'relative';
3637
view?: View = new View();
@@ -41,6 +42,6 @@ export type ComponentProps = ExtractPropTypes<typeof componentProps>;
4142

4243
export interface TargetHTMLElement extends HTMLElement {
4344
mask?: HTMLElement;
44-
instance?: VNode | unknown;
45+
instance?: VNode['component'];
4546
options?: LoadingProps;
4647
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import type { VNode } from 'vue';
12
import { h, render } from 'vue';
23

34
const COMPONENT_CONTAINER_SYMBOL = Symbol('dev_component_container');
4-
5+
type IMargeVNode = {
6+
[COMPONENT_CONTAINER_SYMBOL]?: HTMLDivElement;
7+
} & VNode;
58
/**
69
* 创建组件实例对象
710
* 返回的实例和调用 getCurrentComponent() 返回的一致
811
* @param {*} Component
912
*/
10-
export function createComponent(Component: any, props: any, children: any = null) {
11-
const vnode: any = h(Component, { ...props }, children);
13+
export function createComponent(component: any, props: any, children: any = null): VNode['component'] {
14+
const vnode: IMargeVNode = h(component, { ...props }, children);
1215
const container = document.createElement('div');
1316
vnode[COMPONENT_CONTAINER_SYMBOL] = container;
1417
render(vnode, container);
@@ -19,6 +22,6 @@ export function createComponent(Component: any, props: any, children: any = null
1922
* 销毁组件实例对象
2023
* @param {*} ComponnetInstance 通过createComponent方法得到的组件实例对象
2124
*/
22-
export function unmountComponent(ComponnetInstance: any) {
25+
export function unmountComponent(ComponnetInstance: any): void {
2326
render(null, ComponnetInstance?.vnode[COMPONENT_CONTAINER_SYMBOL]);
2427
}

0 commit comments

Comments
 (0)