Skip to content

Commit

Permalink
Impove code for dropdown menu generation
Browse files Browse the repository at this point in the history
  • Loading branch information
art009 committed Jan 12, 2021
1 parent d73cb55 commit 7c2ef65
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions src/Nav.php
Expand Up @@ -18,19 +18,20 @@ final class Nav extends Widget
private bool $activateItems = true;
private bool $activateParents = false;
private string $currentPath = '';
private bool $dropdown = false;
private bool $encodeLabels = true;
private array $items = [];

protected function run(): string
{
$html = $this->renderItems();
$items = [];

if ($this->dropdown) {
$html .= Html::endTag('div');
foreach ($this->items as $item) {
if (!isset($item['visible']) || $item['visible']) {
$items[] = $this->renderItem($item);
}
}

return $html;
return implode("\n", $items);
}

/**
Expand Down Expand Up @@ -136,14 +137,14 @@ public function items(array $value): self
private function renderDropdown(array $items, array $parentItem): string
{
return Dropdown::widget()
->dividerClass('navbar-divider')
->itemClass('navbar-item')
->itemsClass('navbar-dropdown')
->encloseByContainer(false)
->encodeLabels($this->encodeLabels)
->items($items)
->itemsOptions(ArrayHelper::getValue($parentItem, 'dropdownOptions', []))
->render() . "\n";
->dividerClass('navbar-divider')
->itemClass('navbar-item')
->itemsClass('navbar-dropdown')
->encloseByContainer(false)
->encodeLabels($this->encodeLabels)
->items($items)
->itemsOptions(ArrayHelper::getValue($parentItem, 'dropdownOptions', []))
->render() . "\n";
}

/**
Expand Down Expand Up @@ -218,26 +219,6 @@ private function renderIcon(string $label, string $icon, array $iconOptions): st
return $label;
}

/**
* Renders widget items.
*
* @throws InvalidArgumentException|JsonException
*
* @return string
*/
private function renderItems(): string
{
$items = [];

foreach ($this->items as $item) {
if (!isset($item['visible']) || $item['visible']) {
$items[] = $this->renderItem($item);
}
}

return implode("\n", $items);
}

/**
* Renders a widget's item.
*
Expand All @@ -252,7 +233,7 @@ private function renderItem(array $item): string
if (!isset($item['label'])) {
throw new InvalidArgumentException('The "label" option is required.');
}

$dropdown = false;
$this->encodeLabels = $item['encode'] ?? $this->encodeLabels;

if ($this->encodeLabels) {
Expand Down Expand Up @@ -280,7 +261,7 @@ private function renderItem(array $item): string
$active = $this->isItemActive($item);

if (isset($items)) {
$this->dropdown = true;
$dropdown = true;

Html::addCssClass($options, 'navbar-item has-dropdown is-hoverable');

Expand All @@ -301,12 +282,12 @@ private function renderItem(array $item): string
Html::addCssClass($linkOptions, 'is-active');
}


if ($this->dropdown) {
if ($dropdown) {
return
html::beginTag('div', $options) . "\n" .
Html::a($label, $url, ['class' => 'navbar-link']) . "\n" .
$items;
Html::beginTag('div', $options) . "\n" .
Html::a($label, $url, ['class' => 'navbar-link']) . "\n" .
$items .
Html::endTag('div');
}

return Html::a($label, $url, $linkOptions);
Expand Down

0 comments on commit 7c2ef65

Please sign in to comment.