allow override of title tag, added some more options to tests

This commit is contained in:
Simon Karlen 2021-08-04 10:03:47 +02:00
parent 5fbad0fa2f
commit 6ba5b34c2c
No known key found for this signature in database
GPG Key ID: 0630C27D666EBCC3
2 changed files with 17 additions and 6 deletions

View File

@ -97,6 +97,11 @@ class Offcanvas extends Widget
/**
* @var array Additional title options.
*
* The following special options are supported:
*
* - tag: string, the tag name of the button. Defaults to 'h5'.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $titleOptions = [];
@ -142,8 +147,9 @@ class Offcanvas extends Widget
{
$button = $this->renderCloseButton();
if (isset($this->title)) {
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'h5');
Html::addCssClass($this->titleOptions, ['widget' => 'offcanvas-title']);
$header = Html::tag('h5', $this->title, $this->titleOptions);
$header = Html::tag($tag, $this->title, $this->titleOptions);
} else {
$header = '';
}

View File

@ -2,8 +2,6 @@
namespace yiiunit\extensions\bootstrap5;
use yii\bootstrap5\Html;
use yii\bootstrap5\Modal;
use yii\bootstrap5\Offcanvas;
/**
@ -42,6 +40,13 @@ HTML;
ob_start();
Offcanvas::begin([
'title' => 'Offcanvas title',
'headerOptions' => [
'data-test' => 'Test'
],
'titleOptions' => [
'tag' => 'h2'
],
'placement' => Offcanvas::PLACEMENT_END,
'backdrop' => false,
'scrolling' => true,
'closeButton' => false
@ -52,9 +57,9 @@ HTML;
$expected = <<<HTML
<div id="w0" class="offcanvas offcanvas-start" tabindex="-1" data-bs-backdrop="false" data-bs-scroll="true" aria-labelledby="w0-label">
<div class="offcanvas-header">
<h5 id="w0-label" class="offcanvas-title">Offcanvas title</h5>
<div id="w0" class="offcanvas offcanvas-end" tabindex="-1" data-bs-backdrop="false" data-bs-scroll="true" aria-labelledby="w0-label">
<div class="offcanvas-header" data-test="Test">
<h2 id="w0-label" class="offcanvas-title">Offcanvas title</h2>
</div>
<div class="offcanvas-body">
<p>Woohoo, you're reading this text in an offcanvas!</p>