0.5.2 • Published 1 year ago

react-mui-fileuploader v0.5.2

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

React mui fileuploader is a React component based on @mui v5 that allows you to upload files with an awesome ui component.

DEMO

🚀 Installation

  npm install react-mui-fileuploader

💻 Usage

const handleFileUploadError = (error) => {
  // Do something...
}

const handleFilesChange = (files) => {
  // Do something...
  setUploadedFiles([...files]);
}

return (
  <FileUpload 
    getBase64={false}
    multiFile={true}
    disabled={false}
    title="My awesome file uploader"
    header="[Drag to drop]"
    leftLabel="or"
    rightLabel="to select files"
    buttonLabel="click here"
    buttonRemoveLabel="Remove all"
    maxFileSize={10}
    maxUploadFiles={0}
    maxFilesContainerHeight={357}
    acceptedType={'image/*'}
    errorSizeMessage={'fill it or remove it to use the default error message'}
    allowedExtensions={['jpg', 'jpeg']}
    onFilesChange={handleFilesChange}
    onError={handleFileUploadError}
    imageSrc={'path/to/custom/image'}
    BannerProps={{ elevation: 0, variant: "outlined" }}
    showPlaceholderImage={true}
    PlaceholderGridProps={{ md: 4 }}
    LabelsGridProps={{ md: 8 }}
    onContextReady={context => {
      // access to component context here
    }}
    ContainerProps={{
      elevation: 0,
      variant: "outlined",
      sx: { p: 1 }
    }}
    PlaceholderImageDimension={{
      xs: { width: 128, height: 128 },
      sm: { width: 128, height: 128 },
      md: { width: 164, height: 164 },
      lg: { width: 256, height: 256 }
    }}
  />
)

🎨 Possible application

import React, { useState } from "react"
import { createRoot } from "react-dom/client"
import FileUpload from "react-mui-fileuploader"

function MuiFileUploader() {
  const [filesToUpload, setFilesToUpload] = useState([])

  const handleFilesChange = (files) => {
    // Update chosen files
    setFilesToUpload([ ...files ])
  };

  const uploadFiles = () => {
    // Create a form and post it to server
    let formData = new FormData()
    filesToUpload.forEach((file) => formData.append("files", file))

    fetch("/file/upload", {
      method: "POST",
      body: formData
    })
  }

  return (
    <>
      <FileUpload
        multiFile={true}
        onFilesChange={handleFilesChange}
        onContextReady={(context) => {}}
      />
      <button onClick={uploadFiles}>Upload</button>
    </>
  )
}

const root = createRoot(document.getElementById("root"))
root.render(<MuiFileUploader />)

Edit react-mui-fileuploader example

Data structure

NameTypeRequiredDetails
getBase64booleanfalseGet the original input files. Default value false
multiFilebooleanfalseMultifile support. Default value true
titlestringfalseComponent title
headerstringfalseBanner component big title
showPlaceholderImagebooleanfalseShow or hide placeholder image
imageSrcstringfalseBanner image placeholder source path
imageSrcAltstringfalseBanner image placeholder label
leftLabelstringfalseBanner left label
rightLabelstringfalseBanner right label
buttonLabelstringfalseBanner button label
buttonRemoveLabelstringfalseRemove button label
disabledbooleanfalseThis property enables or disables the component. Default value false
maxFileSizenumberfalseMaximum size (in mb) for files to add. Default value 0. Value 0 means unlimited size
maxUploadFilesnumberfalseMaximum files to add. Default to 0. Value 0 means unlimited size
errorSizeMessagestringfalseError returned when a file exceeds maxFileSize limit
acceptedTypestringfalseAccepted file type. Default value image/*
allowedExtensionsarrayfalseArray of allowed extensions. For example, you can specify ['jpg', 'jpeg', 'png']as allowedExtensions
filesContainerHeightnumberfalseContainer Height
maxFilesContainerHeightnumberfalseContainer max height. Default value 300
onErrorfunctionfalseReturned error message when error occurs
onFilesChangefunctionfalseEvent handler returned when files changes
onContextReadyfunctionfalseReturns the component context api
BannerPropsobjectfalseBanner props. Only MUI props are accepted
ContainerPropsobjectfalseContainer props. Only MUI props are accepted
PlaceholderImageDimensionobjectfalseDimensions (width and height) of the placeholder image. You can specify them in the properties xs: {width: 64, height: 64}, sm: {width: 64, height: 64}, md: {width: 64, height: 64}, lg: {width: 64, height: 64}, etc.
PlaceholderGridPropsobjectfalseCustomize the placeholder Grid xs, sm, md, lg, xl sizes
LabelsGridPropsobjectfalseCustomize the labels Grid xs, sm, md, lg, xl sizes

😁 Authors

🤔 FAQ

  • Where can I find more documentation?

    This library is a marriage of @mui and a React setup created with React. Either one would be a great place to start!

🙇‍♂️ Extra

Do you like this library ? Buy me a coffee or support me with a star on Github

  • Btc address: bc1qettgagenn9nc8ks7ghntjfme96yvvkfhntk774

  • Eth address: 0xB0413d8D0336E263e289A915c383e152155881E0

License

react-mui-fileuploader

MIT License

Copyright (c) 2023 rouftom

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.3.0

2 years ago

0.5.0

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.2.0

2 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago