Browse Source

Added `submenuOptions` support at `yii\bootstrap\Dropdown`

tags/2.0.6
Klimov Paul 9 years ago
parent
commit
f387e6ebfd
  1. 1
      CHANGELOG.md
  2. 22
      Dropdown.php
  3. 42
      tests/DropdownTest.php

1
CHANGELOG.md

@ -6,6 +6,7 @@ Yii Framework 2 bootstrap extension Change Log
- Enh #38: Added object support for `content` option in `Collapse` class (pana1990, ItsReddi)
- Enh #40: Added `visible` option to `yii\bootstrap\Tab` widget items (klimov-paul)
- Enh #41: Added `submenuOptions` support at `yii\bootstrap\Dropdown` (spikyjt, klimov-paul)
2.0.4 May 10, 2015
------------------

22
Dropdown.php

@ -48,6 +48,8 @@ class Dropdown extends Widget
* - options: array, optional, the HTML attributes of the item.
* - items: array, optional, the submenu items. The structure is the same as this property.
* Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
* - submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be
* merged with [[submenuOptions]].
*
* To insert divider use `<li role="presentation" class="divider"></li>`.
*/
@ -56,6 +58,12 @@ class Dropdown extends Widget
* @var boolean whether the labels for header items should be HTML-encoded.
*/
public $encodeLabels = true;
/**
* @var array|null the HTML attributes for sub-menu container tags.
* If not set - [[options]] value will be used for it.
* @since 2.0.5
*/
public $submenuOptions;
/**
@ -64,6 +72,12 @@ class Dropdown extends Widget
*/
public function init()
{
if ($this->submenuOptions === null) {
// copying of [[options]] kept for BC
// @todo separate [[submenuOptions]] from [[options]] completely before 2.1 release
$this->submenuOptions = $this->options;
unset($this->submenuOptions['id']);
}
parent::init();
Html::addCssClass($this->options, 'dropdown-menu');
}
@ -88,7 +102,7 @@ class Dropdown extends Widget
protected function renderItems($items, $options = [])
{
$lines = [];
foreach ($items as $i => $item) {
foreach ($items as $item) {
if (isset($item['visible']) && !$item['visible']) {
continue;
}
@ -113,8 +127,10 @@ class Dropdown extends Widget
$content = Html::a($label, $url, $linkOptions);
}
} else {
$submenuOptions = $options;
unset($submenuOptions['id']);
$submenuOptions = $this->submenuOptions;
if (isset($item['submenuOptions'])) {
$submenuOptions = array_merge($submenuOptions, $item['submenuOptions']);
}
$content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
. $this->renderItems($item['items'], $submenuOptions);
Html::addCssClass($itemOptions, 'dropdown-submenu');

42
tests/DropdownTest.php

@ -41,10 +41,50 @@ class DropdownTest extends TestCase
$expected = <<<EXPECTED
<ul id="w0" class="dropdown-menu"><li class="dropdown-header">Page1</li>
<li class="dropdown-submenu"><a href="#" tabindex="-1">Dropdown1</a><ul class="dropdown-menu"><li class="dropdown-header">Page2</li>
<li class="dropdown-submenu"><a href="#" tabindex="-1">Dropdown1</a><ul><li class="dropdown-header">Page2</li>
<li class="dropdown-header">Page3</li></ul></li></ul>
EXPECTED;
$this->assertEqualsWithoutLE($expected, $out);
}
public function testSubMenuOptions()
{
Dropdown::$counter = 0;
$out = Dropdown::widget(
[
'submenuOptions' => [
'class' => 'submenu-list',
],
'items' => [
[
'label' => 'Dropdown1',
'items' => [
['label' => 'Page1', 'content' => 'Page2'],
['label' => 'Page2', 'content' => 'Page3'],
]
],
[
'label' => 'Dropdown2',
'items' => [
['label' => 'Page3', 'content' => 'Page4'],
['label' => 'Page4', 'content' => 'Page5'],
],
'submenuOptions' => [
'class' => 'submenu-override',
],
]
]
]
);
$expected = <<<EXPECTED
<ul id="w0" class="dropdown-menu"><li class="dropdown-submenu"><a href="#" tabindex="-1">Dropdown1</a><ul class="submenu-list"><li class="dropdown-header">Page1</li>
<li class="dropdown-header">Page2</li></ul></li>
<li class="dropdown-submenu"><a href="#" tabindex="-1">Dropdown2</a><ul class="submenu-override"><li class="dropdown-header">Page3</li>
<li class="dropdown-header">Page4</li></ul></li></ul>
EXPECTED;
$this->assertEqualsWithoutLE($expected, $out);
}
}

Loading…
Cancel
Save