Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-images-uploading

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-images-uploading

A simple images uploader without UI. Building by yourself

  • 3.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
31K
decreased by-0.21%
Maintainers
1
Weekly downloads
 
Created
Source

react-images-uploading

NPM JavaScript Style Guide

Images uploader

A simple images uploader without UI. Building by yourself.

#Demo

Introduce blog

Docs for old version 2.x.x

There are some breaking changes from version 3, so if you are using the old version, please follow this

Install

With npm

npm install --save react-images-uploading

With yarn

yarn add react-images-uploading

Basic Usage

import * as React from "react";

import ImageUploading from "react-images-uploading";
// { ImageUploadingPropsType, ImageListType, ImageType } is type for typescript

const maxNumber = 10;
const maxMbFileSize = 5 * 1024 * 1024; // 5Mb

class Example extends React.Component {
  const [images, setImages] = useState([]);

  onChange = (imageList, addUpdateIndex) => {
    // data for submit
    setImages(imageList);
    console.log('index of new chosen images: ', addUpdateIndex)
  };
  onError = (errors, files) => {
    console.log(errors, files);
  };

  render() {
    return (
      <ImageUploading
        value={images}
        onChange={this.onChange}
        maxNumber={maxNumber}
        multiple
        maxFileSize={maxMbFileSize}
        acceptType={["jpg", "gif", "png"]}
        onError={this.onError}
        dataURLKey="data_url"
      >
        {({ imageList, onImageUpload, onImageRemoveAll, onImageUpdate, onImageRemove }) => (
          // write your building UI
          <div>
            <button onClick={onImageUpload}>Upload images</button>
            <button onClick={onImageRemoveAll}>Remove all images</button>

            {imageList.map((image, index) => (
              <div key={index}>
                <img src={image.data_url} />
                <button onClick={() => onImageUpdate(index)}>Update</button>
                <button onClick={() => onImageRemove(index)}>Remove</button>
              </div>
            ))}
          </div>
        )}
      </ImageUploading>
    );
  }
}

Validate

...
  {({ imageList, onImageUpload, onImageRemoveAll, errors }) => (
    <div>
      {errors.maxNumber && <span>Number of selected images exceed maxNumber</span>}
      {errors.acceptType && <span>Your selected file type is not allow</span>}
      {errors.maxFileSize && <span>Selected file size exceed maxFileSize</span>}
      {errors.resolution && <span>Selected file is not match your desired resolution</span>}
    </div>
  )}
...

Resolution

  • "absolute": resolutionWidth and resolutionHeight is equal with selected image absolutely
  • "ratio": resolutionWidth/resolutionHeight ratio is equal with width/height ratio of selected image
  • "less: image width must less than resolutionWidth and image height must less than resolutionHeight
  • "more: image width must more than resolutionWidth and image height must more than resolutionHeight

Drag and Drop

There are two props for this feature is dragProps and isDragging. Usage is very simple. Follow this:

<ImageUploading value={images} onChange={this.onChange} dataURLKey="data_url">
  {({ imageList, dragProps, isDragging }) => (
    // write your building UI
    <div {...dragProps}>
      {isDragging ? "Drop here please" : "Upload space"}
      {imageList.map((image, index) => (
        <img key={index} src={image.data_url} />
      ))}
    </div>
  )}
</ImageUploading>

Props

parametertypeoptionsdefaultdescription
valuearray[]Array of image list
onChangefunction(imageList, addUpdateindex) => voidCalled every add, update or delete action
dataURLKeystring"dataURL"Field name that base64 of selected image is assign to
multiplebooleanfalseSet true for multiple choose
maxNumbernumber1000Number of images user can select if mode = "multiple"
onErrorfunction(errors, files) => voidCalled if have error on validate each update
acceptTypearray['jpg', 'gif', 'png'][]Supported image extension
maxFileSizenumberMax image size(Byte) (will use in the image validation)
resolutionTypestring"absolute" | "less" | "more" | "ratio"Using for validate image with your width \- height resolution that you provide
resolutionWidthnumber> 0
resolutionHeightnumber> 0

Exported options

parametertypedescription
imageListarrayList of images for render.
onImageUploadfunctionCall for upload new image(s)
onImageRemoveAllfunctionCall for remove all image(s)
onImageUpdate(updateIndex: number) => voidCall for update at updateIndex.
onImageRemove(deleteIndex: number | Array) => voidCall for remove one/list image.
errorsobjectExport list of validation
dragPropsobjectNative element props for drag and drop feature
isDraggingboolean"true" if image(s) is dragged into drag and space.

License

MIT ©

Keywords

FAQs

Package last updated on 23 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc