Skip to content

Commit

Permalink
Add file-pond-clear event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Oct 15, 2020
1 parent 5383321 commit 9a4397a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/components/filepond.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ the `file-pond` component adds a watcher on your `wire:model` by using `@entangl
will pick up on those changes and remove the removed files from the FilePond instance.

You can opt out of this behavior by setting the boolean attribute `watch-value` to `false` on the component.

## Clear Event Listener

You may encounter some edge cases where you may need to clear the files out of the FilePond instance
yourself. When using livewire, this can easily be accomplished by adding the following inside your
component somewhere:

```php
$this->dispatchBrowserEvent('file-pond-clear', ['id' => $this->id]);
```

Each Livewire component has its own unique id assigned to it, so by passing `$this->id` into the
event you're emitting, the component will be able to match the ids and clear the files for the correct
component.
14 changes: 13 additions & 1 deletion resources/views/components/files/file-pond.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<div wire:ignore
x-data="{ pond: null, @if ($shouldWatch($attributes)) value: @entangle($attributes->wire('model')), oldValue: undefined @endif }"
x-cloak
x-on:file-pond-clear.window="
const id = $wire && $wire.__instance.id;
const sentId = $event.detail.id;
if (id && (sentId !== id)) {
return;
}
@if ($multiple)
pond.getFiles().forEach(file => pond.removeFile(file.id));
@else
pond.removeFile();
@endif
"
x-init="
{{ $plugins ?? '' }}
Expand Down Expand Up @@ -32,7 +44,7 @@
this.oldValue = value;
@else
! value && pond.removeFile()
! value && pond.removeFile();
@endif
});
@endif
Expand Down

0 comments on commit 9a4397a

Please sign in to comment.