fix: Table nested action arguments

This commit is contained in:
Dan Harrin
2023-12-26 19:05:30 +00:00
parent 3523bb77d5
commit 2bf92393e4
4 changed files with 58 additions and 5 deletions
@@ -114,11 +114,9 @@
@if (! ($this instanceof \Filament\Tables\Contracts\HasTable))
<x-filament-actions::modals />
@elseif ($this->isTableLoaded() &&
filled($this->defaultTableAction) &&
filled($this->defaultTableActionRecord))
@elseif ($this->isTableLoaded() && filled($this->defaultTableAction))
<div
wire:init="mountTableAction(@js($this->defaultTableAction), @js($this->defaultTableActionRecord))"
wire:init="mountTableAction(@js($this->defaultTableAction), @if (filled($this->defaultTableActionRecord)) @js($this->defaultTableActionRecord) @elsenull @endif @if (filled($this->defaultTableActionArguments)) , @js($this->defaultTableActionArguments) @endif)"
></div>
@endif
+35 -1
View File
@@ -29,6 +29,11 @@ trait HasActions
*/
public ?array $mountedTableActionsData = [];
/**
* @var array<string, array<string, mixed>> | null
*/
public ?array $mountedTableActionsArguments = [];
/**
* @var int | string | null
*/
@@ -44,6 +49,12 @@ trait HasActions
#[Url(as: 'tableAction')]
public $defaultTableAction = null;
/**
* @var mixed
*/
#[Url(as: 'tableActionArguments')]
public $defaultTableActionArguments = null;
/**
* @var mixed
*/
@@ -79,6 +90,8 @@ trait HasActions
$result = null;
$originallyMountedActions = $this->mountedTableActions;
try {
if ($this->mountedTableActionHasForm()) {
$action->callBeforeFormValidated();
@@ -116,6 +129,12 @@ trait HasActions
$action->resetArguments();
$action->resetFormData();
// If the action was replaced while it was being called,
// we don't want to unmount it.
if ($originallyMountedActions !== $this->mountedTableActions) {
return null;
}
$this->unmountTableAction();
return $result;
@@ -126,9 +145,13 @@ trait HasActions
$this->mountedTableActionRecord = $record;
}
public function mountTableAction(string $name, ?string $record = null): mixed
/**
* @param array<string, mixed> $arguments
*/
public function mountTableAction(string $name, ?string $record = null, array $arguments = []): mixed
{
$this->mountedTableActions[] = $name;
$this->mountedTableActionsArguments[] = $arguments;
$this->mountedTableActionsData[] = [];
if (count($this->mountedTableActions) === 1) {
@@ -190,6 +213,15 @@ trait HasActions
return null;
}
/**
* @param array<string, mixed> $arguments
*/
public function replaceMountedTableAction(string $name, ?string $record = null, array $arguments = []): void
{
$this->resetMountedTableActionProperties();
$this->mountTableAction($name, $record ?? $this->mountedTableActionRecord, $arguments);
}
public function mountedTableActionShouldOpenModal(): bool
{
$action = $this->getMountedTableAction();
@@ -270,6 +302,7 @@ trait HasActions
protected function resetMountedTableActionProperties(): void
{
$this->mountedTableActions = [];
$this->mountedTableActionsArguments = [];
$this->mountedTableActionsData = [];
}
@@ -305,6 +338,7 @@ trait HasActions
// Setting these to `null` creates a bug where the properties are
// actually set to `'null'` strings and remain in the URL.
$this->defaultTableAction = [];
$this->defaultTableActionArguments = [];
$this->defaultTableActionRecord = [];
return;
@@ -107,4 +107,9 @@ interface HasTable
public function getFilteredSortedTableQuery(): Builder;
public function makeFilamentTranslatableContentDriver(): ?TranslatableContentDriver;
/**
* @param array<string, mixed> $arguments
*/
public function replaceMountedTableAction(string $name, ?string $record = null, array $arguments = []): void;
}
@@ -181,6 +181,15 @@ trait HasActions
*/
protected function getMountableModalActionFromAction(Action $action, array $modalActionNames, ?Model $mountedRecord = null): ?Action
{
$arguments = $this->getLivewire()->mountedTableActionsArguments ?? [];
if (
(($actionArguments = array_shift($arguments)) !== null) &&
(! $action->hasArguments())
) {
$action->arguments($actionArguments);
}
foreach ($modalActionNames as $modalActionName) {
$action = $action->getMountableModalAction($modalActionName);
@@ -191,6 +200,13 @@ trait HasActions
if ($action instanceof Action) {
$action->record($mountedRecord);
}
if (
(($actionArguments = array_shift($arguments)) !== null) &&
(! $action->hasArguments())
) {
$action->arguments($actionArguments);
}
}
if (! $action instanceof Action) {