Browse Source

Added `yii\bootstrap\Collapse::$itemToggleOptions`

tags/2.0.8
Klimov Paul 7 years ago
parent
commit
1db3f49f23
  1. 1
      CHANGELOG.md
  2. 33
      Collapse.php
  3. 37
      tests/CollapseTest.php

1
CHANGELOG.md

@ -7,6 +7,7 @@ Yii Framework 2 bootstrap extension Change Log
- Bug #126: Fixed `yii\bootstrap\ToggleButtonGroup` toggles dropdown for both buttons in case `split` is enabled (klimov-paul)
- Enh #219: Add ability to use custom HTML in navbar-header (razvanphp)
- Enh #171: Add ability to use a brandImage with the navbar (razvanphp)
- Enh #227: Added `yii\bootstrap\Collapse::$itemToggleOptions` allowing setup custom collapse tag name and HTML options (mskayali, klimov-paul)
2.0.7 October 09, 2017

33
Collapse.php

@ -96,6 +96,20 @@ class Collapse extends Widget
* @since 2.0.7
*/
public $autoCloseItems = true;
/**
* @var string the HTML options for the item toggle tag. Key 'tag' might be used here for the tag name specification.
* For example:
*
* ```php
* [
* 'tag' => 'div',
* 'class' => 'custom-toggle',
* ]
* ```
*
* @since 2.0.8
*/
public $itemToggleOptions = [];
/**
@ -170,14 +184,23 @@ class Collapse extends Widget
$header = Html::encode($header);
}
$headerOptions = [
'class' => 'collapse-toggle',
$itemToggleOptions = array_merge([
'tag' => 'a',
'data-toggle' => 'collapse',
];
], $this->itemToggleOptions);
Html::addCssClass($itemToggleOptions, ['widget' => 'collapse-toggle']);
if ($this->autoCloseItems) {
$headerOptions['data-parent'] = '#' . $this->options['id'];
$itemToggleOptions['data-parent'] = '#' . $this->options['id'];
}
$itemToggleTag = ArrayHelper::remove($itemToggleOptions, 'tag', 'a');
if ($itemToggleTag === 'a') {
$headerToggle = Html::a($header, '#' . $id, $itemToggleOptions) . "\n";
} else {
$itemToggleOptions['data-target'] = '#' . $id;
$headerToggle = Html::tag($itemToggleTag, $header, $itemToggleOptions) . "\n";
}
$headerToggle = Html::a($header, '#' . $id, $headerOptions) . "\n";
$header = Html::tag('h4', $headerToggle, ['class' => 'panel-title']);

37
tests/CollapseTest.php

@ -238,4 +238,41 @@ HTML
]);
$this->assertNotContains('data-parent="', $output);
}
/**
* @depends testRender
*/
public function testItemToggleTag()
{
$items = [
[
'label' => 'Item 1',
'content' => 'Content 1',
],
[
'label' => 'Item 2',
'content' => 'Content 2',
],
];
$output = Collapse::widget([
'items' => $items,
'itemToggleOptions' => [
'tag' => 'span',
'class' => 'custom-toggle',
],
]);
$this->assertContains('<h4 class="panel-title"><span class="custom-toggle collapse-toggle" data-toggle="collapse" ', $output);
$this->assertNotContains('<a', $output);
$output = Collapse::widget([
'items' => $items,
'itemToggleOptions' => [
'tag' => 'span',
'class' => ['widget' => 'custom-toggle'],
],
]);
$this->assertContains('<h4 class="panel-title"><span class="custom-toggle" data-toggle="collapse" ', $output);
$this->assertNotContains('collapse-toggle', $output);
}
}

Loading…
Cancel
Save