Skip to content

Commit

Permalink
Add ability to wrap items in checkbox and radio lists (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Oct 27, 2024
1 parent 5020e51 commit 7f40f13
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- 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)

## 3.7.0 September 18, 2024

Expand Down
28 changes: 27 additions & 1 deletion src/Widget/CheckboxList/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ final class CheckboxList implements NoEncodeStringableInterface
{
private ?string $containerTag = 'div';
private array $containerAttributes = [];

private ?string $checkboxWrapTag = null;
private array $checkboxWrapAttributes = [];

private array $checkboxAttributes = [];
private array $checkboxLabelAttributes = [];
private bool $checkboxLabelWrap = true;
Expand Down Expand Up @@ -87,6 +91,20 @@ public function containerAttributes(array $attributes): self
return $new;
}

public function checkboxWrapTag(?string $name): self
{
$new = clone $this;
$new->checkboxWrapTag = $name;
return $new;
}

public function checkboxWrapAttributes(array $attributes): self
{
$new = clone $this;
$new->checkboxWrapAttributes = $attributes;
return $new;
}

public function addCheckboxAttributes(array $attributes): self
{
$new = clone $this;
Expand Down Expand Up @@ -247,6 +265,14 @@ public function render(): string
{
$name = Html::getArrayableName($this->name);

if ($this->checkboxWrapTag === null) {
$beforeCheckbox = '';
$afterCheckbox = '';
} else {
$beforeCheckbox = Html::openTag($this->checkboxWrapTag, $this->checkboxWrapAttributes) . "\n";
$afterCheckbox = "\n" . Html::closeTag($this->checkboxWrapTag);
}

$lines = [];
$index = 0;
foreach ($this->items as $value => $label) {
Expand All @@ -265,7 +291,7 @@ public function render(): string
$this->checkboxLabelAttributes,
$this->checkboxLabelWrap,
);
$lines[] = $this->formatItem($item);
$lines[] = $beforeCheckbox . $this->formatItem($item) . $afterCheckbox;
$index++;
}

Expand Down
28 changes: 27 additions & 1 deletion src/Widget/RadioList/RadioList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final class RadioList implements NoEncodeStringableInterface
{
private ?string $containerTag = 'div';
private array $containerAttributes = [];

private ?string $radioWrapTag = null;
private array $radioWrapAttributes = [];

private array $radioAttributes = [];
private array $radioLabelAttributes = [];
private bool $radioLabelWrap = true;
Expand Down Expand Up @@ -81,6 +85,20 @@ public function containerAttributes(array $attributes): self
return $new;
}

public function radioWrapTag(?string $name): self
{
$new = clone $this;
$new->radioWrapTag = $name;
return $new;
}

public function radioWrapAttributes(array $attributes): self
{
$new = clone $this;
$new->radioWrapAttributes = $attributes;
return $new;
}

public function addRadioAttributes(array $attributes): self
{
$new = clone $this;
Expand Down Expand Up @@ -229,6 +247,14 @@ public function itemFormatter(?Closure $formatter): self

public function render(): string
{
if ($this->radioWrapTag === null) {
$beforeRadio = '';
$afterRadio = '';
} else {
$beforeRadio = Html::openTag($this->radioWrapTag, $this->radioWrapAttributes) . "\n";
$afterRadio = "\n" . Html::closeTag($this->radioWrapTag);
}

$lines = [];
$index = 0;
foreach ($this->items as $value => $label) {
Expand All @@ -247,7 +273,7 @@ public function render(): string
$this->radioLabelAttributes,
$this->radioLabelWrap,
);
$lines[] = $this->formatItem($item);
$lines[] = $beforeRadio . $this->formatItem($item) . $afterRadio;
$index++;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Widget/CheckboxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,29 @@ public function testDisableCheckboxLabelWrap(): void
);
}

public function testCheckboxWrap(): void
{
$html = CheckboxList::create('test')
->items([1 => 'One', 2 => 'Two'])
->checkboxWrapTag('div')
->checkboxWrapAttributes(['class' => 'form-check'])
->render();

$this->assertSame(
<<<HTML
<div>
<div class="form-check">
<label><input type="checkbox" name="test[]" value="1"> One</label>
</div>
<div class="form-check">
<label><input type="checkbox" name="test[]" value="2"> Two</label>
</div>
</div>
HTML,
$html,
);
}

public function testStringable(): void
{
$this->assertSame(
Expand All @@ -773,6 +796,8 @@ public function testImmutability(): void
$this->assertNotSame($widget, $widget->withoutContainer());
$this->assertNotSame($widget, $widget->containerTag(''));
$this->assertNotSame($widget, $widget->containerAttributes([]));
$this->assertNotSame($widget, $widget->checkboxWrapTag('div'));
$this->assertNotSame($widget, $widget->checkboxWrapAttributes([]));
$this->assertNotSame($widget, $widget->addCheckboxAttributes([]));
$this->assertNotSame($widget, $widget->checkboxAttributes([]));
$this->assertNotSame($widget, $widget->addCheckboxLabelAttributes([]));
Expand Down
25 changes: 25 additions & 0 deletions tests/Widget/RadioListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,29 @@ public function testDisableRadioLabelWrap(): void
);
}

public function testRadioWrap(): void
{
$html = RadioList::create('test')
->items([1 => 'One', 2 => 'Two'])
->radioWrapTag('div')
->radioWrapAttributes(['class' => 'form-check'])
->render();

$this->assertSame(
<<<HTML
<div>
<div class="form-check">
<label><input type="radio" name="test" value="1"> One</label>
</div>
<div class="form-check">
<label><input type="radio" name="test" value="2"> Two</label>
</div>
</div>
HTML,
$html,
);
}

public function testStringable(): void
{
$this->assertSame(
Expand All @@ -723,6 +746,8 @@ public function testImmutability(): void
$this->assertNotSame($widget, $widget->withoutContainer());
$this->assertNotSame($widget, $widget->containerTag(''));
$this->assertNotSame($widget, $widget->containerAttributes([]));
$this->assertNotSame($widget, $widget->radioWrapTag('div'));
$this->assertNotSame($widget, $widget->radioWrapAttributes([]));
$this->assertNotSame($widget, $widget->addRadioAttributes([]));
$this->assertNotSame($widget, $widget->radioAttributes([]));
$this->assertNotSame($widget, $widget->radioLabelWrap(false));
Expand Down

0 comments on commit 7f40f13

Please sign in to comment.