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..12cc1e5 100644 --- a/resources/views/components/create-menu.blade.php +++ b/resources/views/components/create-menu.blade.php @@ -1,41 +1,63 @@ -
-@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..a743a3e 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,7 @@ public function mount(): void $this->rounded = QuickCreatePlugin::get()->isRounded(); $this->hiddenIcons = QuickCreatePlugin::get()->shouldHideIcons(); $this->label = QuickCreatePlugin::get()->getLabel(); + $this->keyBindings = QuickCreatePlugin::get()->getKeyBindings(); } /** @@ -146,6 +149,11 @@ public function getActions(): array ->toArray(); } + public function toggleDropdown(): void + { + $this->emit('toggleQuickCreateDropdown'); + } + public function shouldBeHidden(): bool { return QuickCreatePlugin::get()->shouldBeHidden(); @@ -153,6 +161,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(); + } }