Skip to content

Commit

Permalink
Allow actions in schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Jan 16, 2025
1 parent 70cbf05 commit fe1bcda
Show file tree
Hide file tree
Showing 62 changed files with 263 additions and 292 deletions.
12 changes: 1 addition & 11 deletions packages/actions/src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Closure;
use Filament\Actions\Concerns\HasTooltip;
use Filament\Schemas\Components\Actions\ActionContainer;
use Filament\Schemas\Components\Actions\ActionContainer as InfolistActionContainer;
use Filament\Support\Components\ViewComponent;
use Filament\Support\Concerns\HasBadge;
use Filament\Support\Concerns\HasColor;
Expand Down Expand Up @@ -495,7 +494,7 @@ public function clearRecordAfter(): void
$this->record(null);
}

public function toFormComponent(): ActionContainer
public function toSchemaComponent(): ActionContainer
{
$component = ActionContainer::make($this);

Expand All @@ -504,15 +503,6 @@ public function toFormComponent(): ActionContainer
return $component;
}

public function toInfolistComponent(): InfolistActionContainer
{
$component = InfolistActionContainer::make($this);

$this->schemaComponent($component);

return $component;
}

/**
* @param array<string, mixed> $parameters
*/
Expand Down
11 changes: 0 additions & 11 deletions packages/actions/src/Concerns/CanOpenModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,6 @@ public function getModalAction(string $name): ?Action
return $this->getModalActions()[$name] ?? null;
}

public function getMountableModalAction(string $name): ?Action
{
$action = $this->getModalAction($name);

if (! $action instanceof Action) {
return null;
}

return $action;
}

