From 3c87ba080d5a04c93b65941990f7a368bdd9c22a Mon Sep 17 00:00:00 2001 From: Glenn Jacobs Date: Thu, 22 Feb 2024 11:00:19 +0000 Subject: [PATCH] Allow page headers to be customised (#1573) Adds a new trait to allow page headers to be customised. --------- Co-authored-by: glennjacobs --- docs/admin/extending/pages.md | 40 +++++++++++++++++++ .../src/Support/Pages/BaseCreateRecord.php | 1 + .../src/Support/Pages/BaseEditRecord.php | 1 + .../src/Support/Pages/BaseListRecords.php | 1 + .../src/Support/Pages/BaseViewRecord.php | 1 + .../Pages/Concerns/ExtendsHeadings.php | 28 +++++++++++++ .../Extending/CreatePageExtensionTest.php | 23 +++++++++++ 7 files changed, 95 insertions(+) create mode 100644 packages/admin/src/Support/Pages/Concerns/ExtendsHeadings.php diff --git a/docs/admin/extending/pages.md b/docs/admin/extending/pages.md index 037fd84c37..f94ddcc7d9 100644 --- a/docs/admin/extending/pages.md +++ b/docs/admin/extending/pages.md @@ -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 = [ @@ -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 = [ @@ -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 = [ @@ -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 = [ diff --git a/packages/admin/src/Support/Pages/BaseCreateRecord.php b/packages/admin/src/Support/Pages/BaseCreateRecord.php index 679e2e7849..4a0b803d8e 100644 --- a/packages/admin/src/Support/Pages/BaseCreateRecord.php +++ b/packages/admin/src/Support/Pages/BaseCreateRecord.php @@ -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 diff --git a/packages/admin/src/Support/Pages/BaseEditRecord.php b/packages/admin/src/Support/Pages/BaseEditRecord.php index a51b632e07..444805e9de 100644 --- a/packages/admin/src/Support/Pages/BaseEditRecord.php +++ b/packages/admin/src/Support/Pages/BaseEditRecord.php @@ -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 diff --git a/packages/admin/src/Support/Pages/BaseListRecords.php b/packages/admin/src/Support/Pages/BaseListRecords.php index b834df5189..2e978ec95a 100644 --- a/packages/admin/src/Support/Pages/BaseListRecords.php +++ b/packages/admin/src/Support/Pages/BaseListRecords.php @@ -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 diff --git a/packages/admin/src/Support/Pages/BaseViewRecord.php b/packages/admin/src/Support/Pages/BaseViewRecord.php index 6cadd9df8e..bc84c1828f 100644 --- a/packages/admin/src/Support/Pages/BaseViewRecord.php +++ b/packages/admin/src/Support/Pages/BaseViewRecord.php @@ -7,5 +7,6 @@ abstract class BaseViewRecord extends ViewRecord { use Concerns\ExtendsHeaderActions; + use Concerns\ExtendsHeadings; use \Lunar\Admin\Support\Concerns\CallsHooks; } diff --git a/packages/admin/src/Support/Pages/Concerns/ExtendsHeadings.php b/packages/admin/src/Support/Pages/Concerns/ExtendsHeadings.php new file mode 100644 index 0000000000..b1713bda11 --- /dev/null +++ b/packages/admin/src/Support/Pages/Concerns/ExtendsHeadings.php @@ -0,0 +1,28 @@ +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()); + } +} diff --git a/tests/admin/Feature/Support/Extending/CreatePageExtensionTest.php b/tests/admin/Feature/Support/Extending/CreatePageExtensionTest.php index 36e9886f3e..40f9c558bd 100644 --- a/tests/admin/Feature/Support/Extending/CreatePageExtensionTest.php +++ b/tests/admin/Feature/Support/Extending/CreatePageExtensionTest.php @@ -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 {