Skip to content

Commit

Permalink
Add ErrorSummary::listClass() and ErrorSummary::addListClass() (#274
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vjik committed Nov 21, 2023
1 parent dad3a5a commit 94e148e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -22,7 +22,7 @@
"php": "^8.0",
"ext-mbstring": "*",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/html": "^3.0",
"yiisoft/html": "^3.2",
"yiisoft/http": "^1.2",
"yiisoft/hydrator": "dev-master",
"yiisoft/hydrator-validator": "dev-master",
Expand Down
24 changes: 24 additions & 0 deletions src/Field/ErrorSummary.php
Expand Up @@ -138,6 +138,30 @@ public function listAttributes(array $attributes): self
return $new;
}

/**
* Add one or more CSS classes to the list container tag.
*
* @param string|null ...$class One or many CSS classes.
*/
public function addListClass(?string ...$class): self
{
$new = clone $this;
Html::addCssClass($new->listAttributes, $class);
return $new;
}

/**
* Replace current list container tag CSS classes with a new set of classes.
*
* @param string|null ...$class One or many CSS classes.
*/
public function listClass(?string ...$class): static
{
$new = clone $this;
$new->listAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
return $new;
}

protected function generateContent(): ?string
{
$messages = $this->collectErrors();
Expand Down
44 changes: 44 additions & 0 deletions tests/Field/ErrorSummaryTest.php
Expand Up @@ -180,6 +180,48 @@ public function testListAttributes(): void
$this->assertSame($expected, $result);
}

public function testListClass(): void
{
$result = ErrorSummary::widget()
->formModel(ErrorSummaryForm::validated())
->onlyAttributes('year')
->listAttributes(['class' => 'list'])
->listClass('errorsList')
->render();

$expected = <<<HTML
<div>
<p>Please fix the following errors:</p>
<ul class="errorsList">
<li>Bad year.</li>
</ul>
</div>
HTML;

$this->assertSame($expected, $result);
}

public function testAddListClass(): void
{
$result = ErrorSummary::widget()
->formModel(ErrorSummaryForm::validated())
->onlyAttributes('year')
->listClass('errorsList')
->addListClass('errorsList-tiny')
->render();

$expected = <<<HTML
<div>
<p>Please fix the following errors:</p>
<ul class="errorsList errorsList-tiny">
<li>Bad year.</li>
</ul>
</div>
HTML;

$this->assertSame($expected, $result);
}

public function testWithoutForm(): void
{
$field = ErrorSummary::widget();
Expand All @@ -201,6 +243,8 @@ public function testImmutability(): void
$this->assertNotSame($field, $field->header(''));
$this->assertNotSame($field, $field->headerAttributes([]));
$this->assertNotSame($field, $field->listAttributes([]));
$this->assertNotSame($field, $field->listClass());
$this->assertNotSame($field, $field->addListClass());
$this->assertNotSame($field, $field->footer(''));
$this->assertNotSame($field, $field->footerAttributes([]));
}
Expand Down

0 comments on commit 94e148e

Please sign in to comment.