From 8c6f9e29434a0b22bdb2fbc223649a52beda5731 Mon Sep 17 00:00:00 2001 From: John Trickett Date: Sat, 2 Nov 2024 17:38:33 +0000 Subject: [PATCH 1/3] added support for keybindings to open quick add dropdown --- README.md | 17 ++++ .../views/components/create-menu.blade.php | 86 +++++++++++-------- src/Components/QuickCreateMenu.php | 15 +++- src/QuickCreatePlugin.php | 16 +++- 4 files changed, 93 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 373b595..f8d723b 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,23 @@ public function panel(Panel $panel): Panel } ``` +### Registering keybindings + +You can attach keyboard shortcuts to trigger the Quick Create dropdown. To configure these, pass the keyBindings() method to the configuration: + +```php +use Awcodes\FilamentQuickCreate\QuickCreatePlugin; + +public function panel(Panel $panel): Panel +{ + return $panel + ->plugins([ + QuickCreatePlugin::make() + ->keyBindings(['command+shift+a', 'ctrl+shift+a']), + ]) +} +``` + ### Appearance #### Rounded diff --git a/resources/views/components/create-menu.blade.php b/resources/views/components/create-menu.blade.php index 2f692dc..552ff61 100644 --- a/resources/views/components/create-menu.blade.php +++ b/resources/views/components/create-menu.blade.php @@ -1,41 +1,51 @@ -
-@if ($resources && $this->shouldBeHidden() === false) - - - - - - @foreach($resources as $resource) - map(fn (string $keyBinding): string => str_replace('+', '-', $keyBinding)) + ->implode('.'); +@endphp + +
+ @if ($resources && $this->shouldBeHidden() === false) + + + + + + + @foreach($resources as $resource) + + {{ $resource['label'] }} + + @endforeach + + - -@endif + + @endif
diff --git a/src/Components/QuickCreateMenu.php b/src/Components/QuickCreateMenu.php index 4b3547c..0006fd8 100644 --- a/src/Components/QuickCreateMenu.php +++ b/src/Components/QuickCreateMenu.php @@ -33,6 +33,8 @@ class QuickCreateMenu extends Component implements HasActions, HasForms public ?string $label = null; + public ?array $keyBindings = null; + /** * @throws Exception */ @@ -42,6 +44,9 @@ public function mount(): void $this->rounded = QuickCreatePlugin::get()->isRounded(); $this->hiddenIcons = QuickCreatePlugin::get()->shouldHideIcons(); $this->label = QuickCreatePlugin::get()->getLabel(); + + // Retrieve keybindings from the plugin + $this->keyBindings = QuickCreatePlugin::get()->getKeyBindings(); } /** @@ -146,6 +151,11 @@ public function getActions(): array ->toArray(); } + public function toggleDropdown(): void + { + $this->emit('toggleQuickCreateDropdown'); + } + public function shouldBeHidden(): bool { return QuickCreatePlugin::get()->shouldBeHidden(); @@ -153,6 +163,9 @@ public function shouldBeHidden(): bool public function render(): View { - return view('filament-quick-create::components.create-menu'); + return view('filament-quick-create::components.create-menu') + ->with([ + 'keyBindings' => $this->keyBindings, + ]); } } diff --git a/src/QuickCreatePlugin.php b/src/QuickCreatePlugin.php index 0e8dbff..6af00b9 100644 --- a/src/QuickCreatePlugin.php +++ b/src/QuickCreatePlugin.php @@ -40,10 +40,11 @@ class QuickCreatePlugin implements Plugin protected bool | Closure $shouldUseModal = false; + protected string | array | Closure | null $keyBindings = null; + public function boot(Panel $panel): void { Livewire::component('quick-create-menu', Components\QuickCreateMenu::class); - $this->getResourcesUsing(fn () => $panel->getResources()); } @@ -184,7 +185,6 @@ public function sortBy(string | Closure $sortBy = 'label'): static if (! in_array($sortBy, ['label', 'navigation'])) { $sortBy = 'label'; } - $this->sortBy = $sortBy; return $this; @@ -249,4 +249,16 @@ public function alwaysShowModal(bool | Closure $condition = true): static return $this; } + + public function keyBindings(string | array | Closure | null $bindings): static + { + $this->keyBindings = $bindings; + + return $this; + } + + public function getKeyBindings(): ?array + { + return collect($this->evaluate($this->keyBindings))->toArray(); + } } From 80ec014b8fbaf44a430aba12ee6dc1d65ff5dde0 Mon Sep 17 00:00:00 2001 From: John Trickett Date: Sat, 2 Nov 2024 18:03:01 +0000 Subject: [PATCH 2/3] Update QuickCreateMenu.php --- src/Components/QuickCreateMenu.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Components/QuickCreateMenu.php b/src/Components/QuickCreateMenu.php index 0006fd8..a743a3e 100644 --- a/src/Components/QuickCreateMenu.php +++ b/src/Components/QuickCreateMenu.php @@ -44,8 +44,6 @@ public function mount(): void $this->rounded = QuickCreatePlugin::get()->isRounded(); $this->hiddenIcons = QuickCreatePlugin::get()->shouldHideIcons(); $this->label = QuickCreatePlugin::get()->getLabel(); - - // Retrieve keybindings from the plugin $this->keyBindings = QuickCreatePlugin::get()->getKeyBindings(); } From fd1d5b6fce67ef2dfe9fd4c43ee4f553d2cc044f Mon Sep 17 00:00:00 2001 From: John Trickett Date: Mon, 4 Nov 2024 15:15:50 +0000 Subject: [PATCH 3/3] conditional application in blade --- resources/views/components/create-menu.blade.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/views/components/create-menu.blade.php b/resources/views/components/create-menu.blade.php index 552ff61..12cc1e5 100644 --- a/resources/views/components/create-menu.blade.php +++ b/resources/views/components/create-menu.blade.php @@ -4,14 +4,26 @@ ->implode('.'); @endphp -
+
@if ($resources && $this->shouldBeHidden() === false)