Browse Source

Allow override CSS classes added by widgets

tags/2.0.5
Klimov Paul 10 years ago
parent
commit
3ec5987344
  1. 4
      Alert.php
  2. 2
      Button.php
  3. 8
      ButtonDropdown.php
  4. 2
      ButtonGroup.php
  5. 4
      Carousel.php
  6. 6
      Collapse.php
  7. 6
      Dropdown.php
  8. 6
      Modal.php
  9. 6
      Nav.php
  10. 13
      NavBar.php
  11. 4
      Progress.php
  12. 10
      Tabs.php
  13. 37
      docs/guide/usage-widgets.md

4
Alert.php

@ -135,9 +135,7 @@ class Alert extends Widget
*/ */
protected function initOptions() protected function initOptions()
{ {
Html::addCssClass($this->options, 'alert'); Html::addCssClass($this->options, ['alert', 'fade', 'in']);
Html::addCssClass($this->options, 'fade');
Html::addCssClass($this->options, 'in');
if ($this->closeButton !== false) { if ($this->closeButton !== false) {
$this->closeButton = array_merge([ $this->closeButton = array_merge([

2
Button.php

@ -46,7 +46,7 @@ class Button extends Widget
{ {
parent::init(); parent::init();
$this->clientOptions = false; $this->clientOptions = false;
Html::addCssClass($this->options, 'btn'); Html::addCssClass($this->options, ['widget' => 'btn']);
} }
/** /**

8
ButtonDropdown.php

@ -75,7 +75,7 @@ class ButtonDropdown extends Widget
public function run() public function run()
{ {
// @todo use [[options]] instead of [[containerOptions]] and introduce [[buttonOptions]] before 2.1 release // @todo use [[options]] instead of [[containerOptions]] and introduce [[buttonOptions]] before 2.1 release
Html::addCssClass($this->containerOptions, 'btn-group'); Html::addCssClass($this->containerOptions, ['widget' => 'btn-group']);
$options = $this->containerOptions; $options = $this->containerOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div'); $tag = ArrayHelper::remove($options, 'tag', 'div');
@ -94,7 +94,7 @@ class ButtonDropdown extends Widget
*/ */
protected function renderButton() protected function renderButton()
{ {
Html::addCssClass($this->options, 'btn'); Html::addCssClass($this->options, ['widget' => 'btn']);
$label = $this->label; $label = $this->label;
if ($this->encodeLabel) { if ($this->encodeLabel) {
$label = Html::encode($label); $label = Html::encode($label);
@ -102,7 +102,7 @@ class ButtonDropdown extends Widget
if ($this->split) { if ($this->split) {
$options = $this->options; $options = $this->options;
$this->options['data-toggle'] = 'dropdown'; $this->options['data-toggle'] = 'dropdown';
Html::addCssClass($this->options, 'dropdown-toggle'); Html::addCssClass($this->options, ['toggle' => 'dropdown-toggle']);
unset($this->options['id']); unset($this->options['id']);
$splitButton = Button::widget([ $splitButton = Button::widget([
'label' => '<span class="caret"></span>', 'label' => '<span class="caret"></span>',
@ -116,7 +116,7 @@ class ButtonDropdown extends Widget
if (!isset($options['href'])) { if (!isset($options['href'])) {
$options['href'] = '#'; $options['href'] = '#';
} }
Html::addCssClass($options, 'dropdown-toggle'); Html::addCssClass($options, ['toggle' => 'dropdown-toggle']);
$options['data-toggle'] = 'dropdown'; $options['data-toggle'] = 'dropdown';
$splitButton = ''; $splitButton = '';
} }

2
ButtonGroup.php

@ -65,7 +65,7 @@ class ButtonGroup extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
Html::addCssClass($this->options, 'btn-group'); Html::addCssClass($this->options, ['widget' => 'btn-group']);
} }
/** /**

4
Carousel.php

@ -72,7 +72,7 @@ class Carousel extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
Html::addCssClass($this->options, 'carousel'); Html::addCssClass($this->options, ['widget' => 'carousel']);
} }
/** /**
@ -149,7 +149,7 @@ class Carousel extends Widget
throw new InvalidConfigException('The "content" option is required.'); throw new InvalidConfigException('The "content" option is required.');
} }
Html::addCssClass($options, 'item'); Html::addCssClass($options, ['widget' => 'item']);
if ($index === 0) { if ($index === 0) {
Html::addCssClass($options, 'active'); Html::addCssClass($options, 'active');
} }

6
Collapse.php

@ -78,7 +78,7 @@ class Collapse extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
Html::addCssClass($this->options, 'panel-group'); Html::addCssClass($this->options, ['widget' => 'panel-group']);
} }
/** /**
@ -109,7 +109,7 @@ class Collapse extends Widget
} }
$header = $item['label']; $header = $item['label'];
$options = ArrayHelper::getValue($item, 'options', []); $options = ArrayHelper::getValue($item, 'options', []);
Html::addCssClass($options, 'panel panel-default'); Html::addCssClass($options, ['panel' => 'panel', 'widget' => 'panel-default']);
$items[] = Html::tag('div', $this->renderItem($header, $item, ++$index), $options); $items[] = Html::tag('div', $this->renderItem($header, $item, ++$index), $options);
} }
@ -130,7 +130,7 @@ class Collapse extends Widget
$id = $this->options['id'] . '-collapse' . $index; $id = $this->options['id'] . '-collapse' . $index;
$options = ArrayHelper::getValue($item, 'contentOptions', []); $options = ArrayHelper::getValue($item, 'contentOptions', []);
$options['id'] = $id; $options['id'] = $id;
Html::addCssClass($options, 'panel-collapse collapse'); Html::addCssClass($options, ['widget' => 'panel-collapse', 'collapse' => 'collapse']);
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
if ($encodeLabel) { if ($encodeLabel) {

6
Dropdown.php

@ -77,7 +77,7 @@ class Dropdown extends Widget
unset($this->submenuOptions['id']); unset($this->submenuOptions['id']);
} }
parent::init(); parent::init();
Html::addCssClass($this->options, 'dropdown-menu'); Html::addCssClass($this->options, ['widget' => 'dropdown-menu']);
} }
/** /**
@ -120,7 +120,7 @@ class Dropdown extends Widget
if (empty($item['items'])) { if (empty($item['items'])) {
if ($url === null) { if ($url === null) {
$content = $label; $content = $label;
Html::addCssClass($itemOptions, 'dropdown-header'); Html::addCssClass($itemOptions, ['widget' => 'dropdown-header']);
} else { } else {
$content = Html::a($label, $url, $linkOptions); $content = Html::a($label, $url, $linkOptions);
} }
@ -131,7 +131,7 @@ class Dropdown extends Widget
} }
$content = Html::a($label, $url === null ? '#' : $url, $linkOptions) $content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
. $this->renderItems($item['items'], $submenuOptions); . $this->renderItems($item['items'], $submenuOptions);
Html::addCssClass($itemOptions, 'dropdown-submenu'); Html::addCssClass($itemOptions, ['widget' => 'dropdown-submenu']);
} }
$lines[] = Html::tag('li', $content, $itemOptions); $lines[] = Html::tag('li', $content, $itemOptions);

6
Modal.php

@ -136,7 +136,7 @@ class Modal extends Widget
$this->header = $button . "\n" . $this->header; $this->header = $button . "\n" . $this->header;
} }
if ($this->header !== null) { if ($this->header !== null) {
Html::addCssClass($this->headerOptions, 'modal-header'); Html::addCssClass($this->headerOptions, ['widget' => 'modal-header']);
return Html::tag('div', "\n" . $this->header . "\n", $this->headerOptions); return Html::tag('div', "\n" . $this->header . "\n", $this->headerOptions);
} else { } else {
return null; return null;
@ -168,7 +168,7 @@ class Modal extends Widget
protected function renderFooter() protected function renderFooter()
{ {
if ($this->footer !== null) { if ($this->footer !== null) {
Html::addCssClass($this->footerOptions, 'modal-footer'); Html::addCssClass($this->footerOptions, ['widget' => 'modal-footer']);
return Html::tag('div', "\n" . $this->footer . "\n", $this->footerOptions); return Html::tag('div', "\n" . $this->footer . "\n", $this->footerOptions);
} else { } else {
return null; return null;
@ -224,7 +224,7 @@ class Modal extends Widget
'role' => 'dialog', 'role' => 'dialog',
'tabindex' => -1, 'tabindex' => -1,
], $this->options); ], $this->options);
Html::addCssClass($this->options, 'modal'); Html::addCssClass($this->options, ['widget' => 'modal']);
if ($this->clientOptions !== false) { if ($this->clientOptions !== false) {
$this->clientOptions = array_merge(['show' => false], $this->clientOptions); $this->clientOptions = array_merge(['show' => false], $this->clientOptions);

6
Nav.php

@ -121,7 +121,7 @@ class Nav extends Widget
if ($this->dropDownCaret === null) { if ($this->dropDownCaret === null) {
$this->dropDownCaret = Html::tag('b', '', ['class' => 'caret']); $this->dropDownCaret = Html::tag('b', '', ['class' => 'caret']);
} }
Html::addCssClass($this->options, 'nav'); Html::addCssClass($this->options, ['widget' => 'nav']);
} }
/** /**
@ -178,8 +178,8 @@ class Nav extends Widget
if ($items !== null) { if ($items !== null) {
$linkOptions['data-toggle'] = 'dropdown'; $linkOptions['data-toggle'] = 'dropdown';
Html::addCssClass($options, 'dropdown'); Html::addCssClass($options, ['widget' => 'dropdown']);
Html::addCssClass($linkOptions, 'dropdown-toggle'); Html::addCssClass($linkOptions, ['widget' => 'dropdown-toggle']);
if ($this->dropDownCaret !== '') { if ($this->dropDownCaret !== '') {
$label .= ' ' . $this->dropDownCaret; $label .= ' ' . $this->dropDownCaret;
} }

13
NavBar.php

@ -93,11 +93,11 @@ class NavBar extends Widget
{ {
parent::init(); parent::init();
$this->clientOptions = false; $this->clientOptions = false;
Html::addCssClass($this->options, 'navbar'); if (empty($this->options['class'])) {
if ($this->options['class'] === 'navbar') { Html::addCssClass($this->options, ['navbar', 'navbar-default']);
Html::addCssClass($this->options, 'navbar-default'); } else {
Html::addCssClass($this->options, ['widget' => 'navbar']);
} }
Html::addCssClass($this->brandOptions, 'navbar-brand');
if (empty($this->options['role'])) { if (empty($this->options['role'])) {
$this->options['role'] = 'navigation'; $this->options['role'] = 'navigation';
} }
@ -116,12 +116,11 @@ class NavBar extends Widget
} }
echo $this->renderToggleButton(); echo $this->renderToggleButton();
if ($this->brandLabel !== false) { if ($this->brandLabel !== false) {
Html::addCssClass($this->brandOptions, 'navbar-brand'); Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']);
echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions); echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
} }
echo Html::endTag('div'); echo Html::endTag('div');
Html::addCssClass($this->containerOptions, 'collapse'); Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']);
Html::addCssClass($this->containerOptions, 'navbar-collapse');
$options = $this->containerOptions; $options = $this->containerOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div'); $tag = ArrayHelper::remove($options, 'tag', 'div');
echo Html::beginTag($tag, $options); echo Html::beginTag($tag, $options);

4
Progress.php

@ -96,7 +96,7 @@ class Progress extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
Html::addCssClass($this->options, 'progress'); Html::addCssClass($this->options, ['widget' => 'progress']);
} }
/** /**
@ -152,7 +152,7 @@ class Progress extends Widget
'style' => "width:{$percent}%", 'style' => "width:{$percent}%",
]; ];
$options = array_merge($defaultOptions, $options); $options = array_merge($defaultOptions, $options);
Html::addCssClass($options, 'progress-bar'); Html::addCssClass($options, ['widget' => 'progress-bar']);
$out = Html::beginTag('div', $options); $out = Html::beginTag('div', $options);
$out .= $label; $out .= $label;

10
Tabs.php

@ -122,7 +122,7 @@ class Tabs extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
Html::addCssClass($this->options, 'nav ' . $this->navType); Html::addCssClass($this->options, ['widget' => 'nav', $this->navType]);
} }
/** /**
@ -162,13 +162,13 @@ class Tabs extends Widget
if (isset($item['items'])) { if (isset($item['items'])) {
$label .= ' <b class="caret"></b>'; $label .= ' <b class="caret"></b>';
Html::addCssClass($headerOptions, 'dropdown'); Html::addCssClass($headerOptions, ['widget' => 'dropdown']);
if ($this->renderDropdown($n, $item['items'], $panes)) { if ($this->renderDropdown($n, $item['items'], $panes)) {
Html::addCssClass($headerOptions, 'active'); Html::addCssClass($headerOptions, 'active');
} }
Html::addCssClass($linkOptions, 'dropdown-toggle'); Html::addCssClass($linkOptions, ['widget' => 'dropdown-toggle']);
$linkOptions['data-toggle'] = 'dropdown'; $linkOptions['data-toggle'] = 'dropdown';
$header = Html::a($label, "#", $linkOptions) . "\n" $header = Html::a($label, "#", $linkOptions) . "\n"
. Dropdown::widget(['items' => $item['items'], 'clientOptions' => false, 'view' => $this->getView()]); . Dropdown::widget(['items' => $item['items'], 'clientOptions' => false, 'view' => $this->getView()]);
@ -176,7 +176,7 @@ class Tabs extends Widget
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', [])); $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n); $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
Html::addCssClass($options, 'tab-pane'); Html::addCssClass($options, ['widget' => 'tab-pane']);
if (ArrayHelper::remove($item, 'active')) { if (ArrayHelper::remove($item, 'active')) {
Html::addCssClass($options, 'active'); Html::addCssClass($options, 'active');
Html::addCssClass($headerOptions, 'active'); Html::addCssClass($headerOptions, 'active');
@ -241,7 +241,7 @@ class Tabs extends Widget
$content = ArrayHelper::remove($item, 'content'); $content = ArrayHelper::remove($item, 'content');
$options = ArrayHelper::remove($item, 'contentOptions', []); $options = ArrayHelper::remove($item, 'contentOptions', []);
Html::addCssClass($options, 'tab-pane'); Html::addCssClass($options, ['widget' => 'tab-pane']);
if (ArrayHelper::remove($item, 'active')) { if (ArrayHelper::remove($item, 'active')) {
Html::addCssClass($options, 'active'); Html::addCssClass($options, 'active');
Html::addCssClass($item['options'], 'active'); Html::addCssClass($item['options'], 'active');

37
docs/guide/usage-widgets.md

@ -17,3 +17,40 @@ framework features. All widgets belong to `\yii\bootstrap` namespace:
- [[yii\bootstrap\NavBar|NavBar]] - [[yii\bootstrap\NavBar|NavBar]]
- [[yii\bootstrap\Progress|Progress]] - [[yii\bootstrap\Progress|Progress]]
- [[yii\bootstrap\Tabs|Tabs]] - [[yii\bootstrap\Tabs|Tabs]]
## Customize widget CSS classes <span id="widgets-customize-css-classes"></span>
Widgets allow quick composition of the HTML for particular Bootstrap components, which requires usage of the
Bootstrap CSS classes. Those classes are added automatically by the widgets. However usually widget provides
fields or properties, which allow customization of the CSS styles for particular HTML tags.
For example: you may use [[yii\bootstrap\Button::options]] to customize appearance of the rendered button.
Class 'btn' will be added automatically, so you don't need to worry about it. All you need is specify particular
button class:
```php
echo Button::widget([
'label' => 'Action',
'options' => ['class' => 'btn-primary'], // produces class "btn btn-primary"
]);
```
However, sometimes you may need to replace standard class, added by the widget, with alternative one.
For example: widget [[yii\bootstrap\ButtonGroup]] uses 'btn-group' class for the container div, but you may
need to use 'btn-group-vertical', which allows vertical-style layout for the buttons. Using plain 'class' option
simply adds 'btn-group-vertical' to 'btn-group', which produces incorrect result.
In order to actually replace class added by widget, you need to specify 'class' option as array, containing
needed class name under the 'widget' key:
```php
echo ButtonGroup::widget([
'options' => [
'class' => ['widget' => 'btn-group-vertical'] // replace 'btn-group' with 'btn-group-vertical'
],
'buttons' => [
['label' => 'A'],
['label' => 'B'],
]
]);
```

Loading…
Cancel
Save