From 7d4d0d1ddee388819e9caa2c6df26ba436f24770 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 21 Nov 2023 12:58:25 +0300 Subject: [PATCH 1/3] Add `ErrorSummary::listClass()` and `ErrorSummary::addListClass()` --- composer.json | 2 +- src/Field/ErrorSummary.php | 27 ++++++++++++++++++++ tests/Field/ErrorSummaryTest.php | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 76f1307c..6aba957a 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Field/ErrorSummary.php b/src/Field/ErrorSummary.php index d2ab71c6..3b67c926 100644 --- a/src/Field/ErrorSummary.php +++ b/src/Field/ErrorSummary.php @@ -138,6 +138,33 @@ 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. + */ + final public function addListClass(?string ...$class): self + { + $new = clone $this; + Html::addCssClass( + $new->listAttributes, + array_filter($class, static fn ($c) => $c !== null), + ); + return $new; + } + + /** + * Replace current list container tag CSS classes with a new set of classes. + * + * @param string|null ...$class One or many CSS classes. + */ + final public function listClass(?string ...$class): static + { + $new = clone $this; + $new->listAttributes['class'] = $class; + return $new; + } + protected function generateContent(): ?string { $messages = $this->collectErrors(); diff --git a/tests/Field/ErrorSummaryTest.php b/tests/Field/ErrorSummaryTest.php index 59f2280d..19bbaf88 100644 --- a/tests/Field/ErrorSummaryTest.php +++ b/tests/Field/ErrorSummaryTest.php @@ -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 = << +

Please fix the following errors:

+ + + HTML; + + $this->assertSame($expected, $result); + } + + public function testAddListClass(): void + { + $result = ErrorSummary::widget() + ->formModel(ErrorSummaryForm::validated()) + ->onlyAttributes('year') + ->listClass('errorsList') + ->addListClass('errorsList-tiny') + ->render(); + + $expected = << +

Please fix the following errors:

+ + + HTML; + + $this->assertSame($expected, $result); + } + public function testWithoutForm(): void { $field = ErrorSummary::widget(); @@ -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([])); } From 8ad108e9b934efc79b407cf816fbb4e4a06ac0c7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 21 Nov 2023 13:04:39 +0300 Subject: [PATCH 2/3] fix --- src/Field/ErrorSummary.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Field/ErrorSummary.php b/src/Field/ErrorSummary.php index 3b67c926..45cae243 100644 --- a/src/Field/ErrorSummary.php +++ b/src/Field/ErrorSummary.php @@ -146,10 +146,7 @@ public function listAttributes(array $attributes): self final public function addListClass(?string ...$class): self { $new = clone $this; - Html::addCssClass( - $new->listAttributes, - array_filter($class, static fn ($c) => $c !== null), - ); + Html::addCssClass($new->listAttributes, $class); return $new; } @@ -161,7 +158,7 @@ final public function addListClass(?string ...$class): self final public function listClass(?string ...$class): static { $new = clone $this; - $new->listAttributes['class'] = $class; + $new->listAttributes['class'] = array_filter($class, static fn ($c) => $c !== null); return $new; } From 7ed129a3bb761a2559ec327f8342fddea9433551 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 21 Nov 2023 10:06:50 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- src/Field/ErrorSummary.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Field/ErrorSummary.php b/src/Field/ErrorSummary.php index 45cae243..ddc5fb64 100644 --- a/src/Field/ErrorSummary.php +++ b/src/Field/ErrorSummary.php @@ -143,7 +143,7 @@ public function listAttributes(array $attributes): self * * @param string|null ...$class One or many CSS classes. */ - final public function addListClass(?string ...$class): self + public function addListClass(?string ...$class): self { $new = clone $this; Html::addCssClass($new->listAttributes, $class); @@ -155,7 +155,7 @@ final public function addListClass(?string ...$class): self * * @param string|null ...$class One or many CSS classes. */ - final public function listClass(?string ...$class): static + public function listClass(?string ...$class): static { $new = clone $this; $new->listAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);