yanzhen-design-vue
Version:
77 lines (76 loc) • 1.94 kB
JavaScript
import { useSlots, ref, useAttrs, toRefs, computed, mergeProps, unref } from "vue";
import { createReusableTemplate } from "@vueuse/core";
import { isEmptyValue } from "@yanzhen-libs/utils";
import { useProxyProps } from "@yanzhen-libs/utils-vue";
import { pick, isString, merge } from "lodash-es";
import { AntAvatar } from "./avatar.mjs";
const useAvatar = (props, emit) => {
const $slots = useSlots();
const _ref = ref();
const attrs = useAttrs();
const { src, errSrc, text, max } = toRefs(props);
const _loadError = ref(false);
const config = computed(() => pick(props, ...Object.keys(AntAvatar.props)));
const proxyProps = useProxyProps(config, AntAvatar.emits, {
emit,
exclude: ["src", "errSrc"],
filter: true
});
const loadErrors = ref({});
const _props = computed(() => mergeProps(unref(attrs), unref(proxyProps)));
const [DefineAvatarSlotTemplate, ReusableAvatarSlotTemplate] = createReusableTemplate();
const _text = computed(() => {
if (isString(unref(text)) && !isEmptyValue(unref(text))) {
return String(unref(text)).slice(0, unref(max));
}
return void 0;
});
const _slots = computed(() => {
return merge(
{
default: void 0
},
$slots
);
});
function handleImageError(key) {
if (isString(key)) unref(loadErrors)[key] = false;
emit("error");
return true;
}
function isLoadSuccessfully(key) {
if (isString(key) && !isEmptyValue(key)) {
const flag = unref(loadErrors)[key];
return flag !== false;
}
return false;
}
const ctx = {
attrs,
config,
_ref,
_text,
text,
_props,
proxyProps,
_loadError,
$slots,
_slots,
loadErrors,
src,
errSrc
};
const templates = {
DefineAvatarSlotTemplate,
ReusableAvatarSlotTemplate
};
return {
ctx,
templates,
handleImageError,
isLoadSuccessfully
};
};
export {
useAvatar
};