Skip to content

Commit

Permalink
Merge pull request #14 from Log1x/enhance/menu-panel-improvements
Browse files Browse the repository at this point in the history
✨ Add additional configuration for menu panels
  • Loading branch information
datlechin authored Aug 17, 2024
2 parents 439abe3 + 596b45a commit dee1785
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 4 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $panel

Menu panels are the panels that contain the menu items which you can add to the menus.

### Custom Menu Panel
#### Custom Menu Panel

By default, the package provides a **Custom Link** menu panel that allows you to add custom links to the menus.

Expand Down Expand Up @@ -147,7 +147,7 @@ $panel

![Static Menu Panel](./art/static-menu.png)

### Model Menu Panel
#### Model Menu Panel

The model menu panel allows you to add menu items from a model.

Expand Down Expand Up @@ -192,6 +192,32 @@ $panel

![Model Menu Panel](./art/model-menu.png)

#### Additional Menu Panel Options

When registering a menu panel, multiple methods are available allowing you to configure the panel's behavior such as collapse state and pagination.

```php
use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin;
use Datlechin\FilamentMenuBuilder\MenuPanel\StaticMenuPanel;

$panel
...
->plugin(
FilamentMenuBuilderPlugin::make()
->addMenuPanels([
StaticMenuPanel::make()
->addMany([
...
])
->description('Lorem ipsum...')
->icon('heroicon-m-link')
->collapsed(true)
->collapsible(true)
->paginate(perPage: 5, condition: true)
])
)
```

### Custom Fields

In some cases, you may want to extend menu and menu items with custom fields. To do this, start by passing an array of form components to the `addMenuFields` and `addMenuItemFields` methods when registering the plugin:
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/en/menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@
'heading' => 'No items found',
'description' => 'There are no items in this menu.',
],
'pagination' => [
'previous' => 'Previous',
'next' => 'Next',
],
],
];
4 changes: 4 additions & 0 deletions resources/lang/vi/menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@
'heading' => 'Không tìm thấy mục nào',
'description' => 'Không có mục nào trong menu này.',
],
'pagination' => [
'previous' => 'Trước',
'next' => 'Tiếp',
],
],
];
31 changes: 30 additions & 1 deletion resources/views/livewire/panel.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
<form wire:submit="add">
<x-filament::section
:heading="$name"
:collapsible="true"
:description="$description"
:icon="$icon"
:collapsible="$collapsible"
:collapsed="$collapsed"
:persist-collapsed="true"
id="{{ $id }}-panel"
>
{{ $this->form }}

@if ($this->hasPages())
<div class="flex items-center justify-between mt-4">
@if ($this->hasPreviousPage())
<x-filament::link
tag="button"
wire:click="previousPage"
icon="heroicon-m-chevron-left"
>
{{ __('filament-menu-builder::menu-builder.panel.pagination.previous') }}
</x-filament::link>
@endif

@if ($this->hasNextPage())
<x-filament::link
class="ml-auto"
tag="button"
wire:click="nextPage"
icon="heroicon-m-chevron-right"
iconPosition="after"
>
{{ __('filament-menu-builder::menu-builder.panel.pagination.next') }}
</x-filament::link>
@endif
</div>
@endif

@if ($this->items)
<x-slot:footerActions>
<x-filament::button type="submit">
Expand Down
60 changes: 59 additions & 1 deletion src/Livewire/MenuPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ class MenuPanel extends Component implements HasForms

public string $name;

public ?string $description;

public ?string $icon;

public bool $collapsible;

public bool $collapsed;

public bool $paginated;

public int $perPage;

public int $page = 1;

public array $items = [];

#[Validate('required|array')]
Expand All @@ -34,6 +48,12 @@ public function mount(ContractsMenuPanel $menuPanel): void
{
$this->id = $menuPanel->getIdentifier();
$this->name = $menuPanel->getName();
$this->description = $menuPanel->getDescription();
$this->icon = $menuPanel->getIcon();
$this->collapsible = $menuPanel->isCollapsible();
$this->collapsed = $menuPanel->isCollapsed();
$this->paginated = $menuPanel->isPaginated();
$this->perPage = $menuPanel->getPerPage();
$this->items = array_map(function ($item) {
if (isset($item['url']) && is_callable($item['url'])) {
$item['url'] = $item['url']();
Expand All @@ -43,6 +63,13 @@ public function mount(ContractsMenuPanel $menuPanel): void
}, $menuPanel->getItems());
}

public function getItems(): array
{
return $this->paginated
? collect($this->items)->forPage($this->page, $this->perPage)->all()
: $this->items;
}

public function add(): void
{
$this->validate();
Expand Down Expand Up @@ -75,7 +102,7 @@ public function add(): void

public function form(Form $form): Form
{
$items = collect($this->items)->mapWithKeys(fn ($item) => [$item['title'] => $item['title']]);
$items = collect($this->getItems())->mapWithKeys(fn ($item) => [$item['title'] => $item['title']]);

return $form
->schema([
Expand All @@ -86,11 +113,42 @@ public function form(Form $form): Form
->hiddenLabel()
->required()
->bulkToggleable()
->live(condition: $this->paginated)
->visible($items->isNotEmpty())
->options($items),
]);
}

public function getTotalPages(): int
{
return (int) ceil(count($this->items) / $this->perPage);
}

public function nextPage(): void
{
$this->page = min($this->getTotalPages(), $this->page + 1);
}

public function previousPage(): void
{
$this->page = max(1, $this->page - 1);
}

public function hasNextPage(): bool
{
return $this->page < $this->getTotalPages();
}

public function hasPreviousPage(): bool
{
return $this->page > 1;
}

public function hasPages(): bool
{
return $this->paginated && $this->getTotalPages() > 1;
}

public function render(): View
{
return view('filament-menu-builder::livewire.panel');
Expand Down
78 changes: 78 additions & 0 deletions src/MenuPanel/AbstractMenuPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ abstract class AbstractMenuPanel implements MenuPanel

protected int $sort = 999;

protected ?string $description = null;

protected ?string $icon = null;

protected bool $collapsible = true;

protected bool $collapsed = false;

protected bool $paginated = false;

protected int $perPage = 5;

public function __construct(string $name)
{
$this->name = $name;
Expand Down Expand Up @@ -40,4 +52,70 @@ public function getSort(): int
{
return $this->sort;
}

public function description(string $description): static
{
$this->description = $description;

return $this;
}

public function getDescription(): ?string
{
return $this->description;
}

public function icon(string $icon): static
{
$this->icon = $icon;

return $this;
}

public function getIcon(): ?string
{
return $this->icon;
}

public function collapsible(bool $collapsible = true): static
{
$this->collapsible = $collapsible;

return $this;
}

public function isCollapsible(): bool
{
return $this->collapsible;
}

public function collapsed(bool $collapsed = true): static
{
$this->collapsed = $collapsed;

return $this;
}

public function isCollapsed(): bool
{
return $this->collapsed;
}

public function paginate(int $perPage = 5, bool $condition = true): static
{
$this->perPage = $perPage;
$this->paginated = $condition;

return $this;
}

public function isPaginated(): bool
{
return $this->paginated;
}

public function getPerPage(): int
{
return $this->perPage;
}
}

0 comments on commit dee1785

Please sign in to comment.