2.0.0-canary-4 • Published 2 years ago

use-file-picker v2.0.0-canary-4

Weekly downloads
273
License
MIT
Repository
github
Last release
2 years ago

Welcome to use-file-picker šŸ‘‹

Simple react hook to open browser file selector.

alt Version alt License: MIT alt Twitter: twitter.com/JankiewiczMi/

šŸ  Homepage

Documentation

Install

npm i use-file-picker or yarn add use-file-picker

StoryBook

https://use-file-picker.vercel.app/

Usage

Simple txt file content reading

https://codesandbox.io/s/inspiring-swartz-pjxze?file=/src/App.js

import { useFilePicker } from 'use-file-picker';
import React from 'react';

export default function App() {
  const [openFileSelector, { filesContent, loading }] = useFilePicker({
    accept: '.txt',
  });

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <button onClick={() => openFileSelector()}>Select files </button>
      <br />
      {filesContent.map((file, index) => (
        <div>
          <h2>{file.name}</h2>
          <div key={index}>{file.content}</div>
          <br />
        </div>
      ))}
    </div>
  );
}

Reading and rendering Images

https://codesandbox.io/s/busy-nightingale-oox7z?file=/src/App.js

import { useFilePicker } from 'use-file-picker';
import React from 'react';

export default function App() {
  const [openFileSelector, { filesContent, loading, errors }] = useFilePicker({
    readAs: 'DataURL',
    accept: 'image/*',
    multiple: true,
    limitFilesConfig: { max: 1 },
    // minFileSize: 0.1, // in megabytes
    maxFileSize: 50,
    imageSizeRestrictions: {
      maxHeight: 900, // in pixels
      maxWidth: 1600,
      minHeight: 600,
      minWidth: 768,
    },
  });

  if (loading) {
    return <div>Loading...</div>;
  }

  if (errors.length) {
    return <div>Error...</div>;
  }

  return (
    <div>
      <button onClick={() => openFileSelector()}>Select files </button>
      <br />
      {filesContent.map((file, index) => (
        <div key={index}>
          <h2>{file.name}</h2>
          <img alt={file.name} src={file.content}></img>
          <br />
        </div>
      ))}
    </div>
  );
}

On change callbacks

import { useFilePicker } from 'use-file-picker';
import React from 'react';

export default function App() {
  const [openFileSelector, { filesContent, loading, errors }] = useFilePicker({
    readAs: 'DataURL',
    accept: 'image/*',
    multiple: true,
    onFilesSelected: ({ plainFiles, filesContent, errors }) => {
      // this callback is always called, even if there are errors
      console.log('onFilesSelected', plainFiles, filesContent, errors);
    },
    onFilesRejected: ({ errors }) => {
      // this callback is called when there were validation errors
      console.log('onFilesRejected', errors);
    },
    onFilesSuccessfullySelected: ({ plainFiles, filesContent }) => {
      // this callback is called when there were no validation errors
      console.log('onFilesSuccessfullySelected', plainFiles, filesContent);
    },
  });

  if (loading) {
    return <div>Loading...</div>;
  }

  if (errors.length) {
    return <div>Error...</div>;
  }

  return (
    <div>
      <button onClick={() => openFileSelector()}>Select files </button>
      <br />
      {filesContent.map((file, index) => (
        <div key={index}>
          <h2>{file.name}</h2>
          <img alt={file.name} src={file.content}></img>
          <br />
        </div>
      ))}
    </div>
  );
}

Advanced usage

https://codesandbox.io/s/musing-moon-wuq4u?file=/src/App.js

import { useFilePicker } from 'use-file-picker';
import React from 'react';

export default function App() {
  const [openFileSelector, { filesContent, loading, errors, plainFiles, clear }] = useFilePicker({
    multiple: true,
    readAs: 'DataURL', // availible formats: "Text" | "BinaryString" | "ArrayBuffer" | "DataURL"
    // accept: '.ics,.pdf',
    accept: ['.json', '.pdf'],
    limitFilesConfig: { min: 2, max: 3 },
    // minFileSize: 1, // in megabytes
    // maxFileSize: 1,
    // readFilesContent: false, // ignores file content
  });

  if (errors.length) {
    return (
      <div>
        <button onClick={() => openFileSelector()}>Something went wrong, retry! </button>
        {errors[0].fileSizeTooSmall && 'File size is too small!'}
        {errors[0].fileSizeToolarge && 'File size is too large!'}
        {errors[0].readerError && 'Problem occured while reading file!'}
        {errors[0].maxLimitExceeded && 'Too many files'}
        {errors[0].minLimitNotReached && 'Not enough files'}
      </div>
    );
  }

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <button onClick={() => openFileSelector()}>Select file </button>
      <button onClick={() => clear()}>Clear</button>
      <br />
      Number of selected files:
      {plainFiles.length}
      <br />
      {/* If readAs is set to DataURL, You can display an image */}
      {!!filesContent.length && <img src={filesContent[0].content} />}
      <br />
      {plainFiles.map(file => (
        <div key={file.name}>{file.name}</div>
      ))}
    </div>
  );
}

Keeping the previously selected files and removing them from selection on demand

If you want to keep the previously selected files and remove them from the selection, you can use a separate hook called useImperativeFilePicker that is also exported in this package. For files removal, you can use removeFileByIndex or removeFileByReference functions.

import React from 'react';
import { useImperativeFilePicker } from 'use-file-picker';

const Imperative = () => {
  const [
    openFileSelector,
    { filesContent, loading, errors, plainFiles, clear, removeFileByIndex, removeFileByReference },
  ] = useImperativeFilePicker({
    multiple: true,
    readAs: 'Text',
    readFilesContent: true,
    onFilesSelected: ({ plainFiles, filesContent, errors }) => {
      // this callback is always called, even if there are errors
      console.log('onFilesSelected', plainFiles, filesContent, errors);
    },
    onFilesRejected: ({ errors }) => {
      // this callback is called when there were validation errors
      console.log('onFilesRejected', errors);
    },
    onFilesSuccessfullySelected: ({ plainFiles, filesContent }) => {
      // this callback is called when there were no validation errors
      console.log('onFilesSuccessfullySelected', plainFiles, filesContent);
    },
  });

  if (errors.length) {
    return (
      <div>
        <button onClick={() => openFileSelector()}>Something went wrong, retry! </button>
        <div style={{ display: 'flex', flexDirection: 'column' }}>
          {console.log(errors)}
          {Object.entries(errors[0])
            .filter(([key, value]) => key !== 'name' && value)
            .map(([key]) => (
              <div key={key}>{key}</div>
            ))}
        </div>
      </div>
    );
  }

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <button onClick={async () => openFileSelector()}>Select file</button>
      <button onClick={() => clear()}>Clear</button>
      <br />
      Amount of selected files:
      {plainFiles.length}
      <br />
      Amount of filesContent:
      {filesContent.length}
      <br />
      {plainFiles.map((file, i) => (
        <div>
          <div style={{ display: 'flex', alignItems: 'center' }} key={file.name}>
            <div>{file.name}</div>
            <button style={{ marginLeft: 24 }} onClick={() => removeFileByReference(file)}>
              Remove by reference
            </button>
            <button style={{ marginLeft: 24 }} onClick={() => removeFileByIndex(i)}>
              Remove by index
            </button>
          </div>
          <div>{filesContent[i]?.content}</div>
        </div>
      ))}
    </div>
  );
};

API

Props

Prop nameDescriptionDefault valueExample values
multipleAllow user to pick multiple files at oncetruetrue, false
acceptSet type of files that user can choose from the list"*"".png", ".txt", "image/*", ".txt"
readAsSet a return type of filesContent"Text""DataURL", "Text", "BinaryString", "ArrayBuffer"
limitFilesConfigSet maximum and minimum files that user can selectn/a{min: 1, max: 2}, {max: 1}
readFilesContentIgnores files content and omits reading process if set to falsetruetrue, false
minFileSizeSet minimum limit of file size in megabytesn/a0.01 - 50
maxFileSizeSet maximum limit of file size in megabytesn/a0.01 - 50
imageSizeRestrictionsSet maximum and minimum constraints for image size in pixelsn/a{ maxHeight: 1024, minWidth: 768, minHeight:480 }
validatorsAdd custom validation logic[]MyValidator, MySecondValidator
initializeWithCustomParametersallows for customization of the input element that is created by the file picker. It accepts a function that takes in the input element as a parameter and can be used to set any desired attributes or styles on the element.n/a(input) => input.setAttribute("disabled", "")
onFilesSelectedA callback function that is called when files are successfully selected. The function is passed an array of objects with information about each successfully selected filen/a(data) => console.log(data)
onFilesSuccessfullySelectedA callback function that is called when files are successfully selected. The function is passed an array of objects with information about each successfully selected filen/a(data) => console.log(data)
onFilesRejectedA callback function that is called when files are rejected due to validation errors or other issues. The function is passed an array of objects with information about each rejected filen/a(data) => console.log(data)

