Skip to content
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

fix(image): src = File error triggered handleError (#3448) #4102

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/hooks/useImagePreviewUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import { getFileUrlByFileRaw } from '../_common/js/upload/utils';

export function useImagePreviewUrl(imgUrl: Ref<string | File> | ComputedRef<string | File>) {
const previewUrl = ref('');
const loading = ref(true);

watch(
[imgUrl],
([imgUrl], [preImgUrl]) => {
if (preImgUrl === imgUrl) return;
if (typeof imgUrl === 'string') {
loading.value = false;
previewUrl.value = imgUrl;
return;
}
getFileUrlByFileRaw(imgUrl).then((url) => {
loading.value = false;
previewUrl.value = url;
});
},
{ immediate: true },
);

return { previewUrl };
return { loading, previewUrl };
}
94 changes: 7 additions & 87 deletions src/image/__tests__/__snapshots__/vitest-image.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
// Vitest Snapshot v1

exports[`Image Component > props.fit is equal to contain 1`] = `
<img
alt=""
class="t-image t-image--fit-contain t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
`;

exports[`Image Component > props.fit is equal to cover 1`] = `
<img
alt=""
class="t-image t-image--fit-cover t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
`;

exports[`Image Component > props.fit is equal to fill 1`] = `
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
`;

exports[`Image Component > props.fit is equal to none 1`] = `
<img
alt=""
class="t-image t-image--fit-none t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
`;

exports[`Image Component > props.fit is equal to scale-down 1`] = `
<img
alt=""
class="t-image t-image--fit-scale-down t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
`;

exports[`Image Component > props.loading works fine 1`] = `
<div
class="t-image__wrapper t-image__wrapper--shape-square"
Expand All @@ -54,12 +9,7 @@ exports[`Image Component > props.loading works fine 1`] = `
<!---->
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand All @@ -83,12 +33,7 @@ exports[`Image Component > props.overlayContent works fine 1`] = `
<!---->
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand Down Expand Up @@ -158,12 +103,7 @@ exports[`Image Component > props.placeholder works fine 1`] = `
</div>
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand Down Expand Up @@ -217,12 +157,7 @@ exports[`Image Component > slots.loading works fine 1`] = `
<!---->
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand All @@ -248,12 +183,7 @@ exports[`Image Component > slots.overlay-content works fine 1`] = `
<!---->
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand Down Expand Up @@ -317,12 +247,7 @@ exports[`Image Component > slots.overlayContent works fine 1`] = `
<!---->
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand Down Expand Up @@ -396,12 +321,7 @@ exports[`Image Component > slots.placeholder works fine 1`] = `
</div>
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand Down
11 changes: 7 additions & 4 deletions src/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ export default defineComponent({
{ immediate: true },
);

const { previewUrl } = useImagePreviewUrl(imageStrSrc);
const { loading, previewUrl } = useImagePreviewUrl(imageStrSrc);

watch([previewUrl], () => {
hasError.value = false;
isLoaded.value = false;
watch([loading], () => {
if (!loading) {
hasError.value = false;
isLoaded.value = false;
}
});

const shouldLoad = ref(!props.lazy);
Expand Down Expand Up @@ -196,6 +198,7 @@ export default defineComponent({

{(hasError.value || !shouldLoad.value) && <div class={`${classPrefix.value}-image`} />}
{!(hasError.value || !shouldLoad.value) &&
!loading.value &&
(props.srcset && Object.keys(props.srcset).length ? renderImageSrcset() : renderImage())}
{!(hasError.value || !shouldLoad.value) && !isLoaded.value && (
<div class={`${classPrefix.value}-image__loading`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,12 +1169,7 @@ exports[`Upload Component > props.theme: theme=image-flow works fine 1`] = `
<!---->
<!---->
<!---->
<img
alt=""
class="t-image t-image--fit-fill t-image--position-center"
referrerpolicy="strict-origin-when-cross-origin"
src=""
/>
<!---->
<div
class="t-image__loading"
>
Expand Down
Loading