Skip to content

Commit

Permalink
Feature - Add support of extendsInfolist hook (#1531)
Browse files Browse the repository at this point in the history
This PR add support of extendsInfolist hook and fix headerWidget /
FooterWidget not triggered on abstract BaseViewRecord

---------

Co-authored-by: Glenn Jacobs <[email protected]>
  • Loading branch information
lguichard and glennjacobs authored May 22, 2024
1 parent e34842d commit 73ec6f9
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
35 changes: 34 additions & 1 deletion docs/admin/extending/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,26 @@ An example of extending a view page.

```php
use Filament\Actions;

use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Infolists\Infolist;
use Filament\Infolists\Components\TextEntry;
use Lunar\Admin\Support\Extending\ViewPageExtension;
use Lunar\Admin\Filament\Widgets;

class MyViewExtension extends ViewPageExtension
{
public function headerWidgets(array $widgets): array
{
$widgets = [
...$widgets,
Widgets\Dashboard\Orders\OrderStatsOverview::make(),
];

return $widgets;
}

public function heading($title): string
{
return $title . ' - Example';
Expand All @@ -293,7 +309,24 @@ class MyViewExtension extends ViewPageExtension

return $actions;
}


public function extendsInfolist(Infolist $infolist): Infolist
{
return $infolist->schema([
...$infolist->getComponents(true),
TextEntry::make('custom_title'),
]);
}

public function footerWidgets(array $widgets): array
{
$widgets = [
...$widgets,
Widgets\Dashboard\Orders\LatestOrdersTable::make(),
];

return $widgets;
}
}

// Typically placed in your AppServiceProvider file...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static function getOrderSummaryInfolist(): Infolists\Components\Component
]);
}

public function infolist(Infolist $infolist): Infolist
public function getDefaultInfolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Expand Down
3 changes: 3 additions & 0 deletions packages/admin/src/Support/Pages/BaseViewRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

abstract class BaseViewRecord extends ViewRecord
{
use Concerns\ExtendsFooterWidgets;
use Concerns\ExtendsHeaderActions;
use Concerns\ExtendsHeaderWidgets;
use Concerns\ExtendsHeadings;
use Concerns\ExtendsInfolist;
use \Lunar\Admin\Support\Concerns\CallsHooks;
use \Lunar\Admin\Support\Concerns\CallsHooks;
}
18 changes: 18 additions & 0 deletions packages/admin/src/Support/Pages/Concerns/ExtendsInfolist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Lunar\Admin\Support\Pages\Concerns;

use Filament\Infolists\Infolist;

trait ExtendsInfolist
{
public function infolist(Infolist $infolist): Infolist
{
return self::callLunarHook('extendsInfolist', $this->getDefaultInfolist($infolist));
}

protected function getDefaultInfolist(Infolist $infolist): Infolist
{
return $infolist;
}
}
59 changes: 59 additions & 0 deletions tests/admin/Feature/Support/Extending/ViewPageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Filament\Infolists\Infolist;
use Illuminate\Support\Str;
use Lunar\Admin\Filament\Resources\OrderResource\Pages\ManageOrder;
use Lunar\Admin\Support\Facades\LunarPanel;

uses(\Lunar\Tests\Admin\Feature\Filament\TestCase::class)
->group('extending.view');

beforeEach(function () {
$this->asStaff();

$currency = \Lunar\Models\Currency::factory()->create([
'default' => true,
]);

$country = \Lunar\Models\Country::factory()->create();

$this->order = \Lunar\Models\Order::factory()
->for(\Lunar\Models\Customer::factory())
->has(\Lunar\Models\OrderAddress::factory()->state([
'type' => 'shipping',
'country_id' => $country->id,
]), 'shippingAddress')
->has(\Lunar\Models\OrderAddress::factory()->state([
'type' => 'billing',
'country_id' => $country->id,
]), 'billingAddress')
->create([
'currency_code' => $currency->code,
'meta' => [
'additional_info' => Str::random(),
],
]);

});

it('can extend Infolist', function () {
$class = new class extends \Lunar\Admin\Support\Extending\ViewPageExtension
{
public function extendsInfolist(Infolist $infolist): Infolist
{
return $infolist->schema([
...$infolist->getComponents(true),
\Filament\Infolists\Components\TextEntry::make('custom_title')
->label('custom_title'),
]);
}
};

LunarPanel::registerExtension($class, ManageOrder::class);

\Livewire\Livewire::test(ManageOrder::class, [
'record' => $this->order->getRouteKey(),
])
->assertSee($this->order->reference)
->assertSee('custom_title');
});

0 comments on commit 73ec6f9

Please sign in to comment.