Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sortBy Navigation Feature #39

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}