Skip to content

Commit

Permalink
Merge pull request #49 from Senexis/feature-custom-text
Browse files Browse the repository at this point in the history
Add ability to add text only items. (#47)
  • Loading branch information
datlechin authored Nov 9, 2024
2 parents 46f7541 + c806a48 commit 73a77d7
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 7 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,44 @@ $panel

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

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

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

![Custom Link Menu Panel](https://github.com/datlechin/filament-menu-builder/raw/main/art/custom-link.png)

The panel can be disabled by using the following when configuring the plugin, should you not need this functionality.

```php
use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin;

$panel
...
->plugin(
FilamentMenuBuilderPlugin::make()
->showCustomLinkPanel(false)
)
```

#### Custom Text Menu Panel

This package provides a **Custom Text** menu panel that allows you to add custom text items to the menus.

It is identical to the **Custom Link** menu panel except for the fact that you only set a title without a URL or target. This can be useful to add headers to mega-style menus.

The panel is disabled by default to prevent visual clutter. To enable the Custom Text menu panel, you can use the following when configuring the plugin.

```php
use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin;

$panel
...
->plugin(
FilamentMenuBuilderPlugin::make()
->showCustomTextPanel()
)
```

#### Static Menu Panel

The static menu panel allows you to add menu items manually.
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
],
'custom_link' => 'Custom Link',
'custom_text' => 'Custom Text',
'open_in' => [
'label' => 'Open in',
'options' => [
Expand Down
1 change: 1 addition & 0 deletions resources/lang/fr/menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
],
'custom_link' => 'Lien personnalisé',
'custom_text' => 'Custom Text',
'open_in' => [
'label' => 'Ouvrir dans',
'options' => [
Expand Down
1 change: 1 addition & 0 deletions resources/lang/nl/menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
],
'custom_link' => 'Aangepaste link',
'custom_text' => 'Aangepaste tekst',
'open_in' => [
'label' => 'Openen op',
'options' => [
Expand Down
1 change: 1 addition & 0 deletions resources/lang/vi/menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
],
'custom_link' => 'Liên kết Tùy chỉnh',
'custom_text' => 'Custom Text',
'open_in' => [
'label' => 'Mở trong',
'options' => [
Expand Down
6 changes: 5 additions & 1 deletion resources/views/edit-record.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
<livewire:menu-builder-panel :menu="$record" :menuPanel="$menuPanel" />
@endforeach

@if (\Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin::get()->isShowCustomLink())
@if (\Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin::get()->isShowCustomLinkPanel())
<livewire:create-custom-link :menu="$record" />
@endif

@if (\Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin::get()->isShowCustomTextPanel())
<livewire:create-custom-text :menu="$record" />
@endif
</div>
<div class="col-span-12 sm:col-span-8">
<x-filament::section>
Expand Down
16 changes: 16 additions & 0 deletions resources/views/livewire/create-custom-text.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form wire:submit="save">
<x-filament::section
:heading="__('filament-menu-builder::menu-builder.custom_text')"
:collapsible="true"
:persist-collapsed="true"
id="create-custom-text"
>
{{ $this->form }}

<x-slot:footerActions>
<x-filament::button type="submit">
{{ __('filament-menu-builder::menu-builder.actions.add.label') }}
</x-filament::button>
</x-slot:footerActions>
</x-filament::section>
</form>
24 changes: 19 additions & 5 deletions src/FilamentMenuBuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class FilamentMenuBuilderPlugin implements Plugin
*/
protected array $menuPanels = [];

protected bool $showCustomLink = true;
protected bool $showCustomLinkPanel = true;

protected bool $showCustomTextPanel = false;

public function getId(): string
{
Expand Down Expand Up @@ -126,9 +128,16 @@ public function addMenuPanels(array $menuPanels): static
return $this;
}

public function showCustomLink(bool $show = true): static
public function showCustomLinkPanel(bool $show = true): static
{
$this->showCustomLink = $show;
$this->showCustomLinkPanel = $show;

return $this;
}

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

return $this;
}
Expand Down Expand Up @@ -177,9 +186,14 @@ public function getMenuPanels(): array
->all();
}

public function isShowCustomLink(): bool
public function isShowCustomLinkPanel(): bool
{
return $this->showCustomLinkPanel;
}

public function isShowCustomTextPanel(): bool
{
return $this->showCustomLink;
return $this->showCustomTextPanel;
}

public function getLocations(): array
Expand Down
2 changes: 2 additions & 0 deletions src/FilamentMenuBuilderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Datlechin\FilamentMenuBuilder;

use Datlechin\FilamentMenuBuilder\Livewire\CreateCustomLink;
use Datlechin\FilamentMenuBuilder\Livewire\CreateCustomText;
use Datlechin\FilamentMenuBuilder\Livewire\MenuItems;
use Datlechin\FilamentMenuBuilder\Livewire\MenuPanel;
use Filament\Support\Assets\AlpineComponent;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function packageBooted(): void
Livewire::component('menu-builder-items', MenuItems::class);
Livewire::component('menu-builder-panel', MenuPanel::class);
Livewire::component('create-custom-link', CreateCustomLink::class);
Livewire::component('create-custom-text', CreateCustomText::class);
}

protected function getAssetPackageName(): ?string
Expand Down
60 changes: 60 additions & 0 deletions src/Livewire/CreateCustomText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Datlechin\FilamentMenuBuilder\Livewire;

use Datlechin\FilamentMenuBuilder\Models\Menu;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Livewire\Component;

class CreateCustomText extends Component implements HasForms
{
use InteractsWithForms;

public Menu $menu;

public string $title = '';

public function save(): void
{
$this->validate([
'title' => ['required', 'string'],
]);

$this->menu
->menuItems()
->create([
'title' => $this->title,
'order' => $this->menu->menuItems->max('order') + 1,
]);

Notification::make()
->title(__('filament-menu-builder::menu-builder.notifications.created.title'))
->success()
->send();

$this->reset('title');
$this->dispatch('menu:created');
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')
->label(__('filament-menu-builder::menu-builder.form.title'))
->required(),
]);
}

public function render(): View
{
return view('filament-menu-builder::livewire.create-custom-text');
}
}
1 change: 1 addition & 0 deletions src/Models/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected function type(): Attribute
return Attribute::get(function () {
return match (true) {
$this->linkable instanceof MenuPanelable => $this->linkable->getMenuPanelName(),
is_null($this->linkable) && is_null($this->url) => __('filament-menu-builder::menu-builder.custom_text'),
default => __('filament-menu-builder::menu-builder.custom_link'),
};
});
Expand Down

0 comments on commit 73a77d7

Please sign in to comment.