yii2-bootstrap5/src/Button.php

69 lines
1.4 KiB
PHP
Raw Normal View History

2021-02-10 05:04:59 +08:00
<?php
2021-08-07 05:38:53 +08:00
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
2021-02-10 05:04:59 +08:00
declare(strict_types=1);
namespace yii\bootstrap5;
use yii\base\InvalidConfigException;
2021-02-10 05:04:59 +08:00
/**
* Button renders a bootstrap button.
*
* For example,
*
* ```php
* echo Button::widget([
* 'label' => 'Action',
* 'options' => ['class' => 'btn-lg'],
* ]);
* ```
2021-08-05 15:21:05 +08:00
* @see https://getbootstrap.com/docs/5.1/components/buttons/
2021-02-10 05:04:59 +08:00
* @author Antonio Ramirez <amigo.cobos@gmail.com>
*/
class Button extends Widget
{
/**
* @var string the tag to use to render the button
*/
public $tagName = 'button';
2021-02-10 05:04:59 +08:00
/**
* @var string the button label
*/
public $label = 'Button';
2021-02-10 05:04:59 +08:00
/**
* @var bool whether the label should be HTML-encoded.
*/
public $encodeLabel = true;
2021-02-10 05:04:59 +08:00
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
* @throws InvalidConfigException
2021-02-10 05:04:59 +08:00
*/
public function init()
{
parent::init();
$this->clientOptions = [];
Html::addCssClass($this->options, ['widget' => 'btn']);
}
/**
* {@inheritdoc}
* @return string
*/
public function run(): string
{
2021-07-12 23:22:29 +08:00
return Html::tag(
$this->tagName,
$this->encodeLabel ? Html::encode($this->label) : $this->label,
$this->options
);
2021-02-10 05:04:59 +08:00
}
}