Skip to content

Commit c634a21

Browse files
authored
Merge pull request #13 from mikocot/improved-readme
improve README.md to show a real usecase of the library
2 parents 371b2f4 + 8088d6d commit c634a21

File tree

1 file changed

+88
-50
lines changed

1 file changed

+88
-50
lines changed

README.md

Lines changed: 88 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,107 @@ React mui fileuploader is a React component based on @mui v5 that allows you to
1717
## [DEMO](https://eb6ie7.csb.app/)
1818

1919
## 🚀 Installation
20+
2021
```nodejs
2122
npm install react-mui-fileuploader
2223
```
2324

2425
## 💻 Usage
26+
27+
```javascript
28+
const handleFileUploadError = (error) => {
29+
// Do something...
30+
}
31+
32+
const handleFilesChange = (files) => {
33+
// Do something...
34+
setUploadedFiles([...files]);
35+
}
36+
37+
return (
38+
<FileUpload
39+
getBase64={false}
40+
multiFile={true}
41+
disabled={false}
42+
title="My awesome file uploader"
43+
header="[Drag to drop]"
44+
leftLabel="or"
45+
rightLabel="to select files"
46+
buttonLabel="click here"
47+
buttonRemoveLabel="Remove all"
48+
maxFileSize={10}
49+
maxUploadFiles={0}
50+
maxFilesContainerHeight={357}
51+
acceptedType={'image/*'}
52+
errorSizeMessage={'fill it or remove it to use the default error message'}
53+
allowedExtensions={['jpg', 'jpeg']}
54+
onFilesChange={handleFilesChange}
55+
onError={handleFileUploadError}
56+
imageSrc={'path/to/custom/image'}
57+
BannerProps={{ elevation: 0, variant: "outlined" }}
58+
onContextReady={context => {
59+
// access to component context here
60+
}}
61+
ContainerProps={{
62+
elevation: 0,
63+
variant: "outlined",
64+
sx: { p: 1 }
65+
}}
66+
PlaceholderImageDimension={{
67+
xs: { width: 128, height: 128 },
68+
sm: { width: 128, height: 128 },
69+
md: { width: 164, height: 164 },
70+
lg: { width: 256, height: 256 }
71+
}}
72+
/>
73+
)
74+
75+
```
76+
77+
## 🎨 Possible application
78+
2579
```javascript
26-
import React from 'react'
27-
import ReactDOM from 'react-dom'
28-
import FileUpload from "react-mui-fileuploader"
29-
30-
function Wrapper() {
31-
32-
const handleFileUploadError = (error) => {
33-
// Do something...
34-
}
35-
80+
import React, { useState } from "react";
81+
import { createRoot } from "react-dom/client";
82+
import FileUpload from "react-mui-fileuploader";
83+
84+
function MuiFileUploader() {
85+
const [filesToUpload, setFilesToUpload] = useState([]);
86+
3687
const handleFilesChange = (files) => {
37-
// Do something...
38-
}
88+
// Update chosen files
89+
setFilesToUpload([...files]);
90+
};
91+
92+
const uploadFiles = () => {
93+
// Create a form and post it to server
94+
let formData = new FormData();
95+
filesToUpload.forEach((file) => formData.append("files", file));
96+
97+
fetch("/file/upload", {
98+
method: "POST",
99+
body: formData
100+
});
101+
};
39102

40103
return (
41-
<FileUpload
42-
getBase64={false}
43-
multiFile={true}
44-
disabled={false}
45-
title="My awesome file uploader"
46-
header="[Drag to drop]"
47-
leftLabel="or"
48-
rightLabel="to select files"
49-
buttonLabel="click here"
50-
buttonRemoveLabel="Remove all"
51-
maxFileSize={10}
52-
maxUploadFiles={0}
53-
maxFilesContainerHeight={357}
54-
acceptedType={'image/*'}
55-
errorSizeMessage={'fill it or remove it to use the default error message'}
56-
allowedExtensions={['jpg', 'jpeg']}
57-
onFilesChange={handleFilesChange}
58-
onError={handleFileUploadError}
59-
imageSrc={'path/to/custom/image'}
60-
BannerProps={{ elevation: 0, variant: "outlined" }}
61-
onContextReady={context => {
62-
// access to component context here
63-
}}
64-
ContainerProps={{
65-
elevation: 0,
66-
variant: "outlined",
67-
sx: { p: 1 }
68-
}}
69-
PlaceholderImageDimension={{
70-
xs: { width: 128, height: 128 },
71-
sm: { width: 128, height: 128 },
72-
md: { width: 164, height: 164 },
73-
lg: { width: 256, height: 256 }
74-
}}
75-
/>
76-
)
104+
<>
105+
<FileUpload
106+
multiFile={true}
107+
onFilesChange={handleFilesChange}
108+
onContextReady={(context) => {}}
109+
/>
110+
<button onClick={uploadFiles}>Upload</button>
111+
</>
112+
);
77113
}
78114

79-
ReactDOM.render(<Wrapper />, document.querySelector('#root'))
80-
115+
const root = createRoot(document.getElementById("root"));
116+
root.render(<MuiFileUploader />);
81117
```
82118

119+
[![Edit react-mui-fileuploader example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/thirsty-visvesvaraya-r9u6ho?fontsize=14&hidenavigation=1&theme=dark)
120+
83121
## Data structure
84122

85123
| Name | Type | Required | Details |

0 commit comments

Comments
 (0)