1.0.4 • Published 1 year ago

@mikhailmogilnikov/file-trigger v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

FileTrigger

The FileTrigger component is a React component designed to handle file uploads.

Installation

npm install @mikhailmogilnikov/file-trigger

Usage

Basic Example

import { FileTrigger } from '@mikhailmogilnikov/file-trigger';

<FileTrigger onAttach={(files) => console.log('Files uploaded:', files)}>
  Upload Files
</FileTrigger>

Advanced Example

<FileTrigger
  id='images' // passes directly to input
  name='images'
  fileType="image"
  fileLimitBytes={5000000} // File size limit in bytes
  onAttach={(files) => console.log('Images uploaded:', files)}
  onLimit={(file) => console.log('File too large:', file)}
>
  <Button>Upload Images</Button>
</FileTrigger>

Any jsx inside will open file dialog on click.

Props

PropDescriptionTypeDefault
refRef to the input element.Ref<HTMLInputElement> \| nullnull
onAttachCallback function when files are uploaded.(files: FileList) => voidundefined
fileLimitBytesFile size limit in bytes.numberundefined
onLimitCallback function when a file exceeds the size limit.(file: File) => voidundefined
disableAutoClearIf true, does not clear the input value after file selection.booleanfalse
showInputIf true, displays the input element.booleanfalse
fileTypeType of file that can be uploaded.FileType'all'

+ all props from input element.

Examples

Image Upload

<FileTrigger
  fileType="image"
  onAttach={(files) => console.log('Images uploaded:', files)}
>
  Upload Images
</FileTrigger>

File Size Limitation

<FileTrigger
  fileLimitBytes={1000000} // 1 MB
  onLimit={(file) => console.log('File too large:', file)}
>
  Upload File
</FileTrigger>

Types

FileType

FileType is a union type that defines the types of files that can be uploaded. It can be one of the following:

  • 'image': Images.
  • 'video': Videos.
  • 'pdf': PDF files.
  • 'audio': Audio files.
  • 'text': Text files.
  • 'all': All file types.