Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 3c652ff

Browse files
committed
Watch FilePond value for manual changes
1 parent ff00c11 commit 3c652ff

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

docs/components/filepond.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,12 @@ If you're into using CDNs, you can add these lines to your layout file for the a
136136
<link href="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css" rel="stylesheet">
137137
<script src="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.js"></script>
138138
```
139+
140+
## Manually Deleting Files
141+
142+
If you decide to do something like list out the files and delete them yourself manually instead of the revert button provided by FilePond, you will
143+
find that the files still appear to be in FilePond, even though Livewire has actually cleared out your `wire:model` value. To help with this,
144+
the `file-pond` component adds a watcher on your `wire:model` by using `@entangle`. Now, when you remove the files manually, the component
145+
will pick up on those changes and remove the removed files from the FilePond instance.
146+
147+
You can opt out of this behavior by setting the boolean attribute `watch-value` to `false` on the component.

resources/views/components/files/file-pond.blade.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
11
<div wire:ignore
2-
x-data
2+
x-data="{ pond: null, @if ($shouldWatch($attributes)) value: @entangle($attributes->wire('model')), oldValue: undefined @endif }"
33
x-cloak
44
x-init="
55
{{ $plugins ?? '' }}
6-
FilePond.setOptions({
6+
7+
@if ($shouldWatch($attributes))
8+
$watch('value', value => {
9+
@if ($multiple)
10+
const removeOldFiles = (newValue, oldValue) => {
11+
if (newValue.length < oldValue.length) {
12+
const difference = oldValue.filter(i => ! newValue.includes(i));
13+
14+
difference.forEach(serverId => {
15+
const file = pond.getFiles().find(f => f.serverId === serverId);
16+
17+
file && pond.removeFile(file.id);
18+
});
19+
}
20+
};
21+
22+
if (this.oldValue !== undefined) {
23+
try {
24+
const files = Array.isArray(value) ? value : JSON.parse(String(value).split('livewire-files:')[1]);
25+
const oldFiles = Array.isArray(this.oldValue) ? this.oldValue : JSON.parse(String(this.oldValue).split('livewire-files:')[1]);
26+
27+
if (Array.isArray(files) && Array.isArray(oldFiles)) {
28+
removeOldFiles(files, oldFiles);
29+
}
30+
} catch (e) {}
31+
}
32+
33+
this.oldValue = value;
34+
@else
35+
! value && pond.removeFile()
36+
@endif
37+
});
38+
@endif
39+
40+
pond = FilePond.create($refs.input, {
741
{{ $jsonOptions() }}
842
{{-- Enhance for livewire support --}}
943
@if ($attributes->whereStartsWith('wire:model')->first())
1044
server: {
1145
process: (fieldName, file, metadata, load, error, progress, abort, transfer, options) => {
12-
@this.upload('{{ $attributes['wire:model'] }}', file, load, error, progress);
46+
@this.upload('{{ $attributes->wire('model')->value() }}', file, load, error, progress);
1347
},
1448
revert: (filename, load) => {
15-
@this.removeUpload('{{ $attributes['wire:model'] }}', filename, load);
49+
@this.removeUpload('{{ $attributes->wire('model')->value() }}', filename, load);
1650
},
1751
},
1852
@endif
1953
{{ $optionsSlot ?? '' }}
2054
});
21-
FilePond.create($refs.input);
2255
"
2356
>
2457
<input x-ref="input"

src/Components/Files/FilePond.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ class FilePond extends BladeComponent
2929
/** @var string|null */
3030
public $description;
3131

32+
/**
33+
* When set to true, the component will watch for changes to the wire:model
34+
* and manually remove the files from the FilePond instance if they are
35+
* still present.
36+
*
37+
* @var bool
38+
*/
39+
public bool $watchValue;
40+
3241
public function __construct(
3342
bool $multiple = false,
3443
bool $allowDrop = true,
@@ -38,6 +47,7 @@ public function __construct(
3847
int $maxFiles = null,
3948
string $type = null,
4049
string $description = null,
50+
bool $watchValue = true,
4151
bool $showErrors = true
4252
) {
4353
$this->multiple = $multiple;
@@ -49,6 +59,7 @@ public function __construct(
4959
$this->options = $options;
5060
$this->description = $description;
5161
$this->showErrors = $showErrors;
62+
$this->watchValue = $watchValue;
5263
}
5364

5465
public function options(): array
@@ -83,4 +94,10 @@ public function jsonOptions(): string
8394

8495
return '...' . json_encode((object) $this->options()) . ',';
8596
}
97+
98+
public function shouldWatch($attributes): bool
99+
{
100+
return $this->watchValue
101+
&& (bool) $attributes->whereStartsWith('wire:model')->first();
102+
}
86103
}

0 commit comments

Comments
 (0)