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 checkbox/radio lists methods to set wrap classes #228

Merged
merged 4 commits into from
Oct 29, 2024
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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/Widget/CheckboxList/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/Widget/RadioList/RadioList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
75 changes: 75 additions & 0 deletions tests/Widget/CheckboxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div>
<div$expected>
<label><input type="checkbox" name="test[]" value="1"> One</label>
</div>
<div$expected>
<label><input type="checkbox" name="test[]" value="2"> Two</label>
</div>
</div>
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
<div>
<div$expected>
<label><input type="checkbox" name="test[]" value="1"> One</label>
</div>
<div$expected>
<label><input type="checkbox" name="test[]" value="2"> Two</label>
</div>
</div>
HTML,
$html,
);
}

public function testStringable(): void
{
$this->assertSame(
Expand All @@ -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([]));
Expand Down
75 changes: 75 additions & 0 deletions tests/Widget/RadioListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div>
<div$expected>
<label><input type="radio" name="test" value="1"> One</label>
</div>
<div$expected>
<label><input type="radio" name="test" value="2"> Two</label>
</div>
</div>
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
<div>
<div$expected>
<label><input type="radio" name="test" value="1"> One</label>
</div>
<div$expected>
<label><input type="radio" name="test" value="2"> Two</label>
</div>
</div>
HTML,
$html,
);
}

public function testStringable(): void
{
$this->assertSame(
Expand All @@ -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));
Expand Down
Loading