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. * @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. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/ */
public $titleOptions = []; public $titleOptions = [];
@ -142,8 +147,9 @@ class Offcanvas extends Widget
{ {
$button = $this->renderCloseButton(); $button = $this->renderCloseButton();
if (isset($this->title)) { if (isset($this->title)) {
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'h5');
Html::addCssClass($this->titleOptions, ['widget' => 'offcanvas-title']); Html::addCssClass($this->titleOptions, ['widget' => 'offcanvas-title']);
$header = Html::tag('h5', $this->title, $this->titleOptions); $header = Html::tag($tag, $this->title, $this->titleOptions);
} else { } else {
$header = ''; $header = '';
} }

View File

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