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

Add setters for table and tbody attributes in GridView #133

Merged
merged 6 commits into from
Nov 22, 2023
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
},
"config-plugin": {
"params": "params.php",
"di": "di.php"
"di": "di.php",
"widgets-themes": "widgets-themes.php"
}
},
"autoload": {
Expand Down
14 changes: 14 additions & 0 deletions config/widgets-themes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Yiisoft\Yii\DataView\GridView;

return [
'bootstrap5' => [
GridView::class => [
'tableClass()' => ['table table-bordered'],
'tbodyClass()' => ['table-group-divider'],
],
],
];
83 changes: 71 additions & 12 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +50,8 @@
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,
Expand Down Expand Up @@ -257,15 +257,74 @@
}

/**
* 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

Check warning on line 276 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L276

Added line #L276 was not covered by tests
{
$new = clone $this;
Html::addCssClass($new->tableAttributes, $class);
return $new;

Check warning on line 280 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L278-L280

Added lines #L278 - L280 were not covered by tests
}

/**
* 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

Check warning on line 288 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L288

Added line #L288 was not covered by tests
{
$new = clone $this;
$new->tableAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
return $new;

Check warning on line 292 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L290-L292

Added lines #L290 - L292 were not covered by tests
}

/**
* 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

Check warning on line 300 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L300

Added line #L300 was not covered by tests
{
$new = clone $this;
$new->tbodyAttributes = $attributes;
return $new;

Check warning on line 304 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L302-L304

Added lines #L302 - L304 were not covered by tests
}

/**
* 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

Check warning on line 312 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L312

Added line #L312 was not covered by tests
{
$new = clone $this;
Html::addCssClass($new->tbodyAttributes, $class);
return $new;

Check warning on line 316 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L314-L316

Added lines #L314 - L316 were not covered by tests
}

/**
* 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

Check warning on line 324 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L324

Added line #L324 was not covered by tests
{
$new = clone $this;
$new->tbodyAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);

Check warning on line 327 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L326-L327

Added lines #L326 - L327 were not covered by tests
return $new;
}

Expand Down Expand Up @@ -440,7 +499,7 @@
$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)) {
Expand All @@ -451,10 +510,10 @@
$rows[] = $this->renderTableRow($columns, $value, $key, $index);

if ($this->afterRow !== null) {
/** @psalm-var array<array-key,string> */
/** @var string */
$row = call_user_func($this->afterRow, $value, $key, $index, $this);

if ($row !== []) {
if (!empty($row)) {
terabytesoftw marked this conversation as resolved.
Show resolved Hide resolved
$rows[] = $row;
}
}
Expand All @@ -465,13 +524,13 @@
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<array-key,string> $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();
}

/**
Expand Down
34 changes: 17 additions & 17 deletions tests/Column/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testContent(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testContentAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -133,7 +133,7 @@ public function testCustomButton(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -188,7 +188,7 @@ public function testDataLabel(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -233,7 +233,7 @@ public function testFooterAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -286,7 +286,7 @@ public function testLabel(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>test.label</th>
Expand Down Expand Up @@ -331,7 +331,7 @@ public function testLabelWithMbString(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Ενέργειες</th>
Expand Down Expand Up @@ -376,7 +376,7 @@ public function testLabelAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th class="test.class">test.label</th>
Expand Down Expand Up @@ -421,7 +421,7 @@ public function testName(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -466,7 +466,7 @@ public function testNotVisible(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr></tr>
</thead>
Expand Down Expand Up @@ -497,7 +497,7 @@ public function testPrimaryKey(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -550,7 +550,7 @@ public function testRender(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -605,7 +605,7 @@ public function testUrlArguments(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -650,7 +650,7 @@ public function testUrlCreator(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -701,7 +701,7 @@ public function testUrlQueryParameters(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -746,7 +746,7 @@ public function testUrlParamsConfig(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -791,7 +791,7 @@ public function testVisibleButtonsClosure(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down
Loading
Loading