diff --git a/README.md b/README.md index 650857d..e651974 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ public function panel(Panel $panel): Panel ### Sorting -By default, Quick Create will sort all the displayed options in descending order. This can be disabled should you choose. In which case they will be displayed in the order they are registered with Filament. +By default, Quick Create will sort all the displayed options in descending order by Label. This can be disabled should you choose. In which case they will be displayed in the order they are registered with Filament. ```php use Awcodes\FilamentQuickCreate\QuickCreatePlugin; @@ -101,6 +101,23 @@ public function panel(Panel $panel): Panel } ``` +### Sorting by resource navigation + +By default, Quick Create will sort all the displayed options by Label. This can be changed to resource navigation sort should you choose. In which case they will be displayed in the order they are displayed in the navigation. + +```php +use Awcodes\FilamentQuickCreate\QuickCreatePlugin; + +public function panel(Panel $panel): Panel +{ + return $panel + ->plugins([ + QuickCreatePlugin::make() + ->sortBy('navigation'), + ]) +} +``` + ### Slide Overs By default, Quick Create will render simple resources in a standard modal. If you would like to render them in a slide over instead you may use the `slideOver()` modifier to do so. diff --git a/src/QuickCreatePlugin.php b/src/QuickCreatePlugin.php index b7d710f..9b5e8fe 100644 --- a/src/QuickCreatePlugin.php +++ b/src/QuickCreatePlugin.php @@ -25,6 +25,8 @@ class QuickCreatePlugin implements Plugin protected bool | Closure | null $shouldUseSlideOver = null; + protected string | Closure $sortBy = 'label'; + public function boot(Panel $panel): void { Livewire::component('quick-create-menu', Components\QuickCreateMenu::class); @@ -94,12 +96,13 @@ public function getResources(): array 'action_name' => $actionName, 'action' => ! $resource->hasPage('create') ? 'mountAction(\'' . $actionName . '\')' : null, 'url' => $resource->hasPage('create') ? $resource::getUrl('create') : null, + 'navigation' => $resource->getNavigationSort() ]; } return null; }) - ->when($this->isSortable(), fn ($collection) => $collection->sortBy('label')) + ->when($this->isSortable(), fn ($collection) => $collection->sortBy($this->sortBy)) ->values() ->toArray(); @@ -150,4 +153,15 @@ public function sort(bool | Closure $condition = true): static return $this; } + + public function sortBy(string | Closure $sortBy = 'label'): static + { + if(!in_array($sortBy, ['label','navigation'])){ + $sortBy = 'label'; + } + + $this->sortBy = $sortBy; + + return $this; + } }