UNPKG

yanzhen-design-vue

Version:

116 lines (115 loc) 3.18 kB
import { ref, computed, useSlots, isVNode, h, onUnmounted, withCtx, mergeProps } from "vue"; import { useProvideLoading, createWatcher, unref, useProxyProps } from "@yanzhen-libs/utils-vue"; import { Spin } from "ant-design-vue"; import { isBoolean, pick, isFunction } from "lodash-es"; import { spinOptions, AntSpin } from "./spin.mjs"; import { getDefaultIndicator } from "./setDefaultIndicator.mjs"; const { ns } = spinOptions; function useSpin(props, emit) { const _ref = ref(); const spinId = computed(() => props.name ?? Symbol("spin")); const [{ loading, refresh }, Loading] = useProvideLoading({ id: spinId, timeout: computed(() => props.timeout) }); const slots = useSlots(); const watcher = createWatcher(); const spinning = computed(() => Boolean(props.spinning)); const tag = computed(() => props.tag); const isNative = computed(() => { if (isBoolean(unref(tag))) { return false; } return unref(tag) === "spin"; }); watcher.set("loading", { source: () => unref(spinning), handler(value) { loading.value = value; }, immediate: true, debounce: 0 }); const config = computed(() => pick(props, Object.keys(AntSpin.props))); const proxyProps = useProxyProps(config, AntSpin.emits, { emit, exclude: ["spinning"], filter: true }); function Indicator(_props) { if (isFunction(slots.indicator)) { return slots.indicator(_props); } if (isVNode(props.indicator)) { return h(props.indicator, _props); } const { indicator } = getDefaultIndicator(); if (isFunction(indicator)) { return indicator(_props); } return void 0; } function Tip(_props) { if (isFunction(slots.tip)) { return slots.tip(_props); } if (isVNode(props.tip)) { return h(props.tip, _props); } return props.tip; } onUnmounted(() => { watcher.stop(); }); const exposeCtx = new Proxy( { refresh }, { get(target, p, receiver) { return Reflect.get(target, p, receiver); } } ); return [ { _ref, Indicator, Tip, exposeCtx }, (props2) => { const children = { loading: (props3) => { const renderDefault = unref(isNative) && (slots == null ? void 0 : slots.default) ? withCtx(slots == null ? void 0 : slots.default) : h("div", { style: { height: "100%" } }); return h( Spin, mergeProps( { wrapperClassName: ns.b("wrapper"), class: [ns.b()] }, unref(proxyProps), props3 ?? {}, { ref: _ref, spinning: unref(isNative) ? unref(spinning) : true } ), { indicator: withCtx(Indicator), tip: withCtx(Tip), default: renderDefault } ); } }; if (unref(isNative)) { return children == null ? void 0 : children.loading(unref(proxyProps)); } return Loading(props2, children); } ]; } export { useSpin };