@majapisoftwares/ui 4.1.7 → 4.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -47,4 +47,5 @@ declare function FileInput<PFP extends object>({ error, className, helpText, onC
|
|
|
47
47
|
previewFileProps?: PFP;
|
|
48
48
|
}): import("react").JSX.Element;
|
|
49
49
|
export type FileInputProps = ComponentPropsWithRef<typeof FileInput>;
|
|
50
|
-
|
|
50
|
+
declare const _default: typeof FileInput;
|
|
51
|
+
export default _default;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import clsx from "../../utils/clsx";
|
|
2
|
-
import { useCallback, useEffect, useRef, useState, } from "react";
|
|
3
|
-
import { useDeepCompareEffect
|
|
2
|
+
import { memo, useCallback, useEffect, useMemo, useRef, useState, } from "react";
|
|
3
|
+
import { useDeepCompareEffect } from "react-use";
|
|
4
4
|
import { defaultHelpTextClassName, defaultLabelClassName, } from "../Input";
|
|
5
5
|
import FileSelect from "../FileSelect";
|
|
6
6
|
import isomorphicObjectId from "@majapisoftwares/next/utils/isomorphicObjectId";
|
|
7
7
|
import { isEqual } from "lodash-es";
|
|
8
8
|
import Text from "../Text";
|
|
9
|
-
import
|
|
9
|
+
import PreviewFile from "./PreviewFile";
|
|
10
10
|
import concurrentForOf from "@majapisoftwares/next/utils/concurrentForOf";
|
|
11
11
|
import paginated from "@majapisoftwares/next/utils/paginated";
|
|
12
12
|
import Pagination from "../Pagination";
|
|
@@ -14,7 +14,7 @@ function FileInput({ error, className, helpText, onChange, name, limit, label, i
|
|
|
14
14
|
const [uploading, setUploading] = useState(false);
|
|
15
15
|
const [innerValue, setInnerValue] = useState(value || []);
|
|
16
16
|
const [currentPage, setCurrentPage] = useState(1);
|
|
17
|
-
const PreviewFileComponent = previewFileComponent || PreviewFile;
|
|
17
|
+
const PreviewFileComponent = useMemo(() => memo(previewFileComponent || PreviewFile, isEqual), [previewFileComponent]);
|
|
18
18
|
const paginatedValue = paginated(innerValue, currentPage - 1, filesPerPage);
|
|
19
19
|
useEffect(() => {
|
|
20
20
|
const totalPages = Math.ceil(innerValue.length / filesPerPage);
|
|
@@ -26,7 +26,7 @@ function FileInput({ error, className, helpText, onChange, name, limit, label, i
|
|
|
26
26
|
if (value && !isEqual(value, innerValue)) {
|
|
27
27
|
setInnerValue(value);
|
|
28
28
|
}
|
|
29
|
-
}, [{ value
|
|
29
|
+
}, [{ value }]);
|
|
30
30
|
const innerRef = useRef({
|
|
31
31
|
get value() {
|
|
32
32
|
return innerValue;
|
|
@@ -98,16 +98,17 @@ function FileInput({ error, className, helpText, onChange, name, limit, label, i
|
|
|
98
98
|
setUploading(false);
|
|
99
99
|
}
|
|
100
100
|
};
|
|
101
|
-
|
|
101
|
+
useDeepCompareEffect(() => {
|
|
102
102
|
if (onChange) {
|
|
103
103
|
onChange({
|
|
104
104
|
target: {
|
|
105
105
|
name,
|
|
106
106
|
value: innerValue.map((file) => ({
|
|
107
107
|
...file,
|
|
108
|
-
url: file.
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
url: file.url ||
|
|
109
|
+
(file.file
|
|
110
|
+
? URL.createObjectURL(file.file)
|
|
111
|
+
: undefined),
|
|
111
112
|
})),
|
|
112
113
|
},
|
|
113
114
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -141,4 +142,4 @@ function FileInput({ error, className, helpText, onChange, name, limit, label, i
|
|
|
141
142
|
</div>)}
|
|
142
143
|
</div>);
|
|
143
144
|
}
|
|
144
|
-
export default FileInput;
|
|
145
|
+
export default memo(FileInput, isEqual);
|
|
@@ -13,4 +13,6 @@ export type PreviewFileProps = {
|
|
|
13
13
|
filesPerPage: number;
|
|
14
14
|
currentPage: number;
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
declare function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, openText, display, additionalInfo, index, className, filesPerPage, currentPage, }: PreviewFileProps): import("react").JSX.Element;
|
|
17
|
+
declare const _default: import("react").MemoExoticComponent<typeof PreviewFile>;
|
|
18
|
+
export default _default;
|
|
@@ -5,13 +5,21 @@ import Text from "../Text";
|
|
|
5
5
|
import numeral from "numeral";
|
|
6
6
|
import Button from "../Button";
|
|
7
7
|
import { ArrowTopRightOnSquareIcon, TrashIcon, } from "@heroicons/react/20/solid";
|
|
8
|
+
import { memo } from "react";
|
|
8
9
|
import clsx from "../../utils/clsx";
|
|
9
10
|
import checkIsVideo from "./isVideo";
|
|
10
11
|
import checkIsImage from "./isImage";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
import { isEqual } from "lodash-es";
|
|
13
|
+
function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, openText, display = "info", additionalInfo, index, className, filesPerPage, currentPage, }) {
|
|
14
|
+
const url = file.url ||
|
|
15
|
+
(file.file
|
|
16
|
+
? URL.createObjectURL(file.file)
|
|
17
|
+
: undefined);
|
|
18
|
+
if (!url) {
|
|
19
|
+
return (<div className={clsx("p-4 text-red-500", className)}>
|
|
20
|
+
Error: No URL or file provided for preview.
|
|
21
|
+
</div>);
|
|
22
|
+
}
|
|
15
23
|
const isVideo = file.type?.startsWith("video") || checkIsVideo(url);
|
|
16
24
|
const isImage = file.type?.startsWith("image") || checkIsImage(url);
|
|
17
25
|
if (display === "preview" && !isVideo && !isImage) {
|
|
@@ -52,3 +60,4 @@ export function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, o
|
|
|
52
60
|
</Button>)}
|
|
53
61
|
</div>);
|
|
54
62
|
}
|
|
63
|
+
export default memo(PreviewFile, isEqual);
|