1.0.8 • Published 4 years ago

react-file-input-previews-base64 v1.0.8

Weekly downloads
548
License
MIT
Repository
github
Last release
4 years ago

react-file-input-previews-base64

This package provides an easy to use, ready to go and customizable wrapper around file input, with option for image previews and returning file as base64 string.

demo

Installation

npm install --save react-file-input-previews-base64

Basic Usage

import FileInputComponent from 'react-file-input-previews-base64'

Example Code

<FileInputComponent
  labelText="Select file"
  labelStyle={{fontSize:14}}
  multiple={true}
  callbackFunction={(file_arr)=>{console.log(file_arr)}}
  accept="image/*" 
/>

Options

PropTypeEffectDefault Value
labelTextstringThe text to show in label"File Upload"
multiplebooleanWhether multiple files can be selected or nottrue
acceptstringstring which is passed to accept field of input tag, to specify the types of files that can be selected"*"
imagePreviewbooleanWhether preview section is shown or nottrue
textBoxVisiblebooleanWhether a controlled text field showing information be shownfalse
useTapEventPluginbooleanWhether to use onTouchTap or onClickfalse
callbackFunctionfunctionThe function to be called when files are processed, can take either array of file describing objects or single object depending on if multiple files are allowed()=>{}
nonPreviewComponentcomponentComponent to show in preview section for non-image files, title, size and type are passed to this as propsincluded <NonPreviewDefaultComponent />
buttonComponentcomponentThe component to show for button, onClick or onTouchTap will be merged to its props to trigger the file selector box to show internally<button type="button">Attach</button>
textFieldComponentcomponentThe component to show for controlled text field, onClick or onTouchTap will be merged to its props to trigger the file selector box to show internally, along with value prop for showing information like "foobar.jpg", "2 files selected", "No file selected" etc<input type="text" />
imageContainerStyleobjectObject passed to style prop of image preview section container div{display:"flex", flexDirection:"row", width:"100%", flexWrap:"wrap"}
imageStyleobjectObject passed to style prop of image previews{marginTop: 5, marginBottom: 5, marginRight: 5, width: "auto", height: "30vmin", boxShadow:"rgba(0, 0, 0, 0.188235) 0px 10px 30px, rgba(0, 0, 0, 0.227451) 0px 6px 10px"}
parentStyleobjectObject passed to style prop of container div of whole component{marginTop:14}
labelStyleobjectObject passed to style prop of label{fontSize: 16,color:'rgba(0, 0, 0, 0.298039)',display:'block'}
inputIdstringString passed to id prop of input and htmlFor tag of label components, if not present clicking on label won't trigger the file sector to shownone
inputNamestringString passed to name prop of input, if not present, using this component as part of form will not work.none
defaultFilesarrayArray consisting of urls of files to show as pre-selections, useful for edit forms, currently only supports urls to image files[]

Note

  • onTouchTap is implmented by react-tap-event-plugin and is used in the amazing material-ui.
  • I am using this with material-ui library and the default props for styles follows the material design pattern.
  • The structure of file describing object is:
{
  name: "IMG_20160813_102226.jpg",
  type: "image/jpeg",
  size: 645,
  base64: "data:image/jpeg;base64,/9j/4SzyRXhpZgAATU0AKgA...",
  file: File
}