From f04f3c7f1fe795a7dfacd8f77b0c7e7d8b290f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20A=2E=20L=C3=B3pez=20L=C3=B3pez?= Date: Sat, 7 Sep 2024 01:35:18 -0400 Subject: [PATCH 01/11] #14: use `ul` and `li` tags for the default layout of the `ListView`. --- src/ListView.php | 50 ++++++++++++++++++++++---- tests/ListView/BaseTest.php | 72 ++++++++++++++++++++++--------------- 2 files changed, 87 insertions(+), 35 deletions(-) diff --git a/src/ListView.php b/src/ListView.php index d510759de..2dbd475c7 100644 --- a/src/ListView.php +++ b/src/ListView.php @@ -6,10 +6,10 @@ use Closure; use InvalidArgumentException; -use Yiisoft\Html\Tag\Div; +use Yiisoft\Html\Html; +use Yiisoft\Html\Tag\Li; use Yiisoft\View\Exception\ViewNotFoundException; use Yiisoft\View\View; - use function is_string; /** @@ -20,6 +20,12 @@ final class ListView extends BaseListView private ?Closure $afterItem = null; private ?Closure $beforeItem = null; + /** + * @psalm-var non-empty-string|null + */ + private ?string $itemsWrapperTag = 'ul'; + private array $itemsWrapperAttributes = []; + /** * @var callable|string|null */ @@ -30,6 +36,12 @@ final class ListView extends BaseListView private array $viewParams = []; private ?View $view = null; + public function __construct() + { + parent::__construct(); + $this->containerTag('ul'); + } + /** * Return new instance with afterItem closure. * @@ -81,6 +93,24 @@ public function beforeItem(Closure $value): self return $new; } + public function itemsWrapperTag(?string $tag): self + { + if ($tag === '') { + throw new InvalidArgumentException('Tag name cannot be empty.'); + } + + $new = clone $this; + $new->itemsWrapperTag = $tag; + return $new; + } + + public function itemsWrapperAttributes(array $values): self + { + $new = clone $this; + $new->itemsWrapperAttributes = $values; + return $new; + } + /** * Return new instance with itemView closure. * @@ -182,10 +212,10 @@ protected function renderItem(array|object $data, mixed $key, int $index): strin } if ($this->itemView instanceof Closure) { - $content = (string) call_user_func($this->itemView, $data, $key, $index, $this); + $content = (string)call_user_func($this->itemView, $data, $key, $index, $this); } - return Div::tag() + return Li::tag() ->attributes($this->itemViewAttributes) ->content("\n" . $content) ->encode(false) @@ -217,7 +247,13 @@ protected function renderItems(array $items, \Yiisoft\Validator\Result $filterVa } } - return implode($this->separator, $rows); + $content = implode($this->separator, $rows); + + return $this->itemsWrapperTag === null + ? $content + : Html::tag($this->itemsWrapperTag, "\n" . $content . "\n", $this->itemsWrapperAttributes) + ->encode(false) + ->render(); } /** @@ -238,7 +274,7 @@ private function renderAfterItem(array|object $data, mixed $key, int $index): st $result = ''; if (!empty($this->afterItem)) { - $result = (string) call_user_func($this->afterItem, $data, $key, $index, $this); + $result = (string)call_user_func($this->afterItem, $data, $key, $index, $this); } return $result; @@ -262,7 +298,7 @@ private function renderBeforeItem(array|object $data, mixed $key, int $index): s $result = ''; if (!empty($this->beforeItem)) { - $result = (string) call_user_func($this->beforeItem, $data, $key, $index, $this); + $result = (string)call_user_func($this->beforeItem, $data, $key, $index, $this); } return $result; diff --git a/tests/ListView/BaseTest.php b/tests/ListView/BaseTest.php index c1c22e2c9..21feea006 100644 --- a/tests/ListView/BaseTest.php +++ b/tests/ListView/BaseTest.php @@ -27,16 +27,18 @@ public function testAfterItemBeforeItem(): void Assert::equalsWithoutLE( << +
Page 1 of 1
HTML, @@ -54,12 +56,14 @@ public function testItemViewAttributes(): void Assert::equalsWithoutLE( << -
+
-
+ +
  • Id: 2
    Name: Mary
    Age: 21
    -
  • + +
    Page 1 of 1
    HTML, @@ -76,12 +80,14 @@ public function testItemViewAsString(): void Assert::equalsWithoutLE( << -
    +
    -
    + +
  • Id: 2
    Name: Mary
    Age: 21
    -
  • + +
    Page 1 of 1
    HTML, @@ -103,12 +109,14 @@ public function testItemViewAsCallable(): void Assert::equalsWithoutLE( << -
    +
    -
    + +
  • 2
    Mary
    -
  • + +
    Page 1 of 1
    HTML, @@ -132,12 +140,14 @@ public function testSeparator(): void Assert::equalsWithoutLE( << -
    +
    -
    + +
  • Id: 2
    Name: Mary
    Age: 21
    -
  • + +
    Page 1 of 1
    HTML, @@ -160,12 +170,14 @@ public function testViewParams(): void Assert::equalsWithoutLE( << -
    +
    -
    + +
  • 2
    -
  • + +
    Page 1 of 1
    HTML, @@ -183,9 +195,11 @@ public function testOffsetPaginationConfig(): void Assert::equalsWithoutLE( << -
    +
    + +
    Page 1 of 2