Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Simple wrapper component that convert child component to a drag and drop file input

License

NotificationsYou must be signed in to change notification settings

rasimandiran/react-file-trap

Repository files navigation

Simple wrapper component that convert child component to a drag and drop file input.

Demo

Edit remark-codesandbox demo

Installation

Install react-file-trap withnpm

npm install react-file-trap

Usage

<FileTrapref={wrapperRef}allowedExtensions={['jpg','png']}handleChange={handleChange}handleDrag={handleDrag}handleDrop={handleDrop}onValidationError={onValidationError}fileCount={3}maxFileSize={2}// MBminFileSize={0.1}// MBbrowseOnClick={false}><divstyle={{margin:10,border:"2px solid red"}}><h2style={{margin:5}}>Current Event:{currentEvent}</h2><h2style={{margin:5}}>Valid File Count:{validFiles.length}</h2><h2style={{margin:5}}>Invalid File Count:{invalidFiles.length}</h2><h2style={{margin:5}}>Last Error:{lastError}</h2></div></FileTrap>

Props

ParameterTypeDescriptionDefault ValueNotes
refrefSuggest to use to reset and/or trigger browse outside of child componentundefinedShould be defined to trigger somefunctions
allowedExtensionsarrayAllowed file extensionsundefinedDon't provide to allow all file types
handleChangefunctionRuns when selected and/or dropped a valid fileMandatory prop to handle files. Seeevents
handleDragfunctionRuns for every drag event. Possible values:dragoverdragleaveundefinedSeeevents
handleDropfunctionRuns when dropped a fileundefinedSeeevents
onValidationErrorfunctionRuns everytime if dropped or selected file invalidundefinedSeeevents
fileCountnumberAllowed file countNumber.MAX_VALUE
maxFileSizenumberAllowed upper limit for file size in MBNumber.MAX_VALUE
minFileSizenumberAllowed lower limit for file size in MB0
browseOnClickbooleanTrigger browse window on click to child componenttrue

Events

handleChange

Runs on every change on the valid files.fileList parameter includes all valid files.

consthandleChange=(fileList)=>{setFiles(filesList);setTotalSize(byteFormatter(newFiles.reduce((acc,file)=>acc+file.size,0)));};

handleDrag

Runs on drag event, eventName parametet can be:dragover ordragleave

consthandleDrag=(eventName)=>{setCurrentEvent(eventName);};

handleDrop

Runs after a dropped files.

consthandleDrop=()=>{setCurrentEvent('dropped');};

onValidationError

Runs after selected/dropped files.

constonValidationError=(invalidFileList,errorMsg)=>{setInvalidFiles([...invalidFiles, ...invalidFileList]);setLastError(errorMsg);}

Functions

browseFiles

Trigger browse window manually.ref prop is needed.

wrapperRef.current.browseFiles();

resetWrapper

To reset selected files.ref prop is needed.

wrapperRef.current.resetWrapper();

removeFile

To remove specific file.file should be provided as parameter.ref prop is also needed.

wrapperRef.current.removeFile(file);

Notes

For more details about usage, please checkexample project in the root directory.

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp