diff --git a/composer.json b/composer.json index 71fb52d5e..c062dcda0 100644 --- a/composer.json +++ b/composer.json @@ -63,7 +63,8 @@ }, "config-plugin": { "params": "params.php", - "di": "di.php" + "di": "di.php", + "widgets-themes": "widgets-themes.php" } }, "autoload": { diff --git a/config/widgets-themes.php b/config/widgets-themes.php new file mode 100644 index 000000000..4e7973fc8 --- /dev/null +++ b/config/widgets-themes.php @@ -0,0 +1,14 @@ + [ + GridView::class => [ + 'tableClass()' => ['table table-bordered'], + 'tbodyClass()' => ['table-group-divider'], + ], + ], +]; diff --git a/src/GridView.php b/src/GridView.php index 53ea4e005..905d1f39b 100644 --- a/src/GridView.php +++ b/src/GridView.php @@ -12,7 +12,6 @@ use Yiisoft\Html\Html; use Yiisoft\Html\Tag\Col; use Yiisoft\Html\Tag\Colgroup; -use Yiisoft\Html\Tag\Tbody; use Yiisoft\Html\Tag\Td; use Yiisoft\Html\Tag\Tr; use Yiisoft\Router\CurrentRoute; @@ -51,7 +50,8 @@ final class GridView extends BaseListView private bool $headerTableEnabled = true; private array $headerRowAttributes = []; private array $rowAttributes = []; - private array $tableAttributes = ['class' => 'table']; + private array $tableAttributes = []; + private array $tbodyAttributes = []; public function __construct( private CurrentRoute $currentRoute, @@ -257,15 +257,74 @@ public function rowAttributes(array $values): self } /** - * Return new instance with the HTML attributes for the table. + * Return new instance with the HTML attributes for the `table` tag. * - * @param array $values Attribute values indexed by attribute names. + * @param array $attributes The tag attributes in terms of name-value pairs. */ - public function tableAttributes(array $values): self + public function tableAttributes(array $attributes): self { $new = clone $this; - $new->tableAttributes = $values; + $new->tableAttributes = $attributes; + return $new; + } + /** + * Add one or more CSS classes to the `table` tag. + * + * @param string|null ...$class One or many CSS classes. + */ + public function addTableClass(?string ...$class): self + { + $new = clone $this; + Html::addCssClass($new->tableAttributes, $class); + return $new; + } + + /** + * Replace current `table` tag CSS classes with a new set of classes. + * + * @param string|null ...$class One or many CSS classes. + */ + public function tableClass(?string ...$class): static + { + $new = clone $this; + $new->tableAttributes['class'] = array_filter($class, static fn ($c) => $c !== null); + return $new; + } + + /** + * Return new instance with the HTML attributes for the `tbody` tag. + * + * @param array $attributes The tag attributes in terms of name-value pairs. + */ + public function tbodyAttributes(array $attributes): self + { + $new = clone $this; + $new->tbodyAttributes = $attributes; + return $new; + } + + /** + * Add one or more CSS classes to the `tbody` tag. + * + * @param string|null ...$class One or many CSS classes. + */ + public function addTbodyClass(?string ...$class): self + { + $new = clone $this; + Html::addCssClass($new->tbodyAttributes, $class); + return $new; + } + + /** + * Replace current `tbody` tag CSS classes with a new set of classes. + * + * @param string|null ...$class One or many CSS classes. + */ + public function tbodyClass(?string ...$class): static + { + $new = clone $this; + $new->tbodyAttributes['class'] = array_filter($class, static fn ($c) => $c !== null); return $new; } @@ -440,7 +499,7 @@ private function renderTableBody(array $columns): string $index = 0; foreach ($this->getItems() as $key => $value) { if ($this->beforeRow !== null) { - /** @var array */ + /** @var string */ $row = call_user_func($this->beforeRow, $value, $key, $index, $this); if (!empty($row)) { @@ -451,10 +510,10 @@ private function renderTableBody(array $columns): string $rows[] = $this->renderTableRow($columns, $value, $key, $index); if ($this->afterRow !== null) { - /** @psalm-var array */ + /** @var string */ $row = call_user_func($this->afterRow, $value, $key, $index, $this); - if ($row !== []) { + if (!empty($row)) { $rows[] = $row; } } @@ -465,13 +524,13 @@ private function renderTableBody(array $columns): string if ($rows === [] && $this->emptyText !== '') { $colspan = count($columns); - return Tbody::tag() + return Html::tbody($this->tbodyAttributes) ->rows(Tr::tag()->cells($this->renderEmpty($colspan))) ->render(); } - /** @psalm-var array $rows */ - return Html::tag('tbody', PHP_EOL . implode(PHP_EOL, $rows) . PHP_EOL)->encode(false)->render(); + $tbody = Html::tbody($this->tbodyAttributes); + return $tbody->open() . PHP_EOL . implode(PHP_EOL, $rows) . PHP_EOL . $tbody->close(); } /** diff --git a/tests/Column/ActionColumnTest.php b/tests/Column/ActionColumnTest.php index 47555fb6e..ff14772b1 100644 --- a/tests/Column/ActionColumnTest.php +++ b/tests/Column/ActionColumnTest.php @@ -36,7 +36,7 @@ public function testContent(): void Assert::equalsWithoutLE( << - +
@@ -84,7 +84,7 @@ public function testContentAttributes(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -133,7 +133,7 @@ public function testCustomButton(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -188,7 +188,7 @@ public function testDataLabel(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -233,7 +233,7 @@ public function testFooterAttributes(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -286,7 +286,7 @@ public function testLabel(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -331,7 +331,7 @@ public function testLabelWithMbString(): void Assert::equalsWithoutLE( << -
test.label
+
@@ -376,7 +376,7 @@ public function testLabelAttributes(): void Assert::equalsWithoutLE( << -
Ενέργειες
+
@@ -421,7 +421,7 @@ public function testName(): void Assert::equalsWithoutLE( << -
test.label
+
@@ -466,7 +466,7 @@ public function testNotVisible(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -497,7 +497,7 @@ public function testPrimaryKey(): void Assert::equalsWithoutLE( << -
+
@@ -550,7 +550,7 @@ public function testRender(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -605,7 +605,7 @@ public function testUrlArguments(): void Assert::equalsWithoutLE( << -
Id
+
@@ -650,7 +650,7 @@ public function testUrlCreator(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -701,7 +701,7 @@ public function testUrlQueryParameters(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -746,7 +746,7 @@ public function testUrlParamsConfig(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -791,7 +791,7 @@ public function testVisibleButtonsClosure(): void Assert::equalsWithoutLE( << -
Actions
+
diff --git a/tests/Column/CheckboxColumnTest.php b/tests/Column/CheckboxColumnTest.php index fd14b8561..f9c11fa86 100644 --- a/tests/Column/CheckboxColumnTest.php +++ b/tests/Column/CheckboxColumnTest.php @@ -35,7 +35,7 @@ public function testContent(): void Assert::equalsWithoutLE( << -
Actions
+
@@ -84,7 +84,7 @@ public function testContentAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -135,7 +135,7 @@ public function testDataLabel(): void Assert::equalsWithoutLE( << -
Id
+
@@ -182,7 +182,7 @@ public function testLabel(): void Assert::equalsWithoutLE( << -
Id
+
@@ -229,7 +229,7 @@ public function testLabelMbString(): void Assert::equalsWithoutLE( << -
Id
+
@@ -276,7 +276,7 @@ public function testLabelAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -323,7 +323,7 @@ public function testName(): void Assert::equalsWithoutLE( << -
Id
+
@@ -370,7 +370,7 @@ public function testNotMultiple(): void Assert::equalsWithoutLE( << -
Id
+
@@ -417,7 +417,7 @@ public function testNotVisible(): void Assert::equalsWithoutLE( << -
Id
+
@@ -461,7 +461,7 @@ public function testRender(): void Assert::equalsWithoutLE( << -
Id
+
diff --git a/tests/Column/DataColumnFilterTest.php b/tests/Column/DataColumnFilterTest.php index 1a1c6a5e9..5dd4f4cf0 100644 --- a/tests/Column/DataColumnFilterTest.php +++ b/tests/Column/DataColumnFilterTest.php @@ -74,7 +74,7 @@ public function testFilter(): void Assert::equalsWithoutLE( << -
Id
+
@@ -121,7 +121,7 @@ public function testFilterDate(): void Assert::equalsWithoutLE( << -
Id
+
@@ -173,7 +173,7 @@ public function testFilterDateTime(): void Assert::equalsWithoutLE( << -
Id
+
@@ -225,7 +225,7 @@ public function testFilterEmail(): void Assert::equalsWithoutLE( << -
Id
+
@@ -277,7 +277,7 @@ public function testFilterInputAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -333,7 +333,7 @@ public function testFilterMonth(): void Assert::equalsWithoutLE( << -
Id
+
@@ -385,7 +385,7 @@ public function testFilterNumber(): void Assert::equalsWithoutLE( << -
Id
+
@@ -432,7 +432,7 @@ public function testFilterPositionFooter(): void Assert::equalsWithoutLE( << -
Id
+
@@ -497,7 +497,7 @@ public function testFilterPositionHeader(): void Assert::equalsWithoutLE( << -
Id
+
@@ -556,7 +556,7 @@ public function testFilterRange(): void Assert::equalsWithoutLE( << -
+
@@ -607,7 +607,7 @@ public function testFilterRowAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -666,7 +666,7 @@ public function testFilterSearch(): void Assert::equalsWithoutLE( << -
Id
+
@@ -713,7 +713,7 @@ public function testFilterSelect(): void Assert::equalsWithoutLE( << -
Id
+
@@ -771,7 +771,7 @@ public function testFilterTelephone(): void Assert::equalsWithoutLE( << -
Id
+
@@ -825,7 +825,7 @@ public function testFilterTime(): void Assert::equalsWithoutLE( << -
Id
+
@@ -879,7 +879,7 @@ public function testFilterUrl(): void Assert::equalsWithoutLE( << -
Id
+
@@ -933,7 +933,7 @@ public function testFilterWeek(): void Assert::equalsWithoutLE( << -
Id
+
@@ -987,7 +987,7 @@ public function testFilters(): void Assert::equalsWithoutLE( << -
Id
+
diff --git a/tests/Column/DataColumnTest.php b/tests/Column/DataColumnTest.php index 78659254f..33e5f7af2 100644 --- a/tests/Column/DataColumnTest.php +++ b/tests/Column/DataColumnTest.php @@ -34,7 +34,7 @@ public function testContent(): void Assert::equalsWithoutLE( << -
Id
+
@@ -81,7 +81,7 @@ public function testContentAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -130,7 +130,7 @@ public function testContentAttributesClosure(): void Assert::equalsWithoutLE( << -
Id
+
@@ -177,7 +177,7 @@ public function testDataLabel(): void Assert::equalsWithoutLE( << -
Id
+
@@ -220,7 +220,7 @@ public function testLabel(): void Assert::equalsWithoutLE( << -
Id
+
@@ -263,7 +263,7 @@ public function testLabelMbString(): void Assert::equalsWithoutLE( << -
test.id
+
@@ -306,7 +306,7 @@ public function testLabelAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -355,7 +355,7 @@ public function testLinkSorter(): void Assert::equalsWithoutLE( << -
test.id
+
@@ -402,7 +402,7 @@ public function testName(): void Assert::equalsWithoutLE( << -
id
+
@@ -445,7 +445,7 @@ public function testNotSorting(): void Assert::equalsWithoutLE( << -
Id
+
@@ -488,7 +488,7 @@ public function testNotVisible(): void Assert::equalsWithoutLE( << -
Id
+
@@ -528,7 +528,7 @@ public function testSort(): void Assert::equalsWithoutLE( << -
Id
+
@@ -571,7 +571,7 @@ public function testValue(): void Assert::equalsWithoutLE( << -
Id
+
@@ -614,7 +614,7 @@ public function testValueClosure(): void Assert::equalsWithoutLE( << -
Id
+
diff --git a/tests/Column/RadioColumnTest.php b/tests/Column/RadioColumnTest.php index 777a7a7b0..75847ef75 100644 --- a/tests/Column/RadioColumnTest.php +++ b/tests/Column/RadioColumnTest.php @@ -35,7 +35,7 @@ public function testContent(): void Assert::equalsWithoutLE( << -
Id
+
@@ -85,7 +85,7 @@ public function testContentAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -136,7 +136,7 @@ public function testDataLabel(): void Assert::equalsWithoutLE( << -
Id
+
@@ -183,7 +183,7 @@ public function testLabel(): void Assert::equalsWithoutLE( << -
Id
+
@@ -230,7 +230,7 @@ public function testLabelMbString(): void Assert::equalsWithoutLE( << -
Id
+
@@ -277,7 +277,7 @@ public function testLabelAttributes(): void Assert::equalsWithoutLE( << -
Id
+
@@ -324,7 +324,7 @@ public function testName(): void Assert::equalsWithoutLE( << -
Id
+
@@ -371,7 +371,7 @@ public function testNotVisible(): void Assert::equalsWithoutLE( << -
Id
+
@@ -415,7 +415,7 @@ public function testRender(): void Assert::equalsWithoutLE( << -
Id
+
diff --git a/tests/GridView/BaseTest.php b/tests/GridView/BaseTest.php index a65e39a42..4e6df2ec5 100644 --- a/tests/GridView/BaseTest.php +++ b/tests/GridView/BaseTest.php @@ -35,7 +35,7 @@ public function testAfterItemBeforeItem(): void Assert::equalsWithoutLE( << -
Id
+
@@ -92,7 +92,7 @@ public function testColumnGroupEnabled(): void Assert::equalsWithoutLE( << -
#
+
@@ -145,7 +145,7 @@ public function testColumnGroupEnabledEmpty(): void Assert::equalsWithoutLE( << -
+
@@ -203,7 +203,7 @@ public function testColumnGuess(): void Assert::equalsWithoutLE( << -
+
@@ -256,7 +256,7 @@ public function testEmptyCell(): void Assert::equalsWithoutLE( << -
Id
+
@@ -291,7 +291,7 @@ public function testEmptyText(): void Assert::equalsWithoutLE( << -
Id
+
@@ -333,7 +333,7 @@ public function testFooterRowAttributes(): void Assert::equalsWithoutLE( << -
#
+
@@ -388,7 +388,7 @@ public function testHeader(): void <<List of users
-
#
+
@@ -441,7 +441,7 @@ public function testHeaderIntoGrid(): void <<
List of users
-
#
+
@@ -495,7 +495,7 @@ public function testHeaderRowAttributes(): void Assert::equalsWithoutLE( << -
#
+
@@ -547,7 +547,7 @@ public function testHeaderTableEnabledFalse(): void Assert::equalsWithoutLE( << -
#
+
@@ -591,7 +591,7 @@ public function testRenderEmptyData(): void Assert::equalsWithoutLE( << -
1
+
@@ -632,7 +632,7 @@ public function testRowAttributes(): void Assert::equalsWithoutLE( << -
#
+
@@ -675,7 +675,7 @@ public function testRowAttributes(): void Assert::equalsWithoutLE( << -
#
+
diff --git a/tests/GridView/TranslatorTest.php b/tests/GridView/TranslatorTest.php index a0dd132a6..20d42494a 100644 --- a/tests/GridView/TranslatorTest.php +++ b/tests/GridView/TranslatorTest.php @@ -58,7 +58,7 @@ public function testEmptyTextTranslatorWithLocaleDefault(): void Assert::equalsWithoutLE( << -
#
+
@@ -101,7 +101,7 @@ public function testEmptyTextTranslatorWithLocaleSpanish(): void Assert::equalsWithoutLE( << -
#
+
@@ -144,7 +144,7 @@ public function testEmptyTextTranslatorWithLocaleRussian(): void Assert::equalsWithoutLE( << -
#
+
@@ -185,7 +185,7 @@ public function testSummaryTranslatorWithLocaleDefault(): void Assert::equalsWithoutLE( << -
#
+
@@ -238,7 +238,7 @@ public function testSummaryTranslatorWithLocaleSpanish(): void Assert::equalsWithoutLE( << -
#
+
@@ -291,7 +291,7 @@ public function testSummaryTranslatorWithLocaleRussian(): void Assert::equalsWithoutLE( << -
#
+
diff --git a/tests/Pagination/KeysetPaginationBaseTest.php b/tests/Pagination/KeysetPaginationBaseTest.php index 4c5b0114a..a5275a8c6 100644 --- a/tests/Pagination/KeysetPaginationBaseTest.php +++ b/tests/Pagination/KeysetPaginationBaseTest.php @@ -47,7 +47,7 @@ public function testRenderPaginatorEmptyData(): void Assert::equalsWithoutLE( << -
#
+
@@ -80,7 +80,7 @@ public function testRenderPaginationLinks(): void Assert::equalsWithoutLE( << -
+
@@ -142,7 +142,7 @@ public function testRenderPaginationLinks(): void Assert::equalsWithoutLE( << -
Id
+
@@ -204,7 +204,7 @@ public function testRenderPaginationLinks(): void Assert::equalsWithoutLE( << -
Id
+
@@ -251,7 +251,7 @@ public function testRenderPaginationLinks(): void Assert::equalsWithoutLE( << -
Id
+
@@ -313,7 +313,7 @@ public function testRenderPaginationLinks(): void Assert::equalsWithoutLE( << -
Id
+
diff --git a/tests/Pagination/OffsetPaginationBaseTest.php b/tests/Pagination/OffsetPaginationBaseTest.php index b61f94f5b..f75995f0f 100644 --- a/tests/Pagination/OffsetPaginationBaseTest.php +++ b/tests/Pagination/OffsetPaginationBaseTest.php @@ -31,7 +31,7 @@ public function testRenderPaginatorEmptyData(): void Assert::equalsWithoutLE( << -
Id
+