Skip to content

Commit

Permalink
Merge pull request #1343 from moonshine-software/has-one-modifiers
Browse files Browse the repository at this point in the history
feat: HasOne
  • Loading branch information
lee-to authored Nov 26, 2024
2 parents f9d48e1 + 9eb7d33 commit 856a134
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
65 changes: 60 additions & 5 deletions src/Laravel/src/Fields/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use MoonShine\Contracts\Core\DependencyInjection\FieldsContract;
use MoonShine\Contracts\UI\ComponentContract;
use MoonShine\Contracts\UI\FieldContract;
use MoonShine\Contracts\UI\FormBuilderContract;
use MoonShine\Contracts\UI\HasFieldsContract;
use MoonShine\Contracts\UI\TableBuilderContract;
use MoonShine\Laravel\Collections\Fields;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Components\FormBuilder;
Expand Down Expand Up @@ -48,6 +50,12 @@ class HasOne extends ModelRelationField implements HasFieldsContract

protected bool $isAsync = true;

protected ?Closure $redirectAfter = null;

protected ?Closure $modifyForm = null;

protected ?Closure $modifyTable = null;

public function hasWrapper(): bool
{
return false;
Expand Down Expand Up @@ -115,6 +123,10 @@ protected function resolvePreview(): Renderable|string
->preview()
->simple()
->vertical()
->when(
! \is_null($this->modifyTable),
fn (TableBuilderContract $tableBuilder) => value($this->modifyTable, $tableBuilder)
)
->render();
}

Expand All @@ -139,6 +151,47 @@ private function getFieldsOnPreview(): Closure
};
}

/**
* @param Closure(int $parentId, static $field): string $callback
*/
public function redirectAfter(Closure $callback): static
{
$this->redirectAfter = $callback;

return $this;
}

public function getRedirectAfter(Model|int|null|string $parentId): string
{
if (! \is_null($this->redirectAfter)) {
return (string) value($this->redirectAfter, $parentId, $this);
}

return moonshineRequest()
->getResource()
?->getFormPageUrl($parentId) ?? '';
}

/**
* @param Closure(FormBuilderContract $table): FormBuilderContract $callback
*/
public function modifyForm(Closure $callback): static
{
$this->modifyForm = $callback;

return $this;
}

/**
* @param Closure(TableBuilderContract $table): TableBuilderContract $callback
*/
public function modifyTable(Closure $callback): static
{
$this->modifyTable = $callback;

return $this;
}

/**
* @throws Throwable
* @throws FieldException
Expand Down Expand Up @@ -171,10 +224,8 @@ protected function getComponent(): FormBuilder
$item?->getKey()
);

$redirectAfter = toPage(
page: $parentResource->getFormPage(),
resource: $parentResource,
params: ['resourceItem' => $parentItem->getKey()]
$redirectAfter = $this->getRedirectAfter(
$parentItem->getKey()
);

$isAsync = ! \is_null($item) && ($this->isAsync() || $resource->isAsync());
Expand Down Expand Up @@ -231,7 +282,11 @@ protected function getComponent(): FormBuilder
&& $element->isToOne()
&& $element->getColumn() === $relation->getForeignKeyName()
))
->submit(__('moonshine::ui.save'), ['class' => 'btn-primary btn-lg']);
->submit(__('moonshine::ui.save'), ['class' => 'btn-primary btn-lg'])
->when(
! \is_null($this->modifyForm),
fn (FormBuilderContract $form) => value($this->modifyForm, $form)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Support/src/Traits/WithComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function xDataMethod(string $method, ...$parameters): static
public function xModel(?string $column = null): static
{
if ($this instanceof FieldContract) {
return $this->x('model', $this->getColumn());
return $this->x('model', $column ?? $this->getColumn());
}

return $this->x('model', $column);
Expand Down

0 comments on commit 856a134

Please sign in to comment.