@majapisoftwares/ui 4.1.2 → 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.
@@ -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);
@@ -130,7 +131,7 @@ function FileInput({ error, className, helpText, onChange, name, limit, label, i
130
131
  "md:grid-cols-2": !!innerValue.length,
131
132
  "min-h-[140px]": !!innerValue.length || !readOnly,
132
133
  })}>
133
- {paginatedValue.map((file, index) => (<PreviewFile 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}/>))}
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}/>))}
134
135
  {innerValue.length > filesPerPage && (<Pagination className="col-span-2 mx-auto" currentPage={currentPage} totalItems={innerValue.length} onChangePage={setCurrentPage} itemsPerPage={filesPerPage}/>)}
135
136
  {readOnly && !innerValue.length && (<Text variant="secondary">{emptyText}</Text>)}
136
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}/>)}
@@ -1,6 +1,6 @@
1
1
  import { FileInputFile } from "./FileInput";
2
2
  import { ReactNode } from "react";
3
- export declare function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, openText, display, additionalInfo, index, className, filesPerPage, currentPage, }: {
3
+ export type PreviewFileProps = {
4
4
  file: FileInputFile;
5
5
  readOnly?: boolean;
6
6
  handleDeleteClick: () => void;
@@ -12,4 +12,5 @@ export declare function PreviewFile({ file, readOnly, handleDeleteClick, downloa
12
12
  className?: string;
13
13
  filesPerPage: number;
14
14
  currentPage: number;
15
- }): import("react").JSX.Element;
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
- const videoExtensions = [".mp4"];
10
- const imageExtensions = [".png", ".jpg", ".jpeg", ".webp"];
9
+ import checkIsVideo from "./isVideo";
10
+ import checkIsImage from "./isImage";
11
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
- videoExtensions.some((e) => url.endsWith(e));
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
  }
@@ -47,7 +45,6 @@ export function PreviewFile({ file, readOnly, handleDeleteClick, downloadText, o
47
45
  </Stack>
48
46
  {additionalInfo &&
49
47
  additionalInfo(file, index + (currentPage - 1) * filesPerPage)}
50
- {/*{additionalInfo && additionalInfo(file, index)}*/}
51
48
  </Stack>
52
49
  </Group>)}
53
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
+ }
package/package.json CHANGED
@@ -4,6 +4,6 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/majapisoftwares/ui.git"
6
6
  },
7
- "version": "4.1.2",
7
+ "version": "4.1.3",
8
8
  "type": "module"
9
9
  }