Skip to content

Commit 23f23fe

Browse files
authored
Merge pull request #4 from TappNetwork/add_support_to_multiple_uppy_instances
Add support to multiple Uppy instances
2 parents d3dbb41 + 6a9d6b4 commit 23f23fe

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,43 @@ Default drag & drop options if none is informed:
362362
}
363363
```
364364

365+
**Upload Element Class**
366+
367+
Use the `uploadElementClass` attribute to provide the class of the HTML element used for upload:
368+
369+
```php
370+
$imageClass = 'images';
371+
```
372+
373+
```html
374+
<x-input.uppy :uploadElementClass="$imageClass" />
375+
```
376+
377+
The `upload` class will be used if none is provided.
378+
379+
**Multiple Uppy Instances**
380+
381+
If you want to use multiple Uppy instances, add a different `uploadElementClass` attribute to each instance. E.g.:
382+
383+
```html
384+
<!-- First Uppy instance for image uploads -->
385+
<div>
386+
<input type="hidden" name="images" id="images" />
387+
<x-input.uppy :options="$imageOptions" :hiddenField="$imageField" :uploadElementClass="$imageClass" />
388+
</div>
389+
390+
391+
<!-- Second Uppy instance for video uploads -->
392+
<div>
393+
<input type="hidden" name="videos" id="videos" />
394+
<x-input.uppy :options="$videoOptions" :hiddenField="$videoField" :uploadElementClass="$videoClass" />
395+
</div>
396+
```
397+
398+
**Note from Uppy docs**: _"If multiple Uppy instances are being used, for instance, on two different pages,
399+
an id should be specified. This allows Uppy to store information in localStorage without colliding with other Uppy instances."_
400+
[Learn more here](https://uppy.io/docs/uppy/#id-39-uppy-39).
401+
365402
**Extra JavaScript to onUploadSuccess**
366403

367404
If you need to add extra JavaScript code on `onUploadSuccess` function, use the `extraJSForOnUploadSuccess` attribute:

resources/views/components/input/uppy.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
{{ $extraJSForOnUploadSuccess }}
2525
};
2626
27-
uppyUpload = new Uppy({{ $options }});
27+
uppyUpload{{ $hiddenField }} = new Uppy({{ $options }});
2828
29-
uppyUpload
29+
uppyUpload{{ $hiddenField }}
3030
.use(DragDrop, {{ $dragDropOptions }})
3131
.use(AwsS3Multipart, {
3232
companionUrl: '/',
@@ -36,10 +36,10 @@
3636
},
3737
})
3838
.use(StatusBar, {{ $statusBarOptions }})
39-
.on('upload-success', onUploadSuccess('.upload .uploaded-files ol'));
39+
.on('upload-success', onUploadSuccess('.{{ $uploadElementClass }} .uploaded-files ol'));
4040
"
4141
>
42-
<section class="upload">
42+
<section class="{{ $uploadElementClass }}">
4343
<div class="for-DragDrop" x-ref="input"></div>
4444

4545
<div class="for-ProgressBar"></div>

src/View/Components/Input/Uppy.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ class Uppy extends Component
1616

1717
public string $extraJSForOnUploadSuccess;
1818

19+
public string $uploadElementClass;
20+
1921
/**
2022
* Create a new component instance.
2123
*
2224
* @return void
2325
*/
24-
public function __construct(string $options = '', string $statusBarOptions = '', string $dragDropOptions = '', string $hiddenField = '', $extraJSForOnUploadSuccess = '')
26+
public function __construct(string $options = '', string $statusBarOptions = '', string $dragDropOptions = '', string $hiddenField = 'file', string $extraJSForOnUploadSuccess = '', string $uploadElementClass = 'upload')
2527
{
2628
$this->options = $options;
2729
$this->statusBarOptions = $statusBarOptions;
2830
$this->dragDropOptions = $dragDropOptions;
2931
$this->hiddenField = $hiddenField;
3032
$this->extraJSForOnUploadSuccess = $extraJSForOnUploadSuccess;
33+
$this->uploadElementClass = $uploadElementClass;
3134

3235
if (!$options) {
3336
$this->options = '{
@@ -39,20 +42,16 @@ public function __construct(string $options = '', string $statusBarOptions = '',
3942

4043
if (!$statusBarOptions) {
4144
$this->statusBarOptions = "{
42-
target: '.upload .for-ProgressBar',
45+
target: '.{$uploadElementClass} .for-ProgressBar',
4346
hideAfterFinish: false,
4447
}";
4548
}
4649

4750
if (!$dragDropOptions) {
4851
$this->dragDropOptions = "{
49-
target: '.upload .for-DragDrop',
52+
target: '.{$uploadElementClass} .for-DragDrop',
5053
}";
5154
}
52-
53-
if (!$hiddenField) {
54-
$this->hiddenField = 'file';
55-
}
5655
}
5756

5857
/**

0 commit comments

Comments
 (0)