From 56eba8bb46d418df540b358bd9d5806334b151f0 Mon Sep 17 00:00:00 2001 From: slovenianGooner Date: Tue, 27 Feb 2024 21:51:34 +0100 Subject: [PATCH 01/12] Add action, tag and method options for MenuItem This adds an action, tag and method options for MenuItem. --- packages/panels/src/Navigation/MenuItem.php | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index bb16def4b6..e09684b87c 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -21,6 +21,12 @@ class MenuItem extends Component protected string | Closure | Native | null $url = null; + protected string | Closure | Native | null $action = null; + + protected string | Closure | null $tag = "a"; + + protected string | Closure | null $method = null; + protected bool | Closure $shouldOpenUrlInNewTab = false; protected bool | Closure $isHidden = false; @@ -78,6 +84,27 @@ class MenuItem extends Component return $this; } + public function action(string | Closure | null $action): static + { + $this->action = $action; + + return $this; + } + + public function tag(string | Closure | null $tag): static + { + $this->tag = $tag; + + return $this; + } + + public function method(string | Closure | null $method): static + { + $this->method = $method; + + return $this; + } + public function openUrlInNewTab(bool | Closure $condition = true): static { $this->shouldOpenUrlInNewTab = $condition; @@ -141,6 +168,21 @@ class MenuItem extends Component return $this->evaluate($this->url); } + public function getAction(): ?string + { + return $this->evaluate($this->action); + } + + public function getTag(): string + { + return $this->evaluate($this->tag); + } + + public function getMethod(): ?string + { + return $this->evaluate($this->method); + } + public function shouldOpenUrlInNewTab(): bool { return (bool) $this->evaluate($this->shouldOpenUrlInNewTab); From e4a3f644e01bbcaef05f4eebf3fb1e9ced130b27 Mon Sep 17 00:00:00 2001 From: slovenianGooner Date: Tue, 27 Feb 2024 21:56:39 +0100 Subject: [PATCH 02/12] Update user-menu.blade.php --- packages/panels/resources/views/components/user-menu.blade.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/panels/resources/views/components/user-menu.blade.php b/packages/panels/resources/views/components/user-menu.blade.php index f28518e402..e069ff3443 100644 --- a/packages/panels/resources/views/components/user-menu.blade.php +++ b/packages/panels/resources/views/components/user-menu.blade.php @@ -69,10 +69,13 @@ @foreach ($items as $key => $item) {{ $item->getLabel() }} From 88694ec242823817c96c01fd124c2c0b0b4cb155 Mon Sep 17 00:00:00 2001 From: slovenianGooner Date: Tue, 27 Feb 2024 22:00:08 +0100 Subject: [PATCH 03/12] Fix additional tag prop. --- packages/panels/resources/views/components/user-menu.blade.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/panels/resources/views/components/user-menu.blade.php b/packages/panels/resources/views/components/user-menu.blade.php index e069ff3443..800304af83 100644 --- a/packages/panels/resources/views/components/user-menu.blade.php +++ b/packages/panels/resources/views/components/user-menu.blade.php @@ -73,7 +73,6 @@ :href="$item->getUrl()" :target="$item->shouldOpenUrlInNewTab() ? '_blank' : null" :icon="$item->getIcon()" - tag="a" :tag="$item->getTag() ?? 'a'" :method="$item->getMethod() ?? null" > From 831577c29ca535a53acbf6d13d1d0d792c6b6bfa Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:28:16 +0000 Subject: [PATCH 04/12] Update MenuItem.php --- packages/panels/src/Navigation/MenuItem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index e09684b87c..7310ed502d 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -21,9 +21,9 @@ class MenuItem extends Component protected string | Closure | Native | null $url = null; - protected string | Closure | Native | null $action = null; + protected string | Closure | null $action = null; - protected string | Closure | null $tag = "a"; + protected string | Closure | null $tag = 'a'; protected string | Closure | null $method = null; From b25e82b5f9445530edf9aba89ddf8cc7846aa7ac Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:28:35 +0000 Subject: [PATCH 05/12] Update MenuItem.php --- packages/panels/src/Navigation/MenuItem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index 7310ed502d..f3095bfd09 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -23,7 +23,7 @@ class MenuItem extends Component protected string | Closure | null $action = null; - protected string | Closure | null $tag = 'a'; + protected string | Closure | null $tag = null; protected string | Closure | null $method = null; @@ -175,7 +175,7 @@ class MenuItem extends Component public function getTag(): string { - return $this->evaluate($this->tag); + return $this->evaluate($this->tag) ?? 'a'; } public function getMethod(): ?string From 17d7be48a2be8e5256242315ac1bd0004eb60e9b Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:29:32 +0000 Subject: [PATCH 06/12] Update user-menu.blade.php --- .../panels/resources/views/components/user-menu.blade.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/panels/resources/views/components/user-menu.blade.php b/packages/panels/resources/views/components/user-menu.blade.php index 800304af83..087873ce9a 100644 --- a/packages/panels/resources/views/components/user-menu.blade.php +++ b/packages/panels/resources/views/components/user-menu.blade.php @@ -68,13 +68,13 @@ @foreach ($items as $key => $item) {{ $item->getLabel() }} From e113c2ca66ac845e50225490e823403ba8ce5be4 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:34:41 +0000 Subject: [PATCH 07/12] Update MenuItem.php --- packages/panels/src/Navigation/MenuItem.php | 30 +++++---------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index f3095bfd09..997350642b 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -21,11 +21,7 @@ class MenuItem extends Component protected string | Closure | Native | null $url = null; - protected string | Closure | null $action = null; - - protected string | Closure | null $tag = null; - - protected string | Closure | null $method = null; + protected string | Closure | null $postAction = null; protected bool | Closure $shouldOpenUrlInNewTab = false; @@ -84,23 +80,9 @@ class MenuItem extends Component return $this; } - public function action(string | Closure | null $action): static + public function postAction(?string $action): static { - $this->action = $action; - - return $this; - } - - public function tag(string | Closure | null $tag): static - { - $this->tag = $tag; - - return $this; - } - - public function method(string | Closure | null $method): static - { - $this->method = $method; + $this->postAction = $action; return $this; } @@ -170,17 +152,17 @@ class MenuItem extends Component public function getAction(): ?string { - return $this->evaluate($this->action); + return $this->evaluate($this->postAction); } public function getTag(): string { - return $this->evaluate($this->tag) ?? 'a'; + return filled($this->getAction()) ? 'form' : 'a'; } public function getMethod(): ?string { - return $this->evaluate($this->method); + return filled($this->getAction()) ? 'post' : null; } public function shouldOpenUrlInNewTab(): bool From 255ea998c47516e45dcac97152858a6f58cfa39e Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:35:11 +0000 Subject: [PATCH 08/12] Update MenuItem.php --- packages/panels/src/Navigation/MenuItem.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index 997350642b..ed238a810f 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -21,7 +21,7 @@ class MenuItem extends Component protected string | Closure | Native | null $url = null; - protected string | Closure | null $postAction = null; + protected string | Closure | null $action = null; protected bool | Closure $shouldOpenUrlInNewTab = false; @@ -82,7 +82,7 @@ class MenuItem extends Component public function postAction(?string $action): static { - $this->postAction = $action; + $this->action = $action; return $this; } @@ -152,7 +152,7 @@ class MenuItem extends Component public function getAction(): ?string { - return $this->evaluate($this->postAction); + return $this->evaluate($this->action); } public function getTag(): string From 1bae548754e2c18e04b5d2f053f9770062a43ca1 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:35:30 +0000 Subject: [PATCH 09/12] Update MenuItem.php --- packages/panels/src/Navigation/MenuItem.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index ed238a810f..e34220bf4a 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -155,16 +155,6 @@ class MenuItem extends Component return $this->evaluate($this->action); } - public function getTag(): string - { - return filled($this->getAction()) ? 'form' : 'a'; - } - - public function getMethod(): ?string - { - return filled($this->getAction()) ? 'post' : null; - } - public function shouldOpenUrlInNewTab(): bool { return (bool) $this->evaluate($this->shouldOpenUrlInNewTab); From 64fc559d7c8c302c625c2f306929e395422e9207 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:36:34 +0000 Subject: [PATCH 10/12] Update user-menu.blade.php --- .../resources/views/components/user-menu.blade.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/panels/resources/views/components/user-menu.blade.php b/packages/panels/resources/views/components/user-menu.blade.php index 087873ce9a..a72a0ed9e9 100644 --- a/packages/panels/resources/views/components/user-menu.blade.php +++ b/packages/panels/resources/views/components/user-menu.blade.php @@ -67,13 +67,17 @@ @foreach ($items as $key => $item) + @php + $itemAction = $item->getAction(); + @endphp + {{ $item->getLabel() }} From d2dd625d099de81ffaa4b308ea26929528b3ce4f Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:37:03 +0000 Subject: [PATCH 11/12] Update MenuItem.php --- packages/panels/src/Navigation/MenuItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index e34220bf4a..964fea9303 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -80,7 +80,7 @@ class MenuItem extends Component return $this; } - public function postAction(?string $action): static + public function postAction(string | Closure | null $action): static { $this->action = $action; From da9404507c5406faee88d22ff034b20021541b7c Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 28 Feb 2024 11:42:07 +0000 Subject: [PATCH 12/12] clean up and docs --- packages/panels/docs/06-navigation.md | 12 ++++++++++++ packages/panels/docs/11-tenancy.md | 12 ++++++++++++ .../resources/views/components/tenant-menu.blade.php | 10 ++++++++-- .../resources/views/components/user-menu.blade.php | 10 +++++----- packages/panels/src/Navigation/MenuItem.php | 10 +++++----- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/panels/docs/06-navigation.md b/packages/panels/docs/06-navigation.md index 3efad6efcb..3730d7742c 100644 --- a/packages/panels/docs/06-navigation.md +++ b/packages/panels/docs/06-navigation.md @@ -486,6 +486,18 @@ MenuItem::make() ->hidden(fn (): bool => ! auth()->user()->can('viewAny', Payment::class)) ``` +### Sending a `POST` HTTP request from a user menu item + +You can send a `POST` HTTP request from a user menu item by passing a URL to the `postAction()` method: + +```php +use Filament\Navigation\MenuItem; + +MenuItem::make() + ->label('Lock session') + ->postAction(fn (): string => route('lock-session')) +``` + ## Disabling breadcrumbs The default layout will show breadcrumbs to indicate the location of the current page within the hierarchy of the app. diff --git a/packages/panels/docs/11-tenancy.md b/packages/panels/docs/11-tenancy.md index 9c23a614e2..7c41371367 100644 --- a/packages/panels/docs/11-tenancy.md +++ b/packages/panels/docs/11-tenancy.md @@ -435,6 +435,18 @@ MenuItem::make() ->hidden(fn (): bool => ! auth()->user()->can('manage-team')) ``` +### Sending a `POST` HTTP request from a tenant menu item + +You can send a `POST` HTTP request from a tenant menu item by passing a URL to the `postAction()` method: + +```php +use Filament\Navigation\MenuItem; + +MenuItem::make() + ->label('Lock session') + ->postAction(fn (): string => route('lock-session')) +``` + ### Hiding the tenant menu You can hide the tenant menu by using the `tenantMenu(false)` diff --git a/packages/panels/resources/views/components/tenant-menu.blade.php b/packages/panels/resources/views/components/tenant-menu.blade.php index 8d3dd02753..e8f2d3ebfb 100644 --- a/packages/panels/resources/views/components/tenant-menu.blade.php +++ b/packages/panels/resources/views/components/tenant-menu.blade.php @@ -116,12 +116,18 @@ @if (count($items)) @foreach ($items as $item) + @php + $itemPostAction = $item->getPostAction(); + @endphp + {{ $item->getLabel() }} diff --git a/packages/panels/resources/views/components/user-menu.blade.php b/packages/panels/resources/views/components/user-menu.blade.php index a72a0ed9e9..a14456121e 100644 --- a/packages/panels/resources/views/components/user-menu.blade.php +++ b/packages/panels/resources/views/components/user-menu.blade.php @@ -68,16 +68,16 @@ @foreach ($items as $key => $item) @php - $itemAction = $item->getAction(); + $itemPostAction = $item->getPostAction(); @endphp - + {{ $item->getLabel() }} diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index 964fea9303..20ccc2c130 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -8,6 +8,8 @@ use Laravel\SerializableClosure\Serializers\Native; class MenuItem extends Component { + protected string | Closure | null $postAction = null; + /** * @var string | array{50: string, 100: string, 200: string, 300: string, 400: string, 500: string, 600: string, 700: string, 800: string, 900: string, 950: string} | Closure | null */ @@ -21,8 +23,6 @@ class MenuItem extends Component protected string | Closure | Native | null $url = null; - protected string | Closure | null $action = null; - protected bool | Closure $shouldOpenUrlInNewTab = false; protected bool | Closure $isHidden = false; @@ -82,7 +82,7 @@ class MenuItem extends Component public function postAction(string | Closure | null $action): static { - $this->action = $action; + $this->postAction = $action; return $this; } @@ -150,9 +150,9 @@ class MenuItem extends Component return $this->evaluate($this->url); } - public function getAction(): ?string + public function getPostAction(): ?string { - return $this->evaluate($this->action); + return $this->evaluate($this->postAction); } public function shouldOpenUrlInNewTab(): bool