npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

handle-files-react

v1.1.4

Published

A react component and hooks to handle files

Downloads

258

Readme

Handle Files React

logo.png

npm

Description

Easy and Quick way to handle files in React with <Dropzone/> and useFileInput() hook.

Installation

npm install handle-files-react
yarn add handle-files-react
pnpm add handle-files-react

Quick Start

useFileInput()

import { useFileInput, FileWithMeta, convertToBytes } from 'handle-files-react';

function App(){
    const { open } = useFileInput();
    const [files, setFiles] = useState<FileWithMeta[]>([]);
    
    return (
        <div>
            <button onClick={async ()=>{
                try {
                    const files = await open({
                        multiple: true,
                        maxBytes: convertToBytes(10, "MB"), // 10MB
                        accept: ".mp4, .png", // native input accept attribute
                        maxFiles:5,
                        customValidator: (file) => file.name.includes("Blender")
                    });
                    setFiles(files);
                } catch (e) {
                    console.error(e);
                }
            }}>Select</button>
            <ul>
                {files.map((file) => (
                    <li key={file.origin.name}>
                        {file.origin.name} ({file.toUnit("MB", 1)})
                    </li>
                ))}
            </ul>
        </div>
    )
}

<Dropzone/>

function App() {
    const [files, setFiles] = useState<FileWithMeta[]>([]);
    const [refEl, setRefEl] = useState<HTMLDivElement | null>(null);
    return (
        <div>
            <DropZone
                onDrop={(files) => {
                    setFiles(files);
                }}
                onError={(e) => {
                    console.error(e);
                }}
                multiple={true}
                maxBytes={convertToBytes(10, "MB")}
                accept={".mp4, .png"}
                maxFiles={5}
                customValidator={(file) => {
                    return file.name.includes("Blender");
                }}
            >
                <div
                    ref={(el) => {
                        if(!refEl) {
                            setRefEl(el); // use ref element with state
                        }
                    }}
                    style={{
                        width: 500,
                        height: 500,
                        backgroundColor: "gray",
                    }}
                >
                    DROP ZONE
                    <ul>
                        {files.map((file) => (
                            <li key={file.origin.name}>
                                {file.origin.name} ({file.toUnit("MB", 1)})
                            </li>
                        ))}
                    </ul>
                </div>
            </DropZone>
        </div>
    );
}

API

common

const units = [
    "B",
    "KB",
    "MB",
    "GB",
    "TB",
    "PB",
    "EB",
    "ZB",
    "YB",
] as const;

interface FileInputOptions {
  multiple?: boolean;
  accept?: string;
  maxBytes?: number;
  maxFiles?: number;
  customValidator?: (file: File) => boolean;
};

interface FileWithMeta {
    origin: File;
    toUnit: TGetUnit;
}

useFileInput()

function useFileInput(){
    const open = (options?: FileInputOptions) => {
        // ...
    }
    return { open };
}

<Dropzone/>

interface Props {
    children: React.ReactElement;
    onDrop: (files: FileWithMeta[]) => void;
    onError?: (error: Error) => void;
    onDragEnter?: (e: React.DragEvent) => void;
    onDragLeave?: (e: React.DragEvent) => void;
}

function DropZone(props:Props & FileInputOptions){
    // return clone children
}

Utils

convertToBytes(value: number, unit: TUnit): number

value * 1024^index (index is the index of the unit in the units array)

convertToBytes(10, "MB"); // 10485760
convertToBytes(10, "GB"); // 10737418240
convertToBytes(10, "TB"); // 10995116277760

FileWithMeta.toUnit(unit: TUnit, fixed?: number): string

fileWithMeta.toUnit("MB", 1); // 10.0MB
fileWithMeta.toUnit("GB", 2); // 10.00GB
fileWithMeta.toUnit("TB", 3); // 10.000TB

Tips

Infer the type of the file

accept option is used to filter the file type and use the File.type property.
Here, is the situation where image.ai is not an type of .ai but application/postscript.
But you can get actual file type by error.

type-help.png