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 #211: ListView::itemViewAttributes() accepts Closure as parameter. #212

21 changes: 16 additions & 5 deletions src/ListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class ListView extends BaseListView
*/
private $itemView = null;

private array $itemViewAttributes = [];
private array|Closure $itemViewAttributes = [];
samdark marked this conversation as resolved.
Show resolved Hide resolved
private string $separator = "\n";
private array $viewParams = [];
private ?View $view = null;
Expand Down Expand Up @@ -109,11 +109,18 @@ public function itemView(string|Closure $value): self
}

/**
* return new instance with the HTML attributes for the container of item view.
* Return new instance with the HTML attributes for the container of item view.
* If this property is specified as a callback, it must return an array of attributes and have the following
glpzzz marked this conversation as resolved.
Show resolved Hide resolved
* signature:
*
* @param array $values Attribute values indexed by attribute names.
* ```php
* function ($data, $key, $index, $widget)
* ```
*
* @param array|Closure $values Attribute values indexed by attribute names.
samdark marked this conversation as resolved.
Show resolved Hide resolved
* @return ListView
*/
public function itemViewAttributes(array $values): self
public function itemViewAttributes(array|Closure $values): self
{
$new = clone $this;
$new->itemViewAttributes = $values;
Expand Down Expand Up @@ -185,8 +192,12 @@ protected function renderItem(array|object $data, mixed $key, int $index): strin
$content = (string) call_user_func($this->itemView, $data, $key, $index, $this);
}

$itemViewAttributes = is_callable($this->itemViewAttributes)
? (array) call_user_func($this->itemViewAttributes, $data, $key, $index, $this)
: $this->itemViewAttributes;

return Div::tag()
->attributes($this->itemViewAttributes)
->attributes($itemViewAttributes)
->content("\n" . $content)
->encode(false)
->render();
Expand Down
27 changes: 27 additions & 0 deletions tests/ListView/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,31 @@ public function testKeysetPaginationConfig(): void
->render(),
);
}

public function testClosureForItemViewAttributes(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<div data-item-id="1" data-item-key="0" data-item-index="0">
<div>Id: 1</div><div>Name: John</div><div>Age: 20</div>
</div>
<div data-item-id="2" data-item-key="1" data-item-index="1">
<div>Id: 2</div><div>Name: Mary</div><div>Age: 21</div>
</div>
<div>Page <b>1</b> of <b>1</b></div>
</div>
HTML,
ListView::widget()
->itemView(dirname(__DIR__) . '/Support/view/_listview.php')
->itemViewAttributes(static fn (array $data, $key, $index, $widget) => [
'data-item-id' => $data['id'],
'data-item-key' => $key,
'data-item-index' => $index,
])
->dataReader($this->createOffsetPaginator($this->data, 10))
->separator(PHP_EOL)
->render(),
);
}
}
Loading