@opensig/opendesign
Version:
91 lines (90 loc) • 3.07 kB
JavaScript
import { defineComponent, inject, useAttrs, computed, useSlots, resolveComponent, createBlock, openBlock, h, renderSlot } from "vue";
import "../config-provider/index.mjs";
import { defaultSize } from "../_utils/global.mjs";
import { IconLoading, IconLinkArrow } from "../_utils/icons.mjs";
import { linkProps } from "./types.mjs";
import { configProviderInjectKey } from "../config-provider/provide.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "OLink",
props: linkProps,
emits: ["click"],
setup(__props, { emit: __emit }) {
const props = __props;
const configProvider = inject(configProviderInjectKey, {});
const $attr = useAttrs();
const emits = __emit;
const onClick = (e) => {
var _a;
if (props.disabled || props.loading) {
e.preventDefault();
return;
}
emits("click", e);
if (props.global) {
(_a = configProvider.link) == null ? void 0 : _a.click(e, props, $attr);
}
};
const linkClass = computed(() => [
{
"o-link-disabled": props.disabled,
"o-link-hover-bg": props.hoverBg,
"o-link-hover-underline": props.hoverUnderline
},
`o-link-${props.color}`,
`o-link-${props.size || defaultSize}`
]);
const $slots = useSlots();
const RouterLink = resolveComponent("RouterLink");
const prefix = () => {
const children = [];
if (props.loading) {
children.push(h(IconLoading.value, { class: "o-rotating" }));
} else if ($slots.icon || props.icon) {
children.push(renderSlot($slots, "icon", {}, () => props.icon ? [h(props.icon)] : []));
}
if (children.length) {
return h("span", { class: "o-link-prefix" }, children);
}
return null;
};
const main = () => {
const children = [];
const slotVnode = renderSlot($slots, "default");
if (props.hoverUnderline) {
children.push(h("span", { class: "o-link-label" }, slotVnode));
} else {
children.push(slotVnode);
}
return h("span", { class: "o-link-main" }, children);
};
const suffix = () => {
if ($slots.suffix || props.suffix) {
return h(
"span",
{ class: "o-link-suffix" },
renderSlot($slots, "suffix", {}, () => props.suffix ? [h(IconLinkArrow.value, { class: "o-link-icon-arrow" })] : [])
);
}
return null;
};
const Link = () => {
const _props = { ...$attr, class: ["o-link", linkClass.value], onClick };
if (props.to && RouterLink) {
_props.to = props.to;
_props.replace = props.replace;
return h(RouterLink, _props, { default: () => [prefix(), main(), suffix()] });
}
if (props.tag === "a") {
_props.href = props.href;
_props.target = props.target;
}
return h(props.tag, _props, { default: () => [prefix(), main(), suffix()] });
};
return (_ctx, _cache) => {
return openBlock(), createBlock(Link);
};
}
});
export {
_sfc_main as default
};