Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #14: Use ul and li tags for the default layout of the ListView #210

Merged
merged 14 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 75 additions & 10 deletions src/ListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Closure;
use InvalidArgumentException;
use Yiisoft\Html\Tag\Div;
use Yiisoft\Html\Html;
use Yiisoft\View\Exception\ViewNotFoundException;
use Yiisoft\View\View;

Expand All @@ -20,11 +20,21 @@ 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
*/
private $itemView = null;

/**
* @psalm-var non-empty-string|null
*/
private ?string $itemViewTag = 'li';
private array $itemViewAttributes = [];
private string $separator = "\n";
private array $viewParams = [];
Expand Down Expand Up @@ -81,6 +91,36 @@ public function beforeItem(Closure $value): self
return $new;
}

/**
* Set the HTML tag for the items wrapper.
*
* @param string|null $tag
* @return $this
*/
public function itemsWrapperTag(?string $tag): self
{
if ($tag === '') {
throw new InvalidArgumentException('The "itemsWrapperTag" property cannot be empty.');
}

$new = clone $this;
$new->itemsWrapperTag = $tag;
return $new;
}

/**
* Set the HTML attributes for the container of the items wrapper.
*
* @param array $values
* @return $this
*/
public function itemsWrapperAttributes(array $values): self
{
$new = clone $this;
$new->itemsWrapperAttributes = $values;
return $new;
}

/**
* Return new instance with itemView closure.
*
Expand Down Expand Up @@ -108,6 +148,23 @@ public function itemView(string|Closure $value): self
return $new;
}

/**
* Set the HTML tag for the container of item view.
*
* @param string|null $tag
* @return $this
*/
public function itemViewTag(?string $tag): self
{
if ($tag === '') {
throw new InvalidArgumentException('The "itemViewTag" property cannot be empty.');
}

$new = clone $this;
$new->itemViewTag = $tag;
return $new;
}

/**
* return new instance with the HTML attributes for the container of item view.
*
Expand Down Expand Up @@ -182,14 +239,16 @@ 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()
->attributes($this->itemViewAttributes)
->content("\n" . $content)
->encode(false)
->render();
return $this->itemViewTag === null
? $content
: Html::tag($this->itemViewTag)
->attributes($this->itemViewAttributes)
->content("\n" . $content)
->encode(false)
->render();
}

/**
Expand Down Expand Up @@ -217,7 +276,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();
}

/**
Expand All @@ -238,7 +303,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;
Expand All @@ -262,7 +327,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;
Expand Down
Loading
Loading