From f41719f1269504dd8a3b2c1ae1aaf5bb801221cf Mon Sep 17 00:00:00 2001 From: Alexander Shabunevich Date: Thu, 30 Nov 2023 20:02:50 +0300 Subject: [PATCH] Remove plugin and tabs component Using navigation link instead --- README.md | 90 +++++++++++----------- resources/views/components/tabs.blade.php | 21 ------ resources/views/page.blade.php | 9 +-- src/Components/TrilistPage.php | 37 +++++---- src/FilamentTrilistPlugin.php | 91 ----------------------- src/FilamentTrilistServiceProvider.php | 3 +- 6 files changed, 68 insertions(+), 183 deletions(-) delete mode 100644 resources/views/components/tabs.blade.php delete mode 100644 src/FilamentTrilistPlugin.php diff --git a/README.md b/README.md index 9a29b8e..a017da5 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ php artisan vendor:publish --tag="filament-trilist-views" Import `TrilistSelect` class and use it on your Filament form: -``` php +```php use Beholdr\FilamentTrilist\Components\TrilistSelect // with custom tree data (see below about tree data) @@ -49,7 +49,7 @@ TrilistSelect::make('category_id') Full options list: -``` php +```php TrilistSelect::make(string $fieldName) ->label(string $fieldLabel) ->placeholder(string | Closure $placeholder) @@ -105,7 +105,7 @@ TrilistSelect::make(string $fieldName) You can use treeselect in [custom filter](https://filamentphp.com/docs/3.x/tables/filters#custom-filter-forms): -``` php +```php use App\Models\Category; use Filament\Tables\Filters\Filter; use Illuminate\Database\Eloquent\Builder; @@ -130,28 +130,11 @@ Filter::make('category') ## Treeview page -![Treeview page](https://github.com/beholdr/filament-trilist/assets/741973/4d0f92d6-aca8-42bd-896a-0eb94cb32858) +![Treeview page](https://github.com/beholdr/filament-trilist/assets/741973/225ea768-3c42-45c3-a80d-88bdb159a4e5) -Import `FilamentTrilistPlugin` class and use it on your Filament panel provider: +Create custom page class inside `Pages` directory of your resource directory. Note that page class extends `Beholdr\FilamentTrilist\Components\TrilistPage`: ```php -use Beholdr\FilamentTrilist\FilamentTrilistPlugin; - -class AdminPanelProvider extends PanelProvider -{ - public function panel(Panel $panel): Panel - { - return $panel - ->plugins([ - FilamentTrilistPlugin::make(), - ]); - } -} -``` - -Create custom page class inside `Pages` directory of your resource directory: - -``` php namespace App\Filament\Resources\PostResource\Pages; use App\Filament\Resources\PostResource; @@ -175,7 +158,7 @@ class TreePosts extends TrilistPage Register created page in the static `getPages()` method of your resource: -``` php +```php public static function getPages(): array { return [ @@ -185,11 +168,32 @@ public static function getPages(): array } ``` +Add link for the page to a panel navigation: + +```php +use Beholdr\FilamentTrilist\FilamentTrilistPlugin; +use Filament\Navigation\NavigationItem; + +class AdminPanelProvider extends PanelProvider +{ + public function panel(Panel $panel): Panel + { + return $panel + ->navigationItems([ + NavigationItem::make('Tree') + ->url(fn () => route('filament.admin.resources.posts.tree')) + ->isActiveWhen(fn () => request()->routeIs('filament.admin.resources.posts.tree')) + ->parentItem('Posts'), + ]) + } +} +``` + ### Treeview as index page -If you want to show treeview page as a first page of a resource, please modify `getPages()` method: +If you want to show treeview page as a first page of a resource, modify `getPages()` method: -``` diff +```diff class PostResource extends Resource { public static function getPages(): array @@ -208,20 +212,20 @@ class PostResource extends Resource } ``` -Then override `tableRoute` property on the trilist page class: +Then change navigation link to point to the table page instead of tree: -``` php -class TrilistPosts extends TrilistPage -{ - protected static string $tableRoute = 'table'; -} +```php +NavigationItem::make('Table') + ->url(fn () => route('filament.admin.resources.posts.table')) + ->isActiveWhen(fn () => request()->routeIs('filament.admin.resources.posts.table')) + ->parentItem('Posts'), ``` ### Treeview options You can set some tree options by overriding static methods in the custom page class: -``` php +```php class TreeCategories extends TrilistPage { public static function getFieldLabel(): string @@ -238,21 +242,11 @@ class TreeCategories extends TrilistPage - `isSearchable()`: enable filtering of items, default: false - `getSearchPrompt()`: search input placeholder -### Custom tab icons - -To change default tab icons you can use given methods on plugin initialization: - -``` php -FilamentTrilistPlugin::make() - ->tabIconTree('heroicon-o-bars-3-bottom-left') - ->tabIconTable('heroicon-o-table-cells') -``` - ## Tree data -You can use hierarchical data from any source while it follows format: +You can use hierarchical data from any source when it follows format: -``` php +```php [ ['id' => 'ID', 'label' => 'Item label', 'children' => [ ['id' => 'ID', 'label' => 'Item label', 'children' => [...]], @@ -265,7 +259,7 @@ You can use hierarchical data from any source while it follows format: You can use special library like [staudenmeir/laravel-adjacency-list](https://github.com/staudenmeir/laravel-adjacency-list) to generate tree items data, for example: -``` php +```php Category::tree()->get()->toTree() ``` @@ -274,7 +268,7 @@ Or use your own relationship methods, even with `ManyToMany` (multiple parents)
Migrations -``` php +```php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; @@ -307,7 +301,7 @@ return new class extends Migration
Model -``` php +```php namespace App\Models; use Illuminate\Contracts\Database\Eloquent\Builder; @@ -337,7 +331,7 @@ class Profession extends Model With given model you can generate tree data like this: -``` php +```php Profession::root()->get(); ``` diff --git a/resources/views/components/tabs.blade.php b/resources/views/components/tabs.blade.php deleted file mode 100644 index 5503cbe..0000000 --- a/resources/views/components/tabs.blade.php +++ /dev/null @@ -1,21 +0,0 @@ -@props(['tree' => false, 'treeTitle', 'treeIcon', 'treeRoute', 'tableTitle', 'tableIcon', 'tableRoute']) - - - - {{ $treeTitle }} - - - - {{ $tableTitle }} - - diff --git a/resources/views/page.blade.php b/resources/views/page.blade.php index e3e3133..a60b4d8 100644 --- a/resources/views/page.blade.php +++ b/resources/views/page.blade.php @@ -1,7 +1,3 @@ -@php - $editRoute = $this->getEditRoute(); -@endphp -
getEditRoute()) { + return 'undefined'; + } - public static function getEditRoute(): ?string - { - /** @var Resource */ - $resource = static::$resource; + $template = route($editRoute, ['record' => '#ID#'], false); - /** @var Page */ - $editPage = $resource::getPages()[static::$editRoute]->getPage(); - - return $editPage::getRouteName(); + return << `\${item.label}` + JS; } public static function getFieldId(): string @@ -63,4 +58,20 @@ public static function getSearchPrompt(): string { return __('filament-forms::components.select.search_prompt'); } + + protected function getEditRoute(): ?string + { + /** @var Resource */ + $resource = static::$resource; + $pages = $resource::getPages(); + + if (empty($pages[static::$editRoute])) { + return null; + } + + /** @var Page */ + $editPage = $pages[static::$editRoute]->getPage(); + + return $editPage::getRouteName(); + } } diff --git a/src/FilamentTrilistPlugin.php b/src/FilamentTrilistPlugin.php deleted file mode 100644 index 1686b9c..0000000 --- a/src/FilamentTrilistPlugin.php +++ /dev/null @@ -1,91 +0,0 @@ -getResources() as $resource) { - $pages = $resource::getPages(); - - foreach ($pages as $page) { - if (is_subclass_of($page->getPage(), TrilistPage::class, true)) { - /** @var TrilistPage */ - $trilistPage = $page->getPage(); - $treeTitle = $trilistPage::getNavigationLabel(); - $treeRoute = $trilistPage::getRouteName($panel->getId()); - $treeIcon = $this->tabIconTree; - - /** @var Page */ - $tablePage = $pages[$trilistPage::getTableRoute()]->getPage(); - $tableTitle = $tablePage::getNavigationLabel(); - $tableRoute = $tablePage::getRouteName($panel->getId()); - $tableIcon = $this->tabIconTable; - - $props = 'treeTitle="' . $treeTitle . '" treeIcon="' . $treeIcon . '" treeRoute="' . $treeRoute . '" tableTitle="' . $tableTitle . '" tableIcon="' . $tableIcon . '" tableRoute="' . $tableRoute . '"'; - - $panel->renderHook( - 'filament-trilist::page.before', - fn (): string => Blade::render(''), - $trilistPage::getResource(), - ); - $panel->renderHook( - 'panels::resource.pages.list-records.table.before', - fn (): string => Blade::render(''), - $trilistPage::getResource(), - ); - } - } - } - } - - public function boot(Panel $panel): void - { - // - } - - public static function make(): static - { - return app(static::class); - } - - public static function get(): static - { - /** @var static $plugin */ - $plugin = filament(app(static::class)->getId()); - - return $plugin; - } - - public function tabIconTree(string $icon): static - { - $this->tabIconTree = $icon; - - return $this; - } - - public function tabIconTable(string $icon): static - { - $this->tabIconTable = $icon; - - return $this; - } -} diff --git a/src/FilamentTrilistServiceProvider.php b/src/FilamentTrilistServiceProvider.php index f0e52e5..1e41794 100644 --- a/src/FilamentTrilistServiceProvider.php +++ b/src/FilamentTrilistServiceProvider.php @@ -23,8 +23,7 @@ public function configurePackage(Package $package): void * More info: https://github.com/spatie/laravel-package-tools */ $package->name(static::$name) - ->hasViews(static::$viewNamespace) - ->hasViewComponent(static::$viewNamespace, 'tabs'); + ->hasViews(static::$viewNamespace); } public function packageBooted(): void