Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource picker #14

Merged
merged 9 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"php": "^8.2",
"blade-ui-kit/blade-icons": "^1.5",
"codedor/filament-translatable-tabs": "^0.1",
"codedor/filament-resource-picker": "dev-master as 0.1",
"filament/filament": "^3.0",
"illuminate/contracts": "^10.0",
"livewire/livewire": "^3.0",
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/picture.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
:class="$class"
:alt="$alt"
/>
@elseif (! $formats && ! $formats)
@elseif ($format && ! $formats)
<img
alt="{{ $alt }}"
title="{{ $title }}"
@class([
$class ?? 'img-fluid',
'lazyload' => $lazyload,
])
src="{{ $image->getFormatOrOriginal('thumbnail') }}"
src="{{ $image->getFormatOrOriginal($format) }}"
>
@elseif ($image)
<picture class="{{ $pictureClass }}">
Expand Down
12 changes: 12 additions & 0 deletions resources/views/components/resource-picker/item.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<label for="resource-picker::{{ $statePath }}-{{ $item->{$keyField} }}">
<div style="display: none">
{{ $slot }}
</div>

<div :class="{
'transition-all cursor-pointer': true,
'rounded-lg p-1 border-2 border-primary-600': state.includes('{{ $item->{$keyField} }}')
}">
<x-filament-media-library::attachment :attachment="$item" />
</div>
</label>
10 changes: 9 additions & 1 deletion resources/views/filament/attachment-input.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
x-on:picked-resource.window="(event) => {
// Set the state only if the event is for this resource picker
if (event.detail.statePath !== '{{ $getStatePath() }}') return
$wire.$set(event.detail.statePath, event.detail.resources)
}"
>
@php
$attachments = $getAttachments();
@endphp
Expand Down
29 changes: 28 additions & 1 deletion src/Filament/AttachmentInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ protected function setUp(): void
$component->getRelationship()->sync($state->toArray());
});

$this->dehydrateStateUsing(function (self $component, $state) {
if ($component->isMultiple()) {
return;
}

if (is_string($state)) {
return $state;
}

return $state[0] ?? null;
});

$this->registerActions([
Action::make('remove-attachment')
->icon('heroicon-o-x-circle')
Expand Down Expand Up @@ -101,7 +113,22 @@ protected function setUp(): void

Action::make('attachment-picker')
->label(__('filament-media-library::picker.select existing media'))
->color('gray'),
->modalHeading(__('filament-media-library::picker.select existing media'))
->color('gray')
->modalSubmitAction(false)
->modalCancelAction(false)
->modalContent(function (self $component) {
return view('filament-resource-picker::picker', [
'resourceClass' => AttachmentResource::class,
'displayType' => 'filament-media-library::resource-picker.item',
'statePath' => $component->getStatePath(),
'state' => $component->getState() ?? [],
'keyField' => 'id',
'labelField' => 'id',
'isMultiple' => $component->isMultiple(),
'isGrid' => true,
]);
}),
]);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Resources/AttachmentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Livewire\Component;
Expand Down Expand Up @@ -226,4 +227,13 @@ public static function getPages(): array
'edit' => Pages\EditAttachment::route('/{record}/edit'),
];
}

public static function resourcePickerQuery(Builder $query, string $search = null): Builder
{
return $query
->when(
$search,
fn () => $query->where('name', 'like', '%' . $search . '%')
);
}
}
Loading