3.0.0 • Published 2 years ago

@andfk/react-images-uploading v3.0.0

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

react-images-uploading

Temporary publish of package until PR

NPM JavaScript Style Guide

Images uploader

A simple images uploader without UI. Building by yourself.

#Demo

Introduce blog

Install

With npm

npm install --save react-images-uploading

With yarn

yarn add react-images-uploading

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 {
  onChange = (imageList) => {
    // data for submit
    console.log(imageList);
  };
  onError = (errors, files) => {
    console.log(errors, files);
  };

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

            {imageList.map((image) => (
              <div key={image.key}>
                <img src={image.dataURL} />
                <button onClick={image.onUpdate}>Update</button>
                <button onClick={image.onRemove}>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

Props

parametertypeoptionsdefaultdescription
multiplebooleanfalseSet true for multiple choose
maxNumbernumber1000Number of images user can select if mode = "multiple"
onChangefunctionCalled every update
onErrorfunction(errors, files) => voidCalled if have error on validate each update
defaultValuearray[{dataURL: ... }, ...]Init data
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. Each item in imageList has some options below.
imageListindex.keystringGenerate filename
imageListindex.dataURLstringUrl image or base64
imageListindex.onUpdatefunctionCall function for replace a new image on current position
imageListindex.onRemovefunctionCall function for remove current image
onImageUploadfunctionCall for upload new image(s)
onImageRemoveAllfunctionCall for remove all image(s)
errorsobjectExport type of validation

License

MIT ©