@fewings/react
Version:
Useful react components and hooks
65 lines (60 loc) • 2.29 kB
text/typescript
import * as React$1 from 'react';
declare const FILE_UNITS: readonly ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
type TUnit = (typeof FILE_UNITS)[number];
type TGetUnitFn = (unit: TUnit, fixed?: number) => string;
type THandleFileOptions = {
multiple?: boolean;
accept?: string | string[];
maxBytes?: number;
maxFiles?: number;
customValidator?: (file: File) => boolean | Promise<boolean>;
};
type TFileWithMeta = {
origin: File;
toUnit: TGetUnitFn;
};
type THandleFileEventHooks = {
onChange?: (files: TFileWithMeta[]) => void;
onError?: (error: Error) => void;
};
declare function useHandleFile({ onChange, onError, ...options }: THandleFileOptions & THandleFileEventHooks): {
select: () => Promise<TFileWithMeta[]>;
register: () => {
onDragOver: (e: React.DragEvent) => void;
onDragEnter: (e: React.DragEvent) => void;
onDragLeave: (e: React.DragEvent) => void;
onDrop: (e: React.DragEvent) => Promise<void>;
onClick: (e: React.MouseEvent) => void;
};
inputRef: React$1.RefObject<HTMLInputElement | null>;
isDragOver: boolean;
};
/**
* Set up options for the input element
*/
declare function setUpOptions(inputEl: HTMLInputElement, options?: THandleFileOptions): void;
/**
* Get a function that converts bytes to the specified unit
*/
declare function getUnitFunc(bytes: number): TGetUnitFn;
/**
* Convert FileList to FileWithMeta[]
*/
declare function convertFilesWithMeta(files: FileList): TFileWithMeta[];
/**
* Validate options for each file
*/
declare function validateOptions(files: FileList, options?: THandleFileOptions): Promise<void>;
/**
* Verify if the file type is allowed
*/
declare function verifyAccept(type: string, accept: string | string[]): boolean;
/**
* Get byte size from value and unit
*/
declare function convertToBytes(value: number, unit: TUnit): number;
/**
* return video, audio file's duration with seconds
*/
declare function getFileDuration(file: File): Promise<number>;
export { FILE_UNITS, type TFileWithMeta, type TGetUnitFn, type THandleFileEventHooks, type THandleFileOptions, type TUnit, convertFilesWithMeta, convertToBytes, getFileDuration, getUnitFunc, setUpOptions, useHandleFile, validateOptions, verifyAccept };