Skip to content

Commit

Permalink
Improve Link creation
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Mar 27, 2024
1 parent 770434b commit 394b2ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
12 changes: 3 additions & 9 deletions src/UI/Actions/Controls/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
use JuniWalk\Utils\Strings;
use JuniWalk\Utils\UI\Actions\Action;
use JuniWalk\Utils\UI\Actions\Component;
use JuniWalk\Utils\UI\Actions\LinkProvider;
use JuniWalk\Utils\UI\Actions\Traits\Control;
use Nette\Application\UI\Control as UIControl;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\Link;

class Button extends UIControl implements Action, Component
Expand All @@ -26,29 +24,25 @@ class Button extends UIControl implements Action, Component
public function __construct(
private string $name,
private ?string $label = null,
string $dest,
array $args = [],
) {
$this->name = Strings::webalize($name);
$this->control = Html::el('a');

$this->setParent(null, $this->name);
$this->monitor(Presenter::class, function() use ($dest, $args) {
$this->link = $this->lookup(LinkProvider::class)->createLink($dest, $args);
});
}


public function setLink(Link|string $link): void
public function setLink(Link|string $link): static
{
$this->link = $link;
return $this;
}


public function create(): Html
{
$label = $this->translator?->translate($this->label) ?? $this->label;
return $this->getControl()->addHtml($label)->setHref($this->link);
return $this->getControl()->addHtml($label)->setHref($this->link ?? '#');
}


Expand Down
17 changes: 10 additions & 7 deletions src/UI/Actions/Traits/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use JuniWalk\Utils\Strings;
use JuniWalk\Utils\UI\Actions\Action;
use JuniWalk\Utils\UI\Actions\Component;
use JuniWalk\Utils\UI\Actions\LinkProvider;
use JuniWalk\Utils\UI\Actions\Controls\Button;
use JuniWalk\Utils\UI\Actions\Controls\Divider;
use JuniWalk\Utils\UI\Actions\Controls\Dropdown;
Expand All @@ -26,9 +27,13 @@ public function addGroup(?string $name = null): Action
}


public function addButton(string $name, string $label = null, string $link = null, array $args = []): Action
public function addButton(string $name, string $label = null, string $dest = null, array $args = []): Action
{
$action = new Button($name, $label, $link ?? $name, $args);
$action = new Button($name, $label);
$action->monitor(Presenter::class, fn() => $action->setLink(
$action->lookup(LinkProvider::class)->createLink($dest ?? $name, $args)
));

return $this->addAction($action);
}

Expand All @@ -49,11 +54,9 @@ public function addDivider(?string $name = null): Action

public function addAction(Action $action): Action
{
$action->monitor(Presenter::class, function(Presenter $presenter) use ($action) {
if ($action instanceof Component) {
$action->setTranslator($presenter->getTranslator());
}
});
if ($action instanceof Component) {
$action->monitor(Presenter::class, fn($parent) => $action->setTranslator($parent->getTranslator()));
}

$this->addComponent($action, null);
return $action;
Expand Down

0 comments on commit 394b2ec

Please sign in to comment.