| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Forms\Controls; |
| 9 |
|
| 10 |
use Packetery\Nette\Utils\Html; |
| 11 |
/** |
| 12 |
* Push button control with no default behavior. |
| 13 |
*/ |
| 14 |
class Button extends BaseControl |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @param string|object $caption |
| 18 |
*/ |
| 19 |
public function __construct($caption = null) |
| 20 |
{ |
| 21 |
parent::__construct($caption); |
| 22 |
$this->control->type = 'button'; |
| 23 |
$this->setOption('type', 'button'); |
| 24 |
} |
| 25 |
/** |
| 26 |
* Is button pressed? |
| 27 |
*/ |
| 28 |
public function isFilled() : bool |
| 29 |
{ |
| 30 |
$value = $this->getValue(); |
| 31 |
return $value !== null && $value !== []; |
| 32 |
} |
| 33 |
/** |
| 34 |
* Bypasses label generation. |
| 35 |
*/ |
| 36 |
public function getLabel($caption = null) |
| 37 |
{ |
| 38 |
return null; |
| 39 |
} |
| 40 |
/** @return static */ |
| 41 |
public function renderAsButton(bool $state = \true) |
| 42 |
{ |
| 43 |
$this->control->setName($state ? 'button' : 'input'); |
| 44 |
return $this; |
| 45 |
} |
| 46 |
/** |
| 47 |
* Generates control's HTML element. |
| 48 |
* @param string|object $caption |
| 49 |
*/ |
| 50 |
public function getControl($caption = null) : Html |
| 51 |
{ |
| 52 |
$this->setOption('rendered', \true); |
| 53 |
$caption = $this->translate($caption ?? $this->getCaption()); |
| 54 |
$el = (clone $this->control)->addAttributes(['name' => $this->getHtmlName(), 'disabled' => $this->isDisabled()]); |
| 55 |
if ($caption instanceof Html || $caption !== null && $el->getName() === 'button') { |
| 56 |
$el->setName('button')->setText($caption); |
| 57 |
} else { |
| 58 |
$el->value = $caption; |
| 59 |
} |
| 60 |
return $el; |
| 61 |
} |
| 62 |
} |
| 63 |
|