Merge pull request #11899 from filamentphp/fix/action-inheriting-from-group

fix: Actions inheriting props from groups
This commit is contained in:
Dan Harrin
2024-03-17 20:47:13 +00:00
committed by GitHub
10 changed files with 115 additions and 33 deletions
+24 -11
View File
@@ -2,6 +2,7 @@
namespace Filament\Actions;
use Exception;
use Filament\Actions\Contracts\HasLivewire;
use Filament\Support\Components\ViewComponent;
use Filament\Support\Concerns\HasBadge;
@@ -13,6 +14,7 @@ use Livewire\Component;
class ActionGroup extends ViewComponent implements HasLivewire
{
use Concerns\BelongsToGroup;
use Concerns\CanBeHidden {
isHidden as baseIsHidden;
}
@@ -50,6 +52,8 @@ class ActionGroup extends ViewComponent implements HasLivewire
*/
protected array $flatActions;
protected Component $livewire;
protected string $evaluationIdentifier = 'group';
protected string $viewIdentifier = 'group';
@@ -89,6 +93,8 @@ class ActionGroup extends ViewComponent implements HasLivewire
$this->flatActions = [];
foreach ($actions as $action) {
$action->group($this);
if ($action instanceof ActionGroup) {
$action->dropdownPlacement('right-top');
@@ -148,17 +154,26 @@ class ActionGroup extends ViewComponent implements HasLivewire
public function livewire(Component $livewire): static
{
foreach ($this->actions as $action) {
if (! $action instanceof HasLivewire) {
continue;
}
$action->livewire($livewire);
}
$this->livewire = $livewire;
return $this;
}
public function getLivewire(): object
{
if (isset($this->livewire)) {
return $this->livewire;
}
$group = $this->getGroup();
if (! ($group instanceof HasLivewire)) {
throw new Exception('This action group does not belong to a Livewire component.');
}
return $group->getLivewire();
}
public function getLabel(): string
{
$label = $this->evaluate($this->label) ?? __('filament-actions::group.trigger.label');
@@ -192,14 +207,12 @@ class ActionGroup extends ViewComponent implements HasLivewire
public function isHidden(): bool
{
$condition = $this->baseIsHidden();
if ($condition) {
if ($this->baseIsHidden()) {
return true;
}
foreach ($this->getActions() as $action) {
if ($action->isHidden()) {
if ($action->isHiddenInGroup()) {
continue;
}
@@ -0,0 +1,22 @@
<?php
namespace Filament\Actions\Concerns;
use Filament\Actions\ActionGroup;
trait BelongsToGroup
{
protected ?ActionGroup $group = null;
public function group(?ActionGroup $group): static
{
$this->group = $group;
return $this;
}
public function getGroup(): ?ActionGroup
{
return $this->group;
}
}
@@ -2,6 +2,8 @@
namespace Filament\Actions\Concerns;
use Exception;
use Filament\Actions\Contracts\HasLivewire;
use Livewire\Component;
trait BelongsToLivewire
@@ -17,6 +19,16 @@ trait BelongsToLivewire
public function getLivewire(): object
{
return $this->livewire;
if (isset($this->livewire)) {
return $this->livewire;
}
$group = $this->getGroup();
if (! ($group instanceof HasLivewire)) {
throw new Exception('This action does not belong to a Livewire component.');
}
return $group->getLivewire();
}
}
@@ -93,6 +93,15 @@ trait CanBeHidden
}
public function isHidden(): bool
{
if ($this->getGroup()?->isHidden()) {
return true;
}
return $this->isHiddenInGroup();
}
public function isHiddenInGroup(): bool
{
if ($this->evaluate($this->isHidden)) {
return true;
@@ -3,6 +3,7 @@
namespace Filament\Actions\Concerns;
use Closure;
use Filament\Actions\Contracts\HasRecord;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
@@ -67,7 +68,19 @@ trait InteractsWithRecord
public function getRecord(): ?Model
{
return $this->evaluate($this->record);
$record = $this->evaluate($this->record);
if ($record) {
return $record;
}
$group = $this->getGroup();
if (! ($group instanceof HasRecord)) {
return null;
}
return $group->getRecord();
}
public function getRecordTitle(?Model $record = null): ?string
@@ -7,4 +7,6 @@ use Livewire\Component;
interface HasLivewire
{
public function livewire(Component $livewire): static;
public function getLivewire(): object;
}
+1
View File
@@ -13,6 +13,7 @@ use Illuminate\Support\Traits\Conditionable;
class StaticAction extends ViewComponent
{
use Concerns\BelongsToGroup;
use Concerns\CanBeDisabled;
use Concerns\CanBeHidden;
use Concerns\CanBeLabeledFrom;
+15 -19
View File
@@ -2,7 +2,7 @@
namespace Filament\Tables\Actions;
use Closure;
use Exception;
use Filament\Actions\ActionGroup as BaseActionGroup;
use Filament\Actions\Concerns\InteractsWithRecord;
use Filament\Actions\Contracts\HasRecord;
@@ -17,32 +17,28 @@ class ActionGroup extends BaseActionGroup implements HasRecord, HasTable
{
use InteractsWithRecord;
public function record(Model | Closure | null $record): static
protected Table $table;
public function table(Table $table): static
{
$this->record = $record;
foreach ($this->actions as $action) {
if (! $action instanceof HasRecord) {
continue;
}
$action->record($record);
}
$this->table = $table;
return $this;
}
public function table(Table $table): static
public function getTable(): Table
{
foreach ($this->actions as $action) {
if (! $action instanceof HasTable) {
continue;
}
$action->table($table);
if (isset($this->table)) {
return $this->table;
}
return $this;
$group = $this->getGroup();
if (! ($group instanceof HasTable)) {
throw new Exception('This action does not belong to a table.');
}
return $group->getTable();
}
/**
@@ -2,6 +2,8 @@
namespace Filament\Tables\Actions\Concerns;
use Exception;
use Filament\Tables\Actions\Contracts\HasTable as ActionHasTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
@@ -18,7 +20,17 @@ trait BelongsToTable
public function getTable(): Table
{
return $this->table;
if (isset($this->table)) {
return $this->table;
}
$group = $this->getGroup();
if (! ($group instanceof ActionHasTable)) {
throw new Exception('This action does not belong to a table.');
}
return $group->getTable();
}
public function getLivewire(): HasTable
@@ -7,4 +7,6 @@ use Filament\Tables\Table;
interface HasTable
{
public function table(Table $table): static;
public function getTable(): Table;
}