@osmandvc/react-upload-control 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
package/README.md
CHANGED
@@ -1,14 +1,37 @@
|
|
1
1
|
# React Upload Control
|
2
2
|
|
3
|
-
|
3
|
+
[![npm version](https://img.shields.io/npm/v/@osmandvc/react-upload-control.svg)](https://www.npmjs.com/package/@osmandvc/react-upload-control)
|
4
|
+
[![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
|
5
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
4
6
|
|
5
|
-
|
7
|
+
A modern, flexible file upload control for React applications.
|
6
8
|
|
7
9
|
React Upload Control is a free, lightweight and open-source file uploader library designed for modern React applications. This library is crafted to provide a high-quality developer experience (DX), making it easy to use for simple file uploads while offering extensive customization options for advanced use cases.
|
8
10
|
|
11
|
+
## Demo
|
12
|
+
|
13
|
+
Check out our interactive demo cases here:
|
14
|
+
[React Upload Control Demo](https://675c9582166050575d7b72e2-bzirycxazq.chromatic.com)
|
15
|
+
|
16
|
+
## Why React Upload Control? 🤷♂️
|
17
|
+
|
18
|
+
Born from real-world needs in a production environment, React Upload Control addresses common limitations found in existing file upload solutions. While there are many file upload libraries available, we found that most of them fell short in crucial areas:
|
19
|
+
|
20
|
+
- 🔄 **No Drag-to-Reorder:** Most solutions lack built-in file ordering capabilities, a crucial feature for many applications
|
21
|
+
- 📚 **Documentation Gaps:** Many libraries either have excessive boilerplate, insufficient documentation or lacking DX
|
22
|
+
- 🔧 **Maintenance Issues:** Several options are outdated or no longer actively maintained
|
23
|
+
- 🎨 **Poor UI/UX:** Many unstyled options result in subpar user interfaces
|
24
|
+
- 🔒 **Vendor Lock-in:** Some solutions tie you to specific cloud services or platforms
|
25
|
+
- 📦 **Bloated Dependencies:** Often file uploaders come bundled within larger UI libraries, increasing your bundle size
|
26
|
+
|
27
|
+
React Upload Control was created to solve these problems, offering a standalone, modern file uploader that's both powerful and flexible. Whether you need a simple file upload control or a feature-rich solution with file processing capabilities, you can customize it to your specific needs without compromising on quality or developer experience.
|
28
|
+
|
29
|
+
## Features summary 🔥
|
30
|
+
|
9
31
|
- 🚀 **Modern Stack:** Built with React 18+ and TypeScript for type-safe development
|
10
32
|
- 📁 **Drag & Drop:** Intuitive file uploading with visual feedback and validation
|
11
33
|
- 📋 **File Management:** Drag-to-reorder capability for organizing user uploads
|
34
|
+
- 🔄 **File Processing:** Optional pre/post-processing capabilities with [@osmandvc/react-upload-control-processors](https://www.npmjs.com/package/@osmandvc/react-upload-control-processors) or custom integrations
|
12
35
|
- 📷 **Camera Integration:** Camera integration for capturing photos directly
|
13
36
|
- 💻 **Developer Experience:** Simple API with comprehensive TypeScript support and documentation
|
14
37
|
- 🌐 **Internationalization:** Built-in i18n support for multiple languages (currently English and German)
|
@@ -19,11 +42,6 @@ React Upload Control is a free, lightweight and open-source file uploader librar
|
|
19
42
|
- ⚙️ **Unopinionated:** You decide how and where files are uploaded, no vendor lock-in
|
20
43
|
- 🔓 **Open Source:** Free to use and modify under the MIT license
|
21
44
|
|
22
|
-
## Demo
|
23
|
-
|
24
|
-
Check out our interactive demo cases here:
|
25
|
-
[React Upload Control Demo](https://675c9582166050575d7b72e2-kpevajcgoj.chromatic.com/)
|
26
|
-
|
27
45
|
## Installation
|
28
46
|
|
29
47
|
To install React Upload Control, use npm or yarn:
|
@@ -83,8 +101,6 @@ The upload handler receives two parameters:
|
|
83
101
|
1. `files`: An array of `UploadedFile` objects to be uploaded
|
84
102
|
2. `onProgressChange`: A callback function to update the upload progress
|
85
103
|
|
86
|
-
The upload handler should return an array of `UploadFileResult` objects, which specify the success, error, and metadata for each file upload. The library will handle the UI updates based on the progress updates and final results you provide.
|
87
|
-
|
88
104
|
Here are two examples of implementing an upload handler:
|
89
105
|
|
90
106
|
### Example 1: Simple Batch Upload
|
@@ -281,6 +297,58 @@ function MyFileUpload() {
|
|
281
297
|
}
|
282
298
|
```
|
283
299
|
|
300
|
+
### Understanding UploadFileResult
|
301
|
+
|
302
|
+
The upload handler must return an array of `UploadFileResult` objects - one for each uploaded file. Here's the structure:
|
303
|
+
|
304
|
+
```typescript
|
305
|
+
type UploadFileResult = {
|
306
|
+
// The ID of the file that was uploaded (must match the original file.id)
|
307
|
+
fileId: string;
|
308
|
+
|
309
|
+
// Whether the upload was successful
|
310
|
+
success: boolean;
|
311
|
+
|
312
|
+
// Optional error information if success is false
|
313
|
+
error?: {
|
314
|
+
text: string; // Human-readable error message
|
315
|
+
code: string; // Error code for programmatic handling
|
316
|
+
};
|
317
|
+
|
318
|
+
// Optional metadata to attach to the file
|
319
|
+
metadata?: {
|
320
|
+
[key: string]: any; // Any additional data you want to store with the file
|
321
|
+
};
|
322
|
+
};
|
323
|
+
```
|
324
|
+
|
325
|
+
Example of a successful upload result:
|
326
|
+
|
327
|
+
```typescript
|
328
|
+
{
|
329
|
+
fileId: "file123",
|
330
|
+
success: true,
|
331
|
+
metadata: {
|
332
|
+
url: "https://example.com/uploads/file123.jpg",
|
333
|
+
uploadedAt: "2025-01-02T12:00:00Z",
|
334
|
+
size: 1024000
|
335
|
+
}
|
336
|
+
}
|
337
|
+
```
|
338
|
+
|
339
|
+
Example of a failed upload result:
|
340
|
+
|
341
|
+
```typescript
|
342
|
+
{
|
343
|
+
fileId: "file456",
|
344
|
+
success: false,
|
345
|
+
error: {
|
346
|
+
text: "File size exceeds server limit",
|
347
|
+
code: "SIZE_LIMIT_EXCEEDED"
|
348
|
+
}
|
349
|
+
}
|
350
|
+
```
|
351
|
+
|
284
352
|
## onDelete Handler
|
285
353
|
|
286
354
|
The `onDelete` handler is designed to be non-blocking and does not include progress tracking. This is intentional for better UX - since it's primarily used for cleanup when resetting a control with already finished uploads. Users should be able to reset the control immediately without waiting for deletion to complete or being blocked by deletion errors.
|
@@ -351,41 +419,50 @@ The provider component that manages the upload state and configuration.
|
|
351
419
|
| disableFileSystem | boolean | false | Disable file system uploads |
|
352
420
|
| className | string | undefined | Additional CSS classes |
|
353
421
|
|
354
|
-
##
|
422
|
+
## File Processing
|
355
423
|
|
356
|
-
|
424
|
+
React Upload Control supports file processing through the optional [@osmandvc/react-upload-control-processors](https://www.npmjs.com/package/@osmandvc/react-upload-control-processors) package. This enables you to process files before or after upload, with built-in support for PDF manipulation and extensible architecture for custom processors.
|
357
425
|
|
358
|
-
```
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
426
|
+
```bash
|
427
|
+
npm install @osmandvc/react-upload-control-processors
|
428
|
+
```
|
429
|
+
|
430
|
+
### Example: PDF to JPEG Conversion
|
431
|
+
|
432
|
+
```tsx
|
433
|
+
import {
|
434
|
+
FileUploadControl,
|
435
|
+
UploadedFile,
|
436
|
+
UploadedFilesProvider,
|
437
|
+
UploadFileResult,
|
438
|
+
} from "@osmandvc/react-upload-control";
|
439
|
+
import { processPdfToJpeg } from "@osmandvc/react-upload-control-processors";
|
440
|
+
|
441
|
+
function FileUploadTest() {
|
442
|
+
return (
|
443
|
+
<div className="max-w-lg">
|
444
|
+
<UploadedFilesProvider
|
445
|
+
config={{
|
446
|
+
mimeTypes: ["image/png", "image/jpeg", "application/pdf"],
|
447
|
+
disableSorting: true,
|
448
|
+
}}
|
449
|
+
handlers={{
|
450
|
+
onUpload: handleUpload,
|
451
|
+
onFinish: handleFinish,
|
452
|
+
preProcessFiles: {
|
453
|
+
"application/pdf": processPdfToJpeg,
|
454
|
+
},
|
455
|
+
}}
|
456
|
+
>
|
457
|
+
<FileUploadControl />
|
458
|
+
</UploadedFilesProvider>
|
459
|
+
</div>
|
460
|
+
);
|
386
461
|
}
|
387
462
|
```
|
388
463
|
|
464
|
+
You can see a demo of this in action [here](https://675c9582166050575d7b72e2-bzirycxazq.chromatic.com/?path=/story/examples-upload-control-with-pdf-pre-processing--default).
|
465
|
+
|
389
466
|
## Creating Custom Upload Sources and Destinations
|
390
467
|
|
391
468
|
The `useUploadFilesProvider` hook allows you to create your own file sources (like drop areas) and file destinations (like file lists) with ease. The provider handles all the complex validation and state management for you.
|