Skip to content

Commit

Permalink
Merge pull request #41 from nicko170/3.x
Browse files Browse the repository at this point in the history
feat: add hidden() to plugin config
  • Loading branch information
awcodes authored Jan 16, 2024
2 parents 3fbc453 + ffee40f commit 6eb6b5f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ public function panel(Panel $panel): Panel
])
}
```

### Hiding Quick Create

By default, Quick Create is visible if there are registered resources. If you would like to hide it you may use the `hidden()` modifier to do so.

```php
use Awcodes\FilamentQuickCreate\QuickCreatePlugin;

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
QuickCreatePlugin::make()
->hidden(fn() => Filament::getTenant()->requiresOnboarding()),
])
}
```
2 changes: 1 addition & 1 deletion resources/views/components/create-menu.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="quick-create-component">
@if ($resources)
@if ($resources && $this->shouldBeHidden() === false)
<x-filament::dropdown placement="bottom-end">
<x-slot name="trigger">
<button
Expand Down
5 changes: 5 additions & 0 deletions src/Components/QuickCreateMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function getActions(): array
->toArray();
}

public function shouldBeHidden(): bool
{
return QuickCreatePlugin::get()->shouldBeHidden();
}

public function render(): View
{
return view('filament-quick-create::components.create-menu');
Expand Down
36 changes: 25 additions & 11 deletions src/QuickCreatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ class QuickCreatePlugin implements Plugin

protected bool $sort = true;

protected bool | Closure | null $shouldUseSlideOver = null;
protected bool|Closure|null $shouldUseSlideOver = null;

protected string | Closure $sortBy = 'label';
protected string|Closure $sortBy = 'label';

protected bool|Closure $hidden = false;

public function boot(Panel $panel): void
{
Livewire::component('quick-create-menu', Components\QuickCreateMenu::class);

$this->getResourcesUsing(fn () => $panel->getResources());
$this->getResourcesUsing(fn() => $panel->getResources());
}

public function excludes(array $resources): static
Expand Down Expand Up @@ -76,12 +78,12 @@ public function getResources(): array

$list = collect($resources)
->filter(function ($item) {
return ! in_array($item, $this->getExcludes());
return !in_array($item, $this->getExcludes());
})
->map(function ($resourceName): ?array {
$resource = app($resourceName);

if (Filament::hasTenancy() && ! Filament::getTenant()) {
if (Filament::hasTenancy() && !Filament::getTenant()) {
return null;
}

Expand All @@ -94,15 +96,15 @@ public function getResources(): array
'model' => $resource->getModel(),
'icon' => $resource->getNavigationIcon(),
'action_name' => $actionName,
'action' => ! $resource->hasPage('create') ? 'mountAction(\'' . $actionName . '\')' : null,
'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($this->sortBy))
->when($this->isSortable(), fn($collection) => $collection->sortBy($this->sortBy))
->values()
->toArray();

Expand Down Expand Up @@ -131,7 +133,7 @@ public function register(Panel $panel): void
$panel
->renderHook(
name: 'panels::user-menu.before',
hook: fn (): string => Blade::render('@livewire(\'quick-create-menu\')')
hook: fn(): string => Blade::render('@livewire(\'quick-create-menu\')')
);
}

Expand All @@ -147,21 +149,33 @@ public function slideOver(bool $condition = true): static
return $this;
}

public function sort(bool | Closure $condition = true): static
public function sort(bool|Closure $condition = true): static
{
$this->sort = $condition;

return $this;
}

public function sortBy(string | Closure $sortBy = 'label'): static
public function sortBy(string|Closure $sortBy = 'label'): static
{
if (! in_array($sortBy, ['label', 'navigation'])) {
if (!in_array($sortBy, ['label', 'navigation'])) {
$sortBy = 'label';
}

$this->sortBy = $sortBy;

return $this;
}

public function hidden(bool|Closure $hidden = true): static
{
$this->hidden = $hidden;

return $this;
}

public function shouldBeHidden(): bool
{
return $this->evaluate($this->hidden) ?? false;
}
}

0 comments on commit 6eb6b5f

Please sign in to comment.