Returns

NameDescription
openFileSelectorOpens file selector
clearClears all files and errors
filesContentGet files array of type FileContent
plainFilesGet array of the File objects
loadingTrue if the reading files is in progress, otherwise False
errorsGet errors array of type FileError if any appears

Custom validation

useFilePicker allows for injection of custom validation logic. Validation is divided into two steps:

  • validateBeforeParsing - takes place before parsing. You have access to config passed as argument to useFilePicker hook and all plain file objects of selected files by user. Called once for all files after selection.
  • validateAfterParsing - takes place after parsing (or is never called if readFilesContent is set to false).You have access to config passed as argument to useFilePicker hook, FileWithPath object that is currently being validated and the reader object that has loaded that file. Called individually for every file.
interface Validator {
  validateBeforeParsing(config: UseFilePickerConfig, plainFiles: File[]): Promise<void>;
  validateAfterParsing(config: UseFilePickerConfig, file: FileWithPath, reader: FileReader): Promise<void>;
}

Validators must return Promise object - resolved promise means that file passed validation, rejected promise means that file did not pass.

Example validator

/**
 * validateBeforeParsing allows the user to select only an even number of files
 * validateAfterParsing allows the user to select only files that have not been modified in the last 24 hours
 */
const customValidator: Validator = {
  validateBeforeParsing: async (config, plainFiles) =>
    new Promise((res, rej) => (plainFiles.length % 2 === 0 ? res() : rej({ oddNumberOfFiles: true }))),
  validateAfterParsing: async (config, file, reader) =>
    new Promise((res, rej) =>
      file.lastModified < new Date().getTime() - 24 * 60 * 60 * 1000
        ? res()
        : rej({ fileRecentlyModified: true, lastModified: file.lastModified })
    ),
};

Interfaces

LimitFilesConfig

LimitFilesConfig {
  min?: number;
  max?: number;
}

UseFilePickerConfig

UseFilePickerConfig extends Options {
  multiple?: boolean;
  accept?: string | string[];
  readAs?: ReadType;
  limitFilesConfig?: LimitFilesConfig;
  readFilesContent?: boolean;
  imageSizeRestrictions?: ImageDims;
  validators?: Validator[];
  onFilesSelected?: (data: SelectedFilesOrErrors) => void;
  onFilesSuccessfullySelected?: (data: SelectedFiles) => void;
  onFilesRejected?: (data: FileErrors) => void;
  initializeWithCustomParameters?: (inputElement: HTMLInputElement) => void;
}

FileContent

FileContent {
  lastModified: number;
  name: string;
  content: string;
}

ImageDims

ImageDims {
  minWidth?: number;
  maxWidth?: number;
  minHeight?: number;
  maxHeight?: number;
}

Options

Options extends ImageDims {
  minFileSize?: number;
  maxFileSize?: number;
}

FileError

FileError extends FileSizeError, FileReaderError, FileLimitError, ImageDimensionError {
  name?: string;
}

FileReaderError

FileReaderError {
  readerError?: DOMException | null;
}

FileLimitError

FileLimitError {
  minLimitNotReached?: boolean;
  maxLimitExceeded?: boolean;
}

FileSizeError

FileSizeError {
  fileSizeToolarge?: boolean;
  fileSizeTooSmall?: boolean;
}

ImageDimensionError

ImageDimensionError {
  imageWidthTooBig?: boolean;
  imageWidthTooSmall?: boolean;
  imageHeightTooBig?: boolean;
  imageHeightTooSmall?: boolean;
  imageNotLoaded?: boolean;
}

SelectedFiles

SelectedFiles {
  plainFiles: FileWithPath[];
  filesContent: FileContent[];
}

FileErrors

FileErrors {
  errors: FileError[];
}

SelectedFilesOrErrors

SelectedFilesOrErrors {
    plainFiles?: undefined;
    filesContent?: undefined;
    errors: FileError[];
} | {
    errors?: undefined;
    plainFiles: FileWithPath[];
    filesContent: FileContent[];
}

Authors

šŸ‘¤ Milosz Jankiewicz

šŸ‘¤ Kamil Planer

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!
Feel free to check the issues page.

Show your support

Give a ā­ļø if this project helped you!

cooldoge Discord & Slack Emoji