diff --git a/src/BaseListView.php b/src/BaseListView.php index 479b26342..66998954d 100644 --- a/src/BaseListView.php +++ b/src/BaseListView.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\DataView; +use BackedEnum; use InvalidArgumentException; use Stringable; use Yiisoft\Data\Paginator\InvalidPageException; @@ -1144,4 +1145,37 @@ public function sortParameterType(int $type): self $new->urlConfig = $this->urlConfig->withSortParameterType($type); return $new; } + + /** + * Set new container classes. + * + * Multiple classes can be set by passing them as separate arguments. `null` values are filtered out + * automatically. + * + * @param BackedEnum|string|null ...$class One or more CSS class names to use. Pass `null` to skip a class. + * @return self + */ + public function containerClass(BackedEnum|string|null ...$class): self + { + $new = clone $this; + $new->containerAttributes['class'] = []; + Html::addCssClass($new->containerAttributes, $class); + return $new; + } + + /** + * Adds one or more CSS classes to the existing container classes. + * + * Multiple classes can be added by passing them as separate arguments. `null` values are filtered out + * automatically. + * + * @param BackedEnum|string|null ...$class One or more CSS class names to add. Pass `null` to skip adding a class. + * @return self A new instance with the specified CSS classes added to existing ones. + */ + public function addContainerClass(BackedEnum|string|null ...$class): self + { + $new = clone $this; + Html::addCssClass($new->containerAttributes, $class); + return $new; + } } diff --git a/tests/ListView/BaseTest.php b/tests/ListView/BaseTest.php index d2d9a1d12..c2c4496ed 100644 --- a/tests/ListView/BaseTest.php +++ b/tests/ListView/BaseTest.php @@ -557,4 +557,56 @@ public function testIgnoreMissingPageFalse(): void $listView->render(); } + + public function testContainerClass(): void + { + Assert::equalsWithoutLE( + << + +
Page 1 of 1
+ + HTML, + ListView::widget() + ->itemView(dirname(__DIR__) . '/Support/view/_listview.php') + ->containerAttributes(['class' => 'existing']) + ->containerClass('my', 'container') + ->dataReader($this->createOffsetPaginator($this->data, 10)) + ->separator(PHP_EOL) + ->render(), + ); + } + + public function testAddContainerClass(): void + { + Assert::equalsWithoutLE( + << + +
Page 1 of 1
+ + HTML, + ListView::widget() + ->itemView(dirname(__DIR__) . '/Support/view/_listview.php') + ->containerAttributes(['class' => 'existing']) + ->addContainerClass('my', 'container') + ->dataReader($this->createOffsetPaginator($this->data, 10)) + ->separator(PHP_EOL) + ->render(), + ); + } }