Skip to content

Instantly share code, notes, and snippets.

@awcodes
Created May 2, 2023 13:09
Show Gist options
  • Save awcodes/2a0a97ce92b80789ab972765b701a509 to your computer and use it in GitHub Desktop.
Save awcodes/2a0a97ce92b80789ab972765b701a509 to your computer and use it in GitHub Desktop.
v3 component actions for Quick Create
<div>
@if ($resources)
<x-filament::dropdown placement="bottom-end">
<x-slot name="trigger" class="ms-4 rtl:me-4 rtl:ms-0">
<button
class="flex flex-shrink-0 w-10 h-10 rounded-full bg-gray-200 items-center justify-center dark:bg-gray-900"
aria-label="{{ __('Add a new resource') }}"
>
@svg('heroicon-o-plus', 'w-4 h-4')
</button>
</x-slot>
<x-filament::dropdown.list>
@foreach($resources as $resource)
{{ $this->createAction($resource['action_name']) }}
@endforeach
</x-filament::dropdown.list>
</x-filament::dropdown>
<x-filament-actions::modals />
@endif
</div>
<?php
namespace FilamentQuickCreate\Http\Livewire;
use Filament\Actions;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use FilamentQuickCreate\Facades\QuickCreate;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Livewire\Component;
class QuickCreateMenu extends Component implements HasActions
{
use InteractsWithActions;
public array $resources = [];
public function mount(): void
{
$this->resources = QuickCreate::getResources();
}
public function createAction(string $key): Actions\Action
{
$resourceInstance = collect($this->resources)->firstWhere('action_name', $key);
$resource = App::make($resourceInstance['resource_name']);
$action = Actions\CreateAction::make($key)
->label(Str::of($resource->getModelLabel())->ucfirst()->toString())
->extraAttributes(['class' => 'w-full'])
->color('gray')
->model($resource->getModel())
->icon($resource->getNavigationIcon());
if ($resource->hasPage('create')) {
$action->url(fn(): string => $resource->getUrl('create'));
} else {
$resourceList = App::make(invade($resource->getPages()['index'])->page);
$action->form(fn(Form $form): Form => $resourceList->form($form->columns(2)));
}
return $action;
}
public function render(): Application|Factory|View|\Illuminate\Foundation\Application
{
return view('filament-quick-create::components.create-menu');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment