Skip to content

Commit

Permalink
feat(Image): add imageProps attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
anlyyao committed Oct 19, 2022
1 parent 932be67 commit 871635d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/avatar/avatar.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<template>
<div :class="avatarClass" :style="customSize">
<div :class="`${name}__inner`">
<img v-if="image && !hideOnLoadFailed" :src="image" :alt="alt" :style="customSize" @error="handleImgLoadError" />
<t-image
v-if="image && !hideOnLoadFailed"
:style="customSize"
v-bind="customImageProps"
@load="handleImgLoadCompleted"
@error="handleImgLoadError"
></t-image>
<div v-else-if="iconContent !== undefined" :class="`${name}__icon`">
<t-node :content="iconContent"></t-node>
</div>
<span v-else>
<t-node :content="avatarContent"></t-node>
</span>
</div>
<div v-if="badgeProps" :class="`${name}__badge`">
<t-badge
Expand All @@ -26,6 +29,7 @@
<script lang="ts">
import { computed, toRefs, defineComponent, getCurrentInstance, inject, ref, SetupContext } from 'vue';
import TBadge from '../badge';
import TImage from '../image';
import config from '../config';
import AvatarProps from './props';
import { TdAvatarGroupProps } from './type';
Expand All @@ -37,14 +41,13 @@ const name = `${prefix}-avatar`;
export default defineComponent({
name,
components: { TNode, TBadge },
components: { TNode, TBadge, TImage },
props: AvatarProps,
emits: ['error'],
setup(props, context: SetupContext) {
const emitEvent = useEmitEvent(props, context.emit);
const internalInstance = getCurrentInstance();
const avatarGroupProps = inject('avatarGroup', {}) as TdAvatarGroupProps;
const avatarContent = computed(() => renderContent(internalInstance, 'default', 'content'));
const iconContent = computed(() => renderTNode(internalInstance, 'icon'));
const sizeValue = ref(props.size || (avatarGroupProps && avatarGroupProps.size));
const avatarClass = computed(() => [
Expand All @@ -64,18 +67,31 @@ export default defineComponent({
}
: {};
});
const handleImgLoadError = (e: Event) => {
const handleImgLoadCompleted = (e: any) => {
emitEvent('load', e);
};
const handleImgLoadError = (e: any) => {
emitEvent('error', e);
};
const baseImageProps = {
src: props.image,
alt: props.alt,
};
const customImageProps = computed(() => ({
...props.ImageProps,
...baseImageProps,
}));
return {
name,
...toRefs(props),
avatarContent,
iconContent,
avatarClass,
customSize,
handleImgLoadCompleted,
handleImgLoadError,
customImageProps,
};
},
});
Expand Down

0 comments on commit 871635d

Please sign in to comment.