Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Add support of extendsInfolist hook #1531

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
Loading