huangyinfeng
4 天以前 db42d08c39ae6129e2b95cd24c0d57c6769282e5
提交 | 用户 | age
bcd98e 1 import type { Ref } from 'vue';
4974de 2 import { unref } from 'vue';
bcd98e 3 import { tryOnUnmounted } from '@vueuse/core';
5db3ce 4 import { createLoading } from './createLoading';
fa828f 5 import type { LoadingProps } from './typing';
5db3ce 6
V 7 export interface UseLoadingOptions {
e15b4f 8   target?: any;
5db3ce 9   props?: Partial<LoadingProps>;
V 10 }
11
2ee01f 12 interface Fn {
V 13   (): void;
14 }
15
26d947 16 export function useLoading(props: Partial<LoadingProps>): [Fn, Fn, (string) => void];
17 export function useLoading(opt: Partial<UseLoadingOptions>): [Fn, Fn, (string) => void];
5db3ce 18
26d947 19 export function useLoading(
56a966 20   opt: Partial<LoadingProps> | Partial<UseLoadingOptions>,
26d947 21 ): [Fn, Fn, (string) => void] {
5db3ce 22   let props: Partial<LoadingProps>;
V 23   let target: HTMLElement | Ref<ElRef> = document.body;
24
25   if (Reflect.has(opt, 'target') || Reflect.has(opt, 'props')) {
26     const options = opt as Partial<UseLoadingOptions>;
27     props = options.props || {};
28     target = options.target || document.body;
29   } else {
30     props = opt as Partial<LoadingProps>;
31   }
32
6b9962 33   const instance = createLoading(props, undefined, true);
5db3ce 34
V 35   const open = (): void => {
9035fd 36     const t = unref(target as Ref<ElRef>);
5db3ce 37     if (!t) return;
V 38     instance.open(t);
39   };
40
41   const close = (): void => {
42     instance.close();
43   };
44
26d947 45   const setTip = (tip: string) => {
46     instance.setTip(tip);
47   };
48
bcd98e 49   tryOnUnmounted(() => {
4974de 50     instance.destroy();
bcd98e 51   });
W 52
26d947 53   return [open, close, setTip];
5db3ce 54 }