diff --git a/CHANGELOG.md b/CHANGELOG.md
index 93b4c1e..4faa086 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,8 +8,10 @@
- New #224: Add optional `wrap` parameter to `BooleanInputTag::label()` method that controls whether to wrap input tag
with label tag or place them aside (@vjik)
- New #225: Add `CheckboxList::checkboxLabelWrap()` and `RadioList::radioLabelWrap()` methods (@vjik)
-- New #227: Add ability to wrap items in checkbox and radio lists by methods `CheckboxList::checkboxWrapTag()`,
- `CheckboxList::checkboxWrapAttributes()`, `RadioList::radioWrapTag()` and `RadioList::radioWrapAttributes()` (@vjik)
+- New #227, #228: Add ability to wrap items in checkbox and radio lists by using methods
+ `CheckboxList::checkboxWrapTag()`, `CheckboxList::checkboxWrapAttributes()`, `CheckboxList::checkboxWrapClass()`,
+ `CheckboxList::addCheckboxWrapClass()`, `RadioList::radioWrapTag()`, `RadioList::radioWrapAttributes()`,
+ `RadioList::radioWrapClass()` and `RadioList::addRadioWrapClass()` (@vjik)
## 3.7.0 September 18, 2024
diff --git a/src/Widget/CheckboxList/CheckboxList.php b/src/Widget/CheckboxList/CheckboxList.php
index 76103fc..6d6f27a 100644
--- a/src/Widget/CheckboxList/CheckboxList.php
+++ b/src/Widget/CheckboxList/CheckboxList.php
@@ -105,6 +105,23 @@ public function checkboxWrapAttributes(array $attributes): self
return $new;
}
+ public function checkboxWrapClass(?string ...$class): self
+ {
+ $new = clone $this;
+ $new->checkboxWrapAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
+ return $new;
+ }
+
+ public function addCheckboxWrapClass(?string ...$class): self
+ {
+ $new = clone $this;
+ Html::addCssClass(
+ $new->checkboxWrapAttributes,
+ array_filter($class, static fn ($c) => $c !== null),
+ );
+ return $new;
+ }
+
public function addCheckboxAttributes(array $attributes): self
{
$new = clone $this;
diff --git a/src/Widget/RadioList/RadioList.php b/src/Widget/RadioList/RadioList.php
index 4dbb576..5852b20 100644
--- a/src/Widget/RadioList/RadioList.php
+++ b/src/Widget/RadioList/RadioList.php
@@ -99,6 +99,23 @@ public function radioWrapAttributes(array $attributes): self
return $new;
}
+ public function radioWrapClass(?string ...$class): self
+ {
+ $new = clone $this;
+ $new->radioWrapAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
+ return $new;
+ }
+
+ public function addRadioWrapClass(?string ...$class): self
+ {
+ $new = clone $this;
+ Html::addCssClass(
+ $new->radioWrapAttributes,
+ array_filter($class, static fn ($c) => $c !== null),
+ );
+ return $new;
+ }
+
public function addRadioAttributes(array $attributes): self
{
$new = clone $this;
diff --git a/tests/Widget/CheckboxListTest.php b/tests/Widget/CheckboxListTest.php
index 9c750fe..230a19c 100644
--- a/tests/Widget/CheckboxListTest.php
+++ b/tests/Widget/CheckboxListTest.php
@@ -781,6 +781,79 @@ public function testCheckboxWrap(): void
);
}
+ public static function dataCheckboxWrapClass(): array
+ {
+ return [
+ ['', []],
+ ['', [null]],
+ [' class', ['']],
+ [' class="main"', ['main']],
+ [' class="main bold"', ['main bold']],
+ [' class="main bold"', ['main', 'bold']],
+ ];
+ }
+
+ #[DataProvider('dataCheckboxWrapClass')]
+ public function testCheckboxWrapClass(string $expected, array $class): void
+ {
+ $html = CheckboxList::create('test')
+ ->items([1 => 'One', 2 => 'Two'])
+ ->checkboxWrapTag('div')
+ ->checkboxWrapAttributes(['class' => 'form-check'])
+ ->checkboxWrapClass(...$class)
+ ->render();
+
+ $this->assertSame(
+ <<
+
+
+
+
+
+
+
+ HTML,
+ $html,
+ );
+ }
+
+ public static function dataAddCheckboxWrapClass(): array
+ {
+ return [
+ [' class="form-check"', []],
+ [' class="form-check"', [null]],
+ [' class="form-check main"', ['main']],
+ [' class="form-check main bold"', ['main bold']],
+ [' class="form-check main bold"', ['main', 'bold']],
+ ];
+ }
+
+ #[DataProvider('dataAddCheckboxWrapClass')]
+ public function testAddCheckboxWrapClass(string $expected, array $class): void
+ {
+ $html = CheckboxList::create('test')
+ ->items([1 => 'One', 2 => 'Two'])
+ ->checkboxWrapTag('div')
+ ->checkboxWrapAttributes(['class' => 'form-check'])
+ ->addCheckboxWrapClass(...$class)
+ ->render();
+
+ $this->assertSame(
+ <<
+
+
+
+
+
+
+
+ HTML,
+ $html,
+ );
+ }
+
public function testStringable(): void
{
$this->assertSame(
@@ -798,6 +871,8 @@ public function testImmutability(): void
$this->assertNotSame($widget, $widget->containerAttributes([]));
$this->assertNotSame($widget, $widget->checkboxWrapTag('div'));
$this->assertNotSame($widget, $widget->checkboxWrapAttributes([]));
+ $this->assertNotSame($widget, $widget->checkboxWrapClass());
+ $this->assertNotSame($widget, $widget->addCheckboxWrapClass());
$this->assertNotSame($widget, $widget->addCheckboxAttributes([]));
$this->assertNotSame($widget, $widget->checkboxAttributes([]));
$this->assertNotSame($widget, $widget->addCheckboxLabelAttributes([]));
diff --git a/tests/Widget/RadioListTest.php b/tests/Widget/RadioListTest.php
index 763b438..d0ffbda 100644
--- a/tests/Widget/RadioListTest.php
+++ b/tests/Widget/RadioListTest.php
@@ -731,6 +731,79 @@ public function testRadioWrap(): void
);
}
+ public static function dataRadioWrapClass(): array
+ {
+ return [
+ ['', []],
+ ['', [null]],
+ [' class', ['']],
+ [' class="main"', ['main']],
+ [' class="main bold"', ['main bold']],
+ [' class="main bold"', ['main', 'bold']],
+ ];
+ }
+
+ #[DataProvider('dataRadioWrapClass')]
+ public function testRadioWrapClass(string $expected, array $class): void
+ {
+ $html = RadioList::create('test')
+ ->items([1 => 'One', 2 => 'Two'])
+ ->radioWrapTag('div')
+ ->radioWrapAttributes(['class' => 'form-check'])
+ ->radioWrapClass(...$class)
+ ->render();
+
+ $this->assertSame(
+ <<
+
+
+
+
+
+
+
+ HTML,
+ $html,
+ );
+ }
+
+ public static function dataAddRadioWrapClass(): array
+ {
+ return [
+ [' class="form-check"', []],
+ [' class="form-check"', [null]],
+ [' class="form-check main"', ['main']],
+ [' class="form-check main bold"', ['main bold']],
+ [' class="form-check main bold"', ['main', 'bold']],
+ ];
+ }
+
+ #[DataProvider('dataAddRadioWrapClass')]
+ public function testAddRadioWrapClass(string $expected, array $class): void
+ {
+ $html = RadioList::create('test')
+ ->items([1 => 'One', 2 => 'Two'])
+ ->radioWrapTag('div')
+ ->radioWrapAttributes(['class' => 'form-check'])
+ ->addRadioWrapClass(...$class)
+ ->render();
+
+ $this->assertSame(
+ <<
+
+
+
+
+
+
+
+ HTML,
+ $html,
+ );
+ }
+
public function testStringable(): void
{
$this->assertSame(
@@ -748,6 +821,8 @@ public function testImmutability(): void
$this->assertNotSame($widget, $widget->containerAttributes([]));
$this->assertNotSame($widget, $widget->radioWrapTag('div'));
$this->assertNotSame($widget, $widget->radioWrapAttributes([]));
+ $this->assertNotSame($widget, $widget->radioWrapClass());
+ $this->assertNotSame($widget, $widget->addRadioWrapClass());
$this->assertNotSame($widget, $widget->addRadioAttributes([]));
$this->assertNotSame($widget, $widget->radioAttributes([]));
$this->assertNotSame($widget, $widget->radioLabelWrap(false));