Skip to content

feat(image): add "load" event 图片组件添加 “load” 事件 #8006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions components/image/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ Previewable image.

### events

| Events Name | Description | Arguments | Version |
| ----------- | -------------------- | ---------------------- | ------- |
| error | Load failed callback | (event: Event) => void | 3.2.0 |
| Events Name | Description | Arguments | Version |
| ----------- | ------------------------ | ---------------------- | ------- |
| error | Load failed callback | (event: Event) => void | 3.2.0 |
| load | Load successful callback | (event: Event) => void | 4.3.0 |

### previewType

Expand Down
1 change: 1 addition & 0 deletions components/image/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*LVQ3R5JjjJEAAA
| 事件名称 | 说明 | 回调参数 | 版本 |
| -------- | ------------ | ---------------------- | ----- |
| error | 加载错误回调 | (event: Event) => void | 3.2.0 |
| load | 加载成功回调 | (event: Event) => void | 4.3.0 |

### previewType

Expand Down
19 changes: 6 additions & 13 deletions components/vc-image/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const imageProps = () => ({
onError: {
type: Function as PropType<HTMLImageElement['onerror']>,
},
onLoad: {
type: Function as PropType<HTMLImageElement['onload']>,
},
});
export type ImageProps = Partial<ReturnType<typeof imageProps>>;
export type ImageStatus = 'normal' | 'error' | 'loading';
Expand All @@ -69,7 +72,7 @@ const ImageInternal = defineComponent({
name: 'VcImage',
inheritAttrs: false,
props: imageProps(),
emits: ['click', 'error'],
emits: ['click', 'error', 'load'],
setup(props, { attrs, slots, emit }) {
const prefixCls = computed(() => props.prefixCls);
const previewPrefixCls = computed(() => `${prefixCls.value}-preview`);
Expand Down Expand Up @@ -118,8 +121,9 @@ const ImageInternal = defineComponent({
} = groupContext;
const currentId = ref(uuid++);
const canPreview = computed(() => props.preview && !isError.value);
const onLoad = () => {
const onLoad = (e: Event) => {
status.value = 'normal';
emit('load', e);
};
const onError = (e: Event) => {
status.value = 'error';
Expand Down Expand Up @@ -158,16 +162,6 @@ const ImageInternal = defineComponent({
}
};

const img = ref<HTMLImageElement>(null);
watch(
() => img,
() => {
if (status.value !== 'loading') return;
if (img.value.complete && (img.value.naturalWidth || img.value.naturalHeight)) {
onLoad();
}
},
);
let unRegister = () => {};
onMounted(() => {
watch(
Expand Down Expand Up @@ -266,7 +260,6 @@ const ImageInternal = defineComponent({
src: fallback,
}
: { onLoad, onError, src: imgSrc })}
ref={img}
/>

{status.value === 'loading' && (
Expand Down