public function prepareModalAction(Action $action): Action
{
if (! $action instanceof Action) {
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/Concerns/HasForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Actions\Concerns;

use Closure;
use Filament\Actions\Action;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Schema;

Expand Down Expand Up @@ -36,7 +37,7 @@ public function disabledForm(bool | Closure $condition = true): static
}

/**
* @param array<Component> | Closure | null $form
* @param array<Component| Action> | Closure | null $form
*/
public function form(array | Closure | null $form): static
{
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/Concerns/HasInfolist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Filament\Actions\Concerns;

use Closure;
use Filament\Actions\Action;
use Filament\Schemas\Components\Component;

trait HasInfolist
{
/**
* @param array<Component> | Closure | null $infolist
* @param array<Component | Action> | Closure | null $infolist
*/
public function infolist(array | Closure | null $infolist): static
{
Expand Down
7 changes: 4 additions & 3 deletions packages/actions/src/Concerns/HasSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
namespace Filament\Actions\Concerns;

use Closure;
use Filament\Actions\Action;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Components\Wizard;
use Filament\Schemas\Schema;

trait HasSchema
{
/**
* @var array<Component> | Closure | null
* @var array<Component | Action> | Closure | null
*/
protected array | Closure | null $schema = null;

protected bool | Closure $isSchemaDisabled = false;

/**
* @param array<Component> | Closure | null $schema
* @param array<Component | Action> | Closure | null $schema
*/
public function components(array | Closure | null $schema): static
{
Expand All @@ -27,7 +28,7 @@ public function components(array | Closure | null $schema): static
}

/**
* @param array<Component> | Closure | null $schema
* @param array<Component | Action> | Closure | null $schema
*/
public function schema(array | Closure | null $schema): static
{
Expand Down
22 changes: 1 addition & 21 deletions packages/actions/src/Concerns/InteractsWithActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ protected function resolveAction(array $action, array $parentActions): Action
{
if (count($parentActions)) {
$parentAction = Arr::last($parentActions);
$resolvedAction = $parentAction->getMountableModalAction($action['name']);
$resolvedAction = $parentAction->getModalAction($action['name']);

if (! $resolvedAction) {
throw new ActionNotResolvableException("Action [{$action['name']}] was not found for action [{$parentAction->getName()}].");
Expand Down Expand Up @@ -536,26 +536,6 @@ public function getAction(string | array $actions): ?Action
return Arr::last($this->resolveActions($actions));
}

/**
* @param array<string> $modalActionNames
*/
protected function getMountableModalActionFromAction(Action $action, array $modalActionNames): ?Action
{
foreach ($modalActionNames as $modalActionName) {
$action = $action->getMountableModalAction($modalActionName);

if (! $action) {
return null;
}
}

if (! $action instanceof Action) {
return null;
}

return $action;
}

protected function getMountedActionSchema(?int $actionNestingIndex = null, ?Action $mountedAction = null): ?Schema
{
$actionNestingIndex ??= array_key_last($this->mountedActions);
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/Exports/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Actions\Exports;

use Carbon\CarbonInterface;
use Filament\Actions\Action;
use Filament\Actions\Exports\Enums\ExportFormat;
use Filament\Actions\Exports\Models\Export;
use Filament\Schemas\Components\Component;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function __invoke(Model $record): array
abstract public static function getColumns(): array;

/**
* @return array<Component>
* @return array<Component | Action>
*/
public static function getOptionsFormComponents(): array
{
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/Imports/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Actions\Imports;

use Carbon\CarbonInterface;
use Filament\Actions\Action;
use Filament\Actions\Imports\Models\Import;
use Filament\Schemas\Components\Component;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -280,7 +281,7 @@ public function saveRecord(): void
abstract public static function getColumns(): array;

/**
* @return array<Component>
* @return array<Component | Action>
*/
public static function getOptionsFormComponents(): array
{
Expand Down
4 changes: 2 additions & 2 deletions packages/forms/src/Components/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function getAddAction(): Action
]))
->modalSubmitActionLabel(__('filament-forms::components.builder.actions.add.modal.actions.add.label'))
->form(function (array $arguments, Builder $component): array {
return $component->getBlock($arguments['block'])->getChildComponents();
return $component->getBlock($arguments['block'])->getDefaultChildComponents();
});
}

Expand Down Expand Up @@ -282,7 +282,7 @@ public function getAddBetweenAction(): Action
]))
->modalSubmitActionLabel(__('filament-forms::components.builder.actions.add_between.modal.actions.add.label'))
->form(function (array $arguments, Builder $component): array {
return $component->getBlock($arguments['block'])->getChildComponents();
return $component->getBlock($arguments['block'])->getDefaultChildComponents();
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/forms/src/Components/MorphToSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Exception;
use Filament\Actions\Action;
use Filament\Forms\Components\MorphToSelect\Type;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Components\Utilities\Set;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static function getDefaultName(): ?string
}

/**
* @return array<Component>
* @return array<Component | Action>
*/
public function getDefaultChildComponents(): array
{
Expand Down
4 changes: 4 additions & 0 deletions packages/forms/src/Components/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Filament\Actions\Action;
use Filament\Forms\Contracts\HasForms;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Components\Concerns\CanBeCollapsed;
use Filament\Schemas\Components\Concerns\HasContainerGridLayout;
use Filament\Schemas\Components\Contracts\CanConcealComponents;
Expand Down Expand Up @@ -795,6 +796,9 @@ public function inset(bool | Closure $condition = true): static
return $this;
}

/**
* @return array<Component | Action>
*/
public function getDefaultChildComponents(): array
{
if ($simpleField = $this->getSimpleField()) {
Expand Down
14 changes: 7 additions & 7 deletions packages/forms/src/Components/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Select extends Field implements Contracts\CanDisableOptions, Contracts\Has
protected string $view = 'filament-forms::components.select';

/**
* @var array<Component> | Closure | null
* @var array<Component | Action> | Closure | null
*/
protected array | Closure | null $createOptionActionForm = null;

Expand All @@ -78,7 +78,7 @@ class Select extends Field implements Contracts\CanDisableOptions, Contracts\Has
protected ?Closure $modifyManageOptionActionsUsing = null;

/**
* @var array<Component> | Closure | null
* @var array<Component | Action> | Closure | null
*/
protected array | Closure | null $editOptionActionForm = null;

Expand Down Expand Up @@ -231,7 +231,7 @@ public function manageOptionActions(?Closure $callback): static
}

/**
* @param array<Component> | Closure | null $schema
* @param array<Component | Action> | Closure | null $schema
*/
public function manageOptionForm(array | Closure | null $schema): static
{
Expand All @@ -242,7 +242,7 @@ public function manageOptionForm(array | Closure | null $schema): static
}

/**
* @param array<Component> | Closure | null $schema
* @param array<Component | Action> | Closure | null $schema
*/
public function createOptionForm(array | Closure | null $schema): static
{
Expand Down Expand Up @@ -362,7 +362,7 @@ public function editOptionAction(?Closure $callback): static
}

/**
* @return array<Component> | Schema | null
* @return array<Component | Action> | Schema | null
*/
public function getCreateOptionActionForm(Schema $form): array | Schema | null
{
Expand All @@ -375,7 +375,7 @@ public function hasCreateOptionActionFormSchema(): bool
}

/**
* @return array<Component> | Schema | null
* @return array<Component | Action> | Schema | null
*/
public function getEditOptionActionForm(Schema $form): array | Schema | null
{
Expand All @@ -388,7 +388,7 @@ public function hasEditOptionActionFormSchema(): bool
}

/**
* @param array<Component> | Closure | null $schema
* @param array<Component | Action> | Closure | null $schema
*/
public function editOptionForm(array | Closure | null $schema): static
{
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/Concerns/InteractsWithForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function getFormModel(): Model | string | null
/**
* @deprecated Override the `form()` method to configure the default form.
*
* @return array<Component>
* @return array<Component | Action>
*/
protected function getFormSchema(): array
{
Expand Down
15 changes: 8 additions & 7 deletions packages/forms/src/Testing/TestsForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Forms\Testing;

use Closure;
use Filament\Actions\Action;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Contracts\HasForms;
Expand Down Expand Up @@ -180,7 +181,7 @@ public function assertFormComponentExists(): Closure
/** @var Schema $form */
$form = $this->instance()->{$formName};

/** @var ?Component $component */
/** @var Component | Action | null $component */
$component = $form->getFlatComponents(withHidden: true)[$componentKey] ?? null;

$livewireClass = $this->instance()::class;
Expand Down Expand Up @@ -453,10 +454,10 @@ public function assertWizardStepExists(): Closure
$form = $this->instance()->{$formName};

/** @var Wizard $wizard */
$wizard = $form->getComponent(fn (Component $component): bool => $component instanceof Wizard);
$wizard = $form->getComponent(fn (Component | Action $component): bool => $component instanceof Wizard);
Assert::assertArrayHasKey(
$step - 1,
$wizard->getChildComponents(),
$wizard->getDefaultChildComponents(),
"Wizard does not have a step {$step}."
);

Expand All @@ -474,7 +475,7 @@ public function assertWizardCurrentStep(): Closure
$form = $this->instance()->{$formName};

/** @var Wizard $wizard */
$wizard = $form->getComponent(fn (Component $component): bool => $component instanceof Wizard);
$wizard = $form->getComponent(fn (Component | Action $component): bool => $component instanceof Wizard);
Assert::assertEquals(
$step,
$current = $wizard->getCurrentStepIndex() + 1,
Expand All @@ -495,7 +496,7 @@ public function goToWizardStep(): Closure
$form = $this->instance()->{$formName};

/** @var Wizard $wizard */
$wizard = $form->getComponent(fn (Component $component): bool => $component instanceof Wizard);
$wizard = $form->getComponent(fn (Component | Action $component): bool => $component instanceof Wizard);

$stepIndex = ($step <= 1) ? 0 : $step - 2;

Expand All @@ -515,7 +516,7 @@ public function goToNextWizardStep(): Closure
$form = $this->instance()->{$formName};

/** @var Wizard $wizard */
$wizard = $form->getComponent(fn (Component $component): bool => $component instanceof Wizard);
$wizard = $form->getComponent(fn (Component | Action $component): bool => $component instanceof Wizard);

$this->call('callSchemaComponentMethod', $wizard->getKey(), 'nextStep', [$wizard->getCurrentStepIndex()]);

Expand All @@ -533,7 +534,7 @@ public function goToPreviousWizardStep(): Closure
$form = $this->instance()->{$formName};

/** @var Wizard $wizard */
$wizard = $form->getComponent(fn (Component $component): bool => $component instanceof Wizard);
$wizard = $form->getComponent(fn (Component | Action $component): bool => $component instanceof Wizard);

$this->call('callSchemaComponentMethod', $wizard->getKey(), 'previousStep', [$wizard->getCurrentStepIndex()]);

Expand Down
Loading

0 comments on commit fe1bcda

Please sign in to comment.