diff --git a/src/UI/Actions/Controls/Button.php b/src/UI/Actions/Controls/Button.php index 89c4451..7e0307b 100644 --- a/src/UI/Actions/Controls/Button.php +++ b/src/UI/Actions/Controls/Button.php @@ -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 @@ -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 ?? '#'); } diff --git a/src/UI/Actions/Traits/Actions.php b/src/UI/Actions/Traits/Actions.php index 86b610a..96bf04c 100644 --- a/src/UI/Actions/Traits/Actions.php +++ b/src/UI/Actions/Traits/Actions.php @@ -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; @@ -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); } @@ -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;