Skip to content

Commit

Permalink
fix: image display in file input field #154 (#167)
Browse files Browse the repository at this point in the history
* fix: image display in file input field #154

* changeset as patch
  • Loading branch information
foyarash authored Mar 11, 2024
1 parent 3daf60d commit 104aaba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-knives-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

fix: image display in file input field ([#154](https://github.com/premieroctet/next-admin/issues/154))
31 changes: 16 additions & 15 deletions packages/next-admin/src/components/inputs/FileWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@ const FileWidget = (props: WidgetProps) => {
}
};

const getFileType = async (signal: AbortSignal) => {
const getFileType = () => {
if (!props.value) {
return;
}

try {
const response = await fetch(props.value, {
signal,
});
setFileIsImage(
response.headers.get("Content-Type")?.includes("image") ?? false
);
setFileImage(props.value);
const img = new Image();

img.onload = () => {
setFileIsImage(true);
setFileImage(props.value);
};

img.onerror = () => {
setFileIsImage(false);
setFileImage(props.value);
};

img.src = props.value as string;
} catch (error) {
setFileIsImage(false);
}
Expand All @@ -70,13 +77,7 @@ const FileWidget = (props: WidgetProps) => {
};

useEffect(() => {
const abortController = new AbortController();

getFileType(abortController.signal);

return () => {
abortController.abort();
};
getFileType();
}, []);

let isLink = false;
Expand Down

0 comments on commit 104aaba

Please sign in to comment.