Skip to content

Commit

Permalink
Merge pull request #39 from sitenzo/sitenzo-add-sort-by-navigation
Browse files Browse the repository at this point in the history
Add sortBy Navigation Feature
  • Loading branch information
awcodes authored Dec 1, 2023
2 parents 4a27033 + 0861dad commit 954eccf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion src/QuickCreatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 954eccf

Please sign in to comment.