Skip to content

Commit

Permalink
Add HasLabelItemClass trait and corresponding test. (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 6, 2024
1 parent 3deded7 commit 8677a7d
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 260 deletions.
28 changes: 28 additions & 0 deletions src/Attribute/Custom/HasLabelItemClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Custom;

/**
* Is used by widgets that implement label item class method.
*/
trait HasLabelItemClass
{
protected string $labelItemClass = '';

/**
* Set a new label item class that will be used for the label item.
*
* @param string $value The label item class to be used for the label item.
*
* @return static A new instance of the current class with the specified label item class.
*/
public function labelItemClass(string $value): static
{
$new = clone $this;
$new->labelItemClass = $value;

return $new;
}
}
15 changes: 7 additions & 8 deletions src/Input/Base/AbstractChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class AbstractChoiceList extends Element implements CheckedValueInterfa
use Attribute\Custom\HasContainer;
use Attribute\Custom\HasEnclosedByLabel;
use Attribute\Custom\HasLabel;
use Attribute\Custom\HasLabelItemClass;
use Attribute\Custom\HasSeparator;
use Attribute\Custom\HasTemplate;
use Attribute\Custom\HasWidgetValidation;
Expand All @@ -46,6 +47,7 @@ public function loadDefaultDefinitions(): array
{
return [
'container()' => [true],
'id()' => [$this->generateId('choice-')],
'template()' => ['{label}\n{tag}'],
];
}
Expand All @@ -64,13 +66,10 @@ protected function run(): string

$attributes = $this->attributes;
$containerAttributes = $this->containerAttributes;
$id = $this->generateId('choice-');
$items = $this->items;
$labelTag = '';
$listItems = [];

if ($this->ariaDescribedBy === true) {
$attributes['aria-describedby'] = "$id-help";
$attributes['aria-describedby'] = "$this->id-help";
}

if (array_key_exists('autofocus', $attributes) && is_bool($attributes['autofocus'])) {
Expand All @@ -87,12 +86,12 @@ protected function run(): string

unset($attributes['value']);

foreach ($items as $item) {
foreach ($this->items as $item) {
$listItem = $item
->attributes($attributes)
->checked($this->checkedValue === $item->getValue())
->enclosedByLabel($this->enclosedByLabel)
->id(null)
->labelClass($this->labelItemClass)
->separator($this->separator);

if ($this->enclosedByLabel === true) {
Expand All @@ -107,7 +106,7 @@ protected function run(): string
true => Tag::widget()
->attributes($containerAttributes)
->content($choiceTag)
->id($id)
->id($this->id)
->tagName($this->containerTag)
->render(),
default => $choiceTag,
Expand All @@ -116,7 +115,7 @@ protected function run(): string
return $this->renderTemplate(
$this->template,
[
'{label}' => $this->renderLabelTag($id),
'{label}' => $this->renderLabelTag($this->id),
'{tag}' => $tag,
],
);
Expand Down
2 changes: 1 addition & 1 deletion src/Input/Base/AbstractInputChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function buildChoiceTag(string $type): string
if ($this->enclosedByLabel) {
$tag = $this->renderEnclosedByLabel($tag);
} else {
$labelTag = $this->renderLabelTag($this->getId() ?? '');
$labelTag = $this->renderLabelTag($id);
}

$choiceTag = $this->prepareTemplate($tag, $labelTag);
Expand Down
20 changes: 20 additions & 0 deletions tests/Attribute/Custom/HasLabelItemClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Attribute\Custom;

use PHPForge\Html\Attribute\Custom\HasLabelItemClass;
use PHPUnit\Framework\TestCase;

final class HasLabelItemClassTest extends TestCase
{
public function testImmutability(): void
{
$instance = new class () {
use HasLabelItemClass;
};

$this->assertNotSame($instance, $instance->labelItemClass(''));
}
}
2 changes: 1 addition & 1 deletion tests/Input/Checkbox/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testAttributes(): void
<<<HTML
<input class="class" id="checkbox-6582f2d099e8b" type="checkbox">
HTML,
Checkbox::widget()->attributes(['class' => 'class'])->id('checkbox-6582f2d099e8b')->render()
Checkbox::widget()->attributes(['class' => 'class', 'id' => 'checkbox-6582f2d099e8b'])->render()
);
}

Expand Down

0 comments on commit 8677a7d

Please sign in to comment.