Skip to content

Commit

Permalink
Pass schema component container through to action
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Jan 16, 2025
1 parent fe1bcda commit c9e9afe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
15 changes: 9 additions & 6 deletions packages/actions/src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,10 @@ protected function getJavaScriptClickHandler(): string
$context['recordKey'] = $this->resolveRecordKey($record);
}

if (filled($componentKey = $this->getSchemaComponent()?->getKey())) {
$context['schemaComponent'] = $componentKey;
if (filled($schemaComponentContainerKey = $this->getSchemaComponentContainer()?->getKey())) {
$context['schemaComponentContainer'] = $schemaComponentContainerKey;
} elseif (filled($schemaComponentKey = $this->getSchemaComponent()?->getKey())) {
$context['schemaComponent'] = $schemaComponentKey;
}

$table = $this->getTable();
Expand Down Expand Up @@ -445,14 +447,15 @@ protected function resolveDefaultClosureDependencyForEvaluationByName(string $pa
return match ($parameterName) {
'arguments' => [$this->getArguments()],
'component', 'schemaComponent' => [$this->getSchemaComponent()],
'context', 'operation' => [$this->getSchemaComponent()->getContainer()->getOperation()],
'context', 'operation' => [$this->getSchemaComponentContainer()?->getOperation() ?? $this->getSchemaComponent()?->getContainer()->getOperation()],
'data' => [$this->getFormData()],
'get' => [$this->getSchemaComponent()->makeGetUtility()],
'livewire' => [$this->getLivewire()],
'model' => [$this->getModel() ?? $this->getSchemaComponent()?->getModel()],
'model' => [$this->getModel() ?? $this->getSchemaComponentContainer()?->getModel() ?? $this->getSchemaComponent()?->getModel()],
'mountedActions' => [$this->getLivewire()->getMountedActions()],
'record' => [$this->getRecord() ?? $this->getSchemaComponent()?->getRecord()],
'record' => [$this->getRecord() ?? $this->getSchemaComponentContainer()?->getRecord() ?? $this->getSchemaComponent()?->getRecord()],
'records', 'selectedRecords' => [$this->getSelectedRecords()],
'schema' => [$this->getSchemaComponentContainer()],
'set' => [$this->getSchemaComponent()->makeSetUtility()],
'state' => [$this->getSchemaComponent()->getState()],
'table' => [$this->getTable()],
Expand All @@ -465,7 +468,7 @@ protected function resolveDefaultClosureDependencyForEvaluationByName(string $pa
*/
protected function resolveDefaultClosureDependencyForEvaluationByType(string $parameterType): array
{
$record = $this->getRecord() ?? $this->getSchemaComponent()?->getRecord();
$record = $this->getRecord() ?? $this->getSchemaComponentContainer()?->getRecord() ?? $this->getSchemaComponent()?->getRecord();

return match ($parameterType) {
EloquentCollection::class, Collection::class => [$this->getSelectedRecords()],
Expand Down
4 changes: 4 additions & 0 deletions packages/actions/src/Concerns/BelongsToLivewire.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function getLivewire(): ?object
return $this->livewire;
}

if ($livewire = $this->getSchemaComponentContainer()?->getLivewire()) {
return $livewire;
}

if ($livewire = $this->getSchemaComponent()?->getLivewire()) {
return $livewire;
}
Expand Down
17 changes: 16 additions & 1 deletion packages/actions/src/Concerns/BelongsToSchemaComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@
namespace Filament\Actions\Concerns;

use Filament\Schemas\Components\Component;
use Filament\Schemas\Schema;

trait BelongsToSchemaComponent
{
protected ?Component $schemaComponent = null;

protected ?Schema $schemaComponentContainer = null;

public function schemaComponent(?Component $component): static
{
$this->schemaComponent = $component;

return $this;
}

public function schemaComponentContainer(?Schema $schema): static
{
$this->schemaComponentContainer = $schema;

return $this;
}

public function getSchemaComponent(): ?Component
{
return $this->schemaComponent;
return $this->schemaComponent ?? $this->getSchemaComponentContainer()?->getParentComponent();
}

public function getSchemaComponentContainer(): ?Schema
{
return $this->schemaComponentContainer;
}
}
1 change: 1 addition & 0 deletions packages/actions/src/Concerns/CanOpenModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ public function prepareModalAction(Action $action): Action
}

return $action
->schemaComponentContainer($this->getSchemaComponentContainer())
->schemaComponent($this->getSchemaComponent())
->livewire($this->getLivewire())
->when(
Expand Down
9 changes: 5 additions & 4 deletions packages/schemas/src/Concerns/HasComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ function (array $carry, Component | Action $component) use ($containerKey, $with
public function getComponents(bool $withActions = true, bool $withHidden = false, bool $withOriginalKeys = false): array
{
$components = array_map(function (Component | Action $component): Component | Action {
if ($component instanceof Component) {
$component->container($this);
if ($component instanceof Action) {
return $component->schemaComponentContainer($this);
}

return $component;
return $component->container($this);
}, $this->evaluate($this->components));

if ($withActions && $withHidden) {
Expand Down Expand Up @@ -143,10 +143,11 @@ protected function cloneComponents(): static
if (is_array($this->components)) {
$this->components = array_map(
fn (Component | Action $component): Component | Action => match (true) {
$component instanceof Action => (clone $component)
->schemaComponentContainer($this),
$component instanceof Component => $component
->container($this)
->getClone(),
$component instanceof Action => clone $component,
},
$this->components,
);
Expand Down
12 changes: 4 additions & 8 deletions packages/schemas/src/Concerns/HasKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ public function key(string | Closure | null $key): static

public function getKey(bool $isAbsolute = true): ?string
{
if ($isAbsolute && $this->hasCachedAbsoluteKey) {
return $this->cachedAbsoluteKey;
}

$key = $this->evaluate($this->key) ?? $this->getStatePath(isAbsolute: false);

if (! $isAbsolute) {
return $key;
}

if ($this->hasCachedAbsoluteKey) {
return $this->cachedAbsoluteKey;
}

if (blank($key)) {
return $this->cacheAbsoluteKey(null);
}

$keyComponents = [];

if (filled($parentComponentInheritanceKey = $this->getParentComponent()?->getInheritanceKey())) {
Expand Down

0 comments on commit c9e9afe

Please sign in to comment.