@majapisoftwares/ui 4.1.1 → 4.1.3
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.
- package/components/FileInput/FileInput.d.ts +4 -2
- package/components/FileInput/FileInput.jsx +19 -19
- package/components/FileInput/PreviewFile.d.ts +5 -2
- package/components/FileInput/PreviewFile.jsx +7 -8
- package/components/FileInput/isImage.d.ts +1 -0
- package/components/FileInput/isImage.js +10 -0
- package/components/FileInput/isVideo.d.ts +1 -0
- package/components/FileInput/isVideo.js +10 -0
- package/package.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ComponentPropsWithRef, ReactNode } from "react";
|
|
1
|
+
import { ComponentPropsWithRef, ComponentType, ReactNode } from "react";
|
|
2
2
|
import { InputProps } from "../Input";
|
|
3
3
|
import { FileSelectProps } from "../FileSelect";
|
|
4
|
+
import { PreviewFileProps } from "./PreviewFile";
|
|
4
5
|
export type FileFile = {
|
|
5
6
|
_id: string;
|
|
6
7
|
file: File;
|
|
@@ -18,7 +19,7 @@ export type FileUrl = {
|
|
|
18
19
|
size: number;
|
|
19
20
|
};
|
|
20
21
|
export type FileInputFile = FileFile | FileUrl;
|
|
21
|
-
declare function FileInput({ error, className, helpText, onChange, name, limit, label, id, required, onMouseOver, onMouseOut, readOnly, value, emptyText, downloadText, openText, fileDisplay, asyncUpload, onRejectFiles, loading, maxConcurrentUploads, fileAdditionalInfo, ref, fileSelectClassName, previewFileClassName, filesPerPage, ...props }: Pick<InputProps<false>, "error" | "className" | "helpText" | "name" | "label" | "id" | "required" | "onMouseOver" | "onMouseOut"> & Omit<FileSelectProps, "onAcceptFiles" | "onRejectFiles"> & {
|
|
22
|
+
declare function FileInput({ error, className, helpText, onChange, name, limit, label, id, required, onMouseOver, onMouseOut, readOnly, value, emptyText, downloadText, openText, fileDisplay, asyncUpload, onRejectFiles, loading, maxConcurrentUploads, fileAdditionalInfo, ref, fileSelectClassName, previewFileClassName, filesPerPage, previewFileComponent, ...props }: Pick<InputProps<false>, "error" | "className" | "helpText" | "name" | "label" | "id" | "required" | "onMouseOver" | "onMouseOut"> & Omit<FileSelectProps, "onAcceptFiles" | "onRejectFiles"> & {
|
|
22
23
|
readOnly?: boolean;
|
|
23
24
|
value?: FileInputFile[];
|
|
24
25
|
onChange?: (event: {
|
|
@@ -42,6 +43,7 @@ declare function FileInput({ error, className, helpText, onChange, name, limit,
|
|
|
42
43
|
fileSelectClassName?: string;
|
|
43
44
|
previewFileClassName?: string;
|
|
44
45
|
filesPerPage?: number;
|
|
46
|
+
previewFileComponent?: ComponentType<PreviewFileProps>;
|
|
45
47
|
}): import("react").JSX.Element;
|
|
46
48
|
export type FileInputProps = ComponentPropsWithRef<typeof FileInput>;
|
|
47
49
|
export default FileInput;
|
|
@@ -10,10 +10,11 @@ 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";
|
|
13
|
-
function FileInput({ error, className, helpText, onChange, name, limit, label, id, required, onMouseOver, onMouseOut, readOnly, value, emptyText = "No files", downloadText = "Download", openText = "Open", fileDisplay, asyncUpload, onRejectFiles, loading, maxConcurrentUploads = 1, fileAdditionalInfo, ref, fileSelectClassName, previewFileClassName, filesPerPage = 10, ...props }) {
|
|
13
|
+
function FileInput({ error, className, helpText, onChange, name, limit, label, id, required, onMouseOver, onMouseOut, readOnly, value, emptyText = "No files", downloadText = "Download", openText = "Open", fileDisplay, asyncUpload, onRejectFiles, loading, maxConcurrentUploads = 1, fileAdditionalInfo, ref, fileSelectClassName, previewFileClassName, filesPerPage = 10, previewFileComponent, ...props }) {
|
|
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
18
|
const paginatedValue = paginated(innerValue, currentPage - 1, filesPerPage);
|
|
18
19
|
useEffect(() => {
|
|
19
20
|
const totalPages = Math.ceil(innerValue.length / filesPerPage);
|
|
@@ -112,33 +113,32 @@ function FileInput({ error, className, helpText, onChange, name, limit, label, i
|
|
|
112
113
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
114
|
});
|
|
114
115
|
}
|
|
115
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
116
116
|
}, [innerValue]);
|
|
117
117
|
const handleDeleteClick = useCallback((clickedFile) => () => {
|
|
118
118
|
setInnerValue((value) => [
|
|
119
119
|
...value.filter((file) => file !== clickedFile),
|
|
120
120
|
]);
|
|
121
121
|
}, []);
|
|
122
|
-
return (<div data-input-name={name} data-error={error ? "" : undefined} data-loading={loading ? "" : undefined} className={clsx("relative data-loading:animate-pulse", className)} onMouseOver={onMouseOver} onMouseOut={onMouseOut}>
|
|
123
|
-
{label && (<label htmlFor={id} className={defaultLabelClassName}>
|
|
124
|
-
{label}
|
|
125
|
-
{required && (<>
|
|
126
|
-
{" "}
|
|
127
|
-
<span className="text-red-500">*</span>
|
|
128
|
-
</>)}
|
|
129
|
-
</label>)}
|
|
122
|
+
return (<div data-input-name={name} data-error={error ? "" : undefined} data-loading={loading ? "" : undefined} className={clsx("relative data-loading:animate-pulse", className)} onMouseOver={onMouseOver} onMouseOut={onMouseOut}>
|
|
123
|
+
{label && (<label htmlFor={id} className={defaultLabelClassName}>
|
|
124
|
+
{label}
|
|
125
|
+
{required && (<>
|
|
126
|
+
{" "}
|
|
127
|
+
<span className="text-red-500">*</span>
|
|
128
|
+
</>)}
|
|
129
|
+
</label>)}
|
|
130
130
|
<div className={clsx("grid grid-cols-1 gap-4", {
|
|
131
131
|
"md:grid-cols-2": !!innerValue.length,
|
|
132
132
|
"min-h-[140px]": !!innerValue.length || !readOnly,
|
|
133
|
-
})}>
|
|
134
|
-
{paginatedValue.map((file, index) => (<
|
|
135
|
-
{innerValue.length > filesPerPage && (<Pagination className="col-span-2 mx-auto" currentPage={currentPage} totalItems={innerValue.length} onChangePage={setCurrentPage} itemsPerPage={filesPerPage}/>)}
|
|
136
|
-
{readOnly && !innerValue.length && (<Text variant="secondary">{emptyText}</Text>)}
|
|
137
|
-
{!readOnly && (!limit || innerValue.length < limit) && (<FileSelect {...props} className={fileSelectClassName} id={id} onAcceptFiles={handleAcceptFiles} limit={limit ? limit - innerValue.length : undefined} uploading={uploading} onRejectFiles={onRejectFiles} error={error}/>)}
|
|
138
|
-
</div>
|
|
139
|
-
{helpText && (<div className={clsx(defaultHelpTextClassName, "in-data-error:text-error-500")}>
|
|
140
|
-
{helpText}
|
|
141
|
-
</div>)}
|
|
133
|
+
})}>
|
|
134
|
+
{paginatedValue.map((file, index) => (<PreviewFileComponent key={index} file={file} readOnly={readOnly} handleDeleteClick={handleDeleteClick(file)} downloadText={downloadText} display={fileDisplay} openText={openText} additionalInfo={fileAdditionalInfo} index={index} className={previewFileClassName} filesPerPage={filesPerPage} currentPage={currentPage}/>))}
|
|
135
|
+
{innerValue.length > filesPerPage && (<Pagination className="col-span-2 mx-auto" currentPage={currentPage} totalItems={innerValue.length} onChangePage={setCurrentPage} itemsPerPage={filesPerPage}/>)}
|
|
136
|
+
{readOnly && !innerValue.length && (<Text variant="secondary">{emptyText}</Text>)}
|
|
137
|
+
{!readOnly && (!limit || innerValue.length < limit) && (<FileSelect {...props} className={fileSelectClassName} id={id} onAcceptFiles={handleAcceptFiles} limit={limit ? limit - innerValue.length : undefined} uploading={uploading} onRejectFiles={onRejectFiles} error={error}/>)}
|
|
138
|
+
</div>
|
|
139
|
+
{helpText && (<div className={clsx(defaultHelpTextClassName, "in-data-error:text-error-500")}>
|
|
140
|
+
{helpText}
|
|
141
|
+
</div>)}
|
|
142
142
|
</div>);
|
|
143
143
|
}
|
|
144
144
|
export default FileInput;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FileInputFile } from "./FileInput";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
|
-
export
|
|
3
|
+
export type PreviewFileProps = {
|
|
4
4
|
file: FileInputFile;
|
|
5
5
|
readOnly?: boolean;
|
|
6
6
|
handleDeleteClick: () => void;
|
|
@@ -10,4 +10,7 @@ export declare function PreviewFile({ file, readOnly, handleDeleteClick, downloa
|
|
|
10
10
|
additionalInfo?: (file: FileInputFile, index: number) => ReactNode;
|
|
11
11
|
index: number;
|
|
12
12
|
className?: string;
|
|
13
|
-
|
|
13
|
+
filesPerPage: number;
|
|
14
|
+
currentPage: number;
|
|
15
|
+
};
|
|
16
|
+
export declare function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, openText, display, additionalInfo, index, className, filesPerPage, currentPage, }: PreviewFileProps): import("react").JSX.Element;
|
|
@@ -6,16 +6,14 @@ import numeral from "numeral";
|
|
|
6
6
|
import Button from "../Button";
|
|
7
7
|
import { ArrowTopRightOnSquareIcon, TrashIcon, } from "@heroicons/react/20/solid";
|
|
8
8
|
import clsx from "../../utils/clsx";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, openText, display = "info", additionalInfo, index, className, }) {
|
|
9
|
+
import checkIsVideo from "./isVideo";
|
|
10
|
+
import checkIsImage from "./isImage";
|
|
11
|
+
export function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, openText, display = "info", additionalInfo, index, className, filesPerPage, currentPage, }) {
|
|
12
12
|
const url = file.file
|
|
13
13
|
? URL.createObjectURL(file.file)
|
|
14
14
|
: file.url;
|
|
15
|
-
const isVideo = file.type?.startsWith("video") ||
|
|
16
|
-
|
|
17
|
-
const isImage = file.type?.startsWith("image") ||
|
|
18
|
-
imageExtensions.some((e) => url.endsWith(e));
|
|
15
|
+
const isVideo = file.type?.startsWith("video") || checkIsVideo(url);
|
|
16
|
+
const isImage = file.type?.startsWith("image") || checkIsImage(url);
|
|
19
17
|
if (display === "preview" && !isVideo && !isImage) {
|
|
20
18
|
display = "info";
|
|
21
19
|
}
|
|
@@ -45,7 +43,8 @@ export function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, o
|
|
|
45
43
|
</Button>
|
|
46
44
|
</Group>)}
|
|
47
45
|
</Stack>
|
|
48
|
-
{additionalInfo &&
|
|
46
|
+
{additionalInfo &&
|
|
47
|
+
additionalInfo(file, index + (currentPage - 1) * filesPerPage)}
|
|
49
48
|
</Stack>
|
|
50
49
|
</Group>)}
|
|
51
50
|
{!readOnly && (<Button icon variant="filled" color="default" className="absolute top-2 right-2 group-hover:opacity-100 sm:opacity-0" onClick={handleDeleteClick}>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function isImage(file: File | string): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const imageExtensions = [".png", ".jpg", ".jpeg", ".webp"];
|
|
2
|
+
export default function isImage(file) {
|
|
3
|
+
if (typeof file === "string") {
|
|
4
|
+
return imageExtensions.some((ext) => file.endsWith(ext));
|
|
5
|
+
}
|
|
6
|
+
if (file instanceof File) {
|
|
7
|
+
return imageExtensions.some((ext) => file.name.endsWith(ext));
|
|
8
|
+
}
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function isVideo(file: File | string): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const videoExtensions = [".mp4"];
|
|
2
|
+
export default function isVideo(file) {
|
|
3
|
+
if (typeof file === "string") {
|
|
4
|
+
return videoExtensions.some((ext) => file.endsWith(ext));
|
|
5
|
+
}
|
|
6
|
+
if (file instanceof File) {
|
|
7
|
+
return videoExtensions.some((ext) => file.name.endsWith(ext));
|
|
8
|
+
}
|
|
9
|
+
return false;
|
|
10
|
+
}
|