Skip to content

Commit

Permalink
Add support for activateItems() and currentPath() to Nav widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 10, 2025
1 parent c62e942 commit 0312ed1
Show file tree
Hide file tree
Showing 7 changed files with 714 additions and 323 deletions.
48 changes: 44 additions & 4 deletions src/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class Dropdown extends \Yiisoft\Widget\Widget
public function addAttributes(array $values): self
{
$new = clone $this;
$new->attributes = array_merge($this->attributes, $values);
$new->attributes = [...$this->attributes, $values];

return $new;
}
Expand All @@ -100,7 +100,7 @@ public function addAttributes(array $values): self
public function addClass(BackedEnum|string|null ...$value): self
{
$new = clone $this;
$new->cssClasses = array_merge($new->cssClasses, $value);
$new->cssClasses = [...$this->cssClasses, ...$value];

return $new;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public function alignment(DropdownAlignment|null ...$value): self
public function addToggleAttributes(array $values): self
{
$new = clone $this;
$new->toggleAttributes = array_merge($this->toggleAttributes, $values);
$new->toggleAttributes = [...$this->toggleAttributes, $values];

return $new;
}
Expand Down Expand Up @@ -254,6 +254,16 @@ public function direction(DropdownDirection $value): self
return $new;
}

/**
* Returns the list of links to appear in the dropdown.
*
* @return DropdownItem[] The links to appear in the dropdown.
*/
public function getItems(): array
{
return $this->items;
}

/**
* List of links to appear in the dropdown. If this property is empty, the widget will not render anything.
*
Expand Down Expand Up @@ -482,6 +492,11 @@ public function toggleVariant(DropdownToggleVariant $value): self
return $new;
}

/**
* Renders the dropdown component.
*
* @return string The rendering result.
*/
public function render(): string
{
$attributes = $this->attributes;
Expand Down Expand Up @@ -531,14 +546,20 @@ public function render(): string
}

/**
* Renders the dropdown items.
*
* @param string|null $toggleId The ID of the toggle button.
*
* @return string The rendering result.
*
* @psalm-param non-empty-string|null $toggleId
*/
private function renderItems(string|null $toggleId): string
{
$items = [];

foreach ($this->items as $item) {
$items[] = $item->getContent();
$items[] = $item->getLiContent();
}

$ulTag = Ul::tag()
Expand All @@ -554,6 +575,12 @@ private function renderItems(string|null $toggleId): string
}

/**
* Renders the dropdown toggle button.
*
* @param string|null $toggleId The ID of the toggle button.
*
* @return string The rendering result.
*
* @psalm-param non-empty-string|null $toggleId
*/
private function renderToggle(string|null $toggleId): string
Expand Down Expand Up @@ -608,6 +635,14 @@ private function renderToggle(string|null $toggleId): string
->render();
}

/**
* Renders the dropdown toggle link.
*
* @param array $toggleAttributes The HTML attributes for the toggle link.
* @param string $toggleContent The content of the toggle link.
*
* @return string The rendering result.
*/
private function renderToggleLink(array $toggleAttributes, string $toggleContent): string
{
$toggleAttributes['role'] = 'button';
Expand All @@ -622,6 +657,11 @@ private function renderToggleLink(array $toggleAttributes, string $toggleContent
->render();
}

/**
* Renders the dropdown split button.
*
* @return string The rendering result.
*/
private function renderToggleSplit(): string
{
if ($this->toggleLink) {
Expand Down
42 changes: 33 additions & 9 deletions src/DropdownItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
* For example,
*
* ```php
* echo DropdownItem::link('Dropdown link', '#');
* DropdownItem::link('Dropdown link', '#');
* DropdownItem::divider();
* DropdownItem::header('Dropdown header');
* DropdownItem::text('Dropdown text');
* DropdownItem::listContent('<a href="#">Dropdown list content</a>');
* ```
*/
final class DropdownItem
Expand All @@ -30,8 +34,10 @@ final class DropdownItem
private const DROPDOWN_ITEM_DISABLED_CLASS = 'disabled';
private const DROPDOWN_ITEM_HEADER_CLASS = 'dropdown-header';
private const DROPDOWN_ITEM_TEXT_CLASS = 'dropdown-item-text';
private string $content = '';
/** @psalm-suppress PropertyNotSetInConstructor */
private Li $content;
private Li $liContent;
private string|null $url = null;

private function __construct()
{
Expand All @@ -53,7 +59,7 @@ public static function divider(array $attributes = [], array $dividerAttributes

unset($dividerAttributes['class']);

$dropdownItem->content = Li::tag()
$dropdownItem->liContent = Li::tag()
->addAttributes($attributes)
->addContent(
"\n",
Expand Down Expand Up @@ -90,7 +96,7 @@ public static function header(

unset($headerAttributes['class']);

$dropdownItem->content = Li::tag()
$dropdownItem->liContent = Li::tag()
->addAttributes($attributes)
->addContent(
"\n",
Expand Down Expand Up @@ -152,7 +158,9 @@ public static function link(
default => A::tag()->addAttributes($linkAttributes)->addClass($classesLink)->content($content)->url($url),
};

$dropdownItem->content = Li::tag()->addAttributes($attributes)->addContent("\n", $liContent, "\n");
$dropdownItem->content = $content;

Check failure on line 161 in src/DropdownItem.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ImplicitToStringCast

src/DropdownItem.php:161:34: ImplicitToStringCast: $dropdownItem->content expects 'string', 'Stringable|string' provided with a __toString method (see https://psalm.dev/060)

Check failure on line 161 in src/DropdownItem.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

ImplicitToStringCast

src/DropdownItem.php:161:34: ImplicitToStringCast: $dropdownItem->content expects 'string', 'Stringable|string' provided with a __toString method (see https://psalm.dev/060)

Check failure on line 161 in src/DropdownItem.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

ImplicitToStringCast

src/DropdownItem.php:161:34: ImplicitToStringCast: $dropdownItem->content expects 'string', 'Stringable|string' provided with a __toString method (see https://psalm.dev/060)
$dropdownItem->liContent = Li::tag()->addAttributes($attributes)->addContent("\n", $liContent, "\n");
$dropdownItem->url = $url;

return $dropdownItem;
}
Expand All @@ -169,7 +177,7 @@ public static function listContent(string|Stringable $content = '', array $attri
{
$dropdownItem = new self();

$dropdownItem->content = Li::tag()->addAttributes($attributes)->addContent($content)->encode(false);
$dropdownItem->liContent = Li::tag()->addAttributes($attributes)->addContent($content)->encode(false);

return $dropdownItem;
}
Expand All @@ -194,7 +202,7 @@ public static function text(

unset($textAttributes['class']);

$dropdownItem->content = Li::tag()
$dropdownItem->liContent = Li::tag()
->addAttributes($attributes)
->addContent(
"\n",
Expand All @@ -209,10 +217,26 @@ public static function text(
}

/**
* @return Li Returns the encoded label content.
* @return string Returns the dropdown item content.
*/
public function getContent(): Li
public function getContent(): string
{
return $this->content;
}

/**
* @return Li Returns the dropdown item <li> content.
*/
public function getLiContent(): Li
{
return $this->liContent;
}

/**
* @return string|null Returns the URL of the dropdown item.
*/
public function getUrl(): string|null
{
return $this->url;
}
}
Loading

0 comments on commit 0312ed1

Please sign in to comment.