Skip to content

Commit

Permalink
Allow page headers to be customised (#1573)
Browse files Browse the repository at this point in the history
Adds a new trait to allow page headers to be customised.

---------

Co-authored-by: glennjacobs <glennjacobs@users.noreply.github.com>
  • Loading branch information
glennjacobs and glennjacobs authored Feb 22, 2024
1 parent 3c954f8 commit 3c87ba0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/admin/extending/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ use Lunar\Admin\Filament\Widgets;

class MyCreateExtension extends CreatePageExtension
{
public function heading($title): string
{
return $title . ' - Example';
}

public function subheading($title): string
{
return $title . ' - Example';
}

public function headerWidgets(array $widgets): array
{
$widgets = [
Expand Down Expand Up @@ -110,6 +120,16 @@ use Lunar\Admin\Filament\Widgets;

class MyEditExtension extends EditPageExtension
{
public function heading($title): string
{
return $title . ' - Example';
}

public function subheading($title): string
{
return $title . ' - Example';
}

public function headerWidgets(array $widgets): array
{
$widgets = [
Expand Down Expand Up @@ -199,6 +219,16 @@ use Lunar\Admin\Filament\Widgets;

class MyListExtension extends ListPageExtension
{
public function heading($title): string
{
return $title . ' - Example';
}

public function subheading($title): string
{
return $title . ' - Example';
}

public function headerWidgets(array $widgets): array
{
$widgets = [
Expand Down Expand Up @@ -250,6 +280,16 @@ use Lunar\Admin\Support\Extending\ViewPageExtension;

class MyViewExtension extends ViewPageExtension
{
public function heading($title): string
{
return $title . ' - Example';
}

public function subheading($title): string
{
return $title . ' - Example';
}

public function headerActions(array $actions): array
{
$actions = [
Expand Down
1 change: 1 addition & 0 deletions packages/admin/src/Support/Pages/BaseCreateRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class BaseCreateRecord extends CreateRecord
use Concerns\ExtendsFormActions;
use Concerns\ExtendsHeaderActions;
use Concerns\ExtendsHeaderWidgets;
use Concerns\ExtendsHeadings;
use \Lunar\Admin\Support\Concerns\CallsHooks;

protected function mutateFormDataBeforeCreate(array $data): array
Expand Down
1 change: 1 addition & 0 deletions packages/admin/src/Support/Pages/BaseEditRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class BaseEditRecord extends EditRecord
use Concerns\ExtendsFormActions;
use Concerns\ExtendsHeaderActions;
use Concerns\ExtendsHeaderWidgets;
use Concerns\ExtendsHeadings;
use \Lunar\Admin\Support\Concerns\CallsHooks;

protected function mutateFormDataBeforeFill(array $data): array
Expand Down
1 change: 1 addition & 0 deletions packages/admin/src/Support/Pages/BaseListRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class BaseListRecords extends ListRecords
use Concerns\ExtendsFooterWidgets;
use Concerns\ExtendsHeaderActions;
use Concerns\ExtendsHeaderWidgets;
use Concerns\ExtendsHeadings;
use \Lunar\Admin\Support\Concerns\CallsHooks;

protected function applySearchToTableQuery(Builder $query): Builder
Expand Down
1 change: 1 addition & 0 deletions packages/admin/src/Support/Pages/BaseViewRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
abstract class BaseViewRecord extends ViewRecord
{
use Concerns\ExtendsHeaderActions;
use Concerns\ExtendsHeadings;
use \Lunar\Admin\Support\Concerns\CallsHooks;
}
28 changes: 28 additions & 0 deletions packages/admin/src/Support/Pages/Concerns/ExtendsHeadings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Lunar\Admin\Support\Pages\Concerns;

use Illuminate\Contracts\Support\Htmlable;

trait ExtendsHeadings
{
public function getDefaultHeading(): string
{
return $this->heading ?? $this->getTitle();
}

public function getHeading(): string|Htmlable
{
return $this->callLunarHook('heading', $this->getDefaultHeading());
}

public function getDefaultSubheading(): ?string
{
return $this->subheading;
}

public function getSubheading(): string|Htmlable|null
{
return $this->callLunarHook('subHeading', $this->getDefaultSubheading());
}
}
23 changes: 23 additions & 0 deletions tests/admin/Feature/Support/Extending/CreatePageExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
uses(\Lunar\Tests\Admin\Feature\Filament\TestCase::class)
->group('extending');

it('can customise page headings', function () {
$class = new class extends \Lunar\Admin\Support\Extending\CreatePageExtension
{
public function heading($title): string
{
return 'New Heading';
}

public function subheading($title): string
{
return 'New Subheading';
}
};

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

$this->asStaff(admin: true);

\Livewire\Livewire::test(CreateCustomer::class)
->assertSee('New Heading')
->assertSee('New Subheading');
});

it('can change data before creation', function () {
$class = new class extends \Lunar\Admin\Support\Extending\CreatePageExtension
{
Expand Down

0 comments on commit 3c87ba0

Please sign in to comment.