Skip to content

Commit

Permalink
Refactor enclosedByLabel method in AbstractInputChoice. (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 10, 2024
1 parent 2b2ee76 commit 6863022
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Input/Base/AbstractInputChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected function buildChoiceTag(string $type): string

/** @var string $id */
$id = $attributes['id'] ?? $this->generateId("$type-");
$labelFor = $this->labelFor ?? $id;

if ($this->ariaDescribedBy === true) {
$attributes['aria-describedby'] = "$id-help";
Expand All @@ -95,9 +96,9 @@ protected function buildChoiceTag(string $type): string
$tag = Tag::widget()->attributes($attributes)->id($id)->tagName('input')->type($type)->value($value)->render();

if ($this->enclosedByLabel) {
$tag = $this->renderEnclosedByLabel($tag);
$tag = $this->renderEnclosedByLabel($tag, $labelFor);
} else {
$labelTag = $this->renderLabelTag($id);
$labelTag = $this->renderLabelTag($labelFor);
}

$choiceTag = $this->prepareTemplate($tag, $labelTag);
Expand All @@ -118,7 +119,7 @@ private function prepareTemplate(string $tag, string $labelTag): string
return $this->renderTemplate($this->template, $tokenValues);
}

private function renderEnclosedByLabel(string $tag): string
private function renderEnclosedByLabel(string $tag, string $labelFor): string
{
if ($this->labelContent === '' || $this->isNotLabel()) {
return $tag;
Expand All @@ -133,7 +134,7 @@ private function renderEnclosedByLabel(string $tag): string
$this->labelContent,
$this->separator,
)
->for($this->getId())
->for($labelFor)
->render();
}
}
29 changes: 29 additions & 0 deletions tests/Input/Checkbox/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ public function testEnclosedByLabelWithLabelContentEmpty(): void
);
}

public function testEnclosedByLabelWithLabelFor(): void
{
Assert::equalsWithoutLE(
<<<HTML
<label for="label-for">
<input id="checkbox-6582f2d099e8b" type="checkbox">
Red
</label>
HTML,
Checkbox::widget()
->enclosedByLabel(true)
->id('checkbox-6582f2d099e8b')
->labelContent('Red')
->labelFor('label-for')
->render()
);
}

public function testForm(): void
{
Assert::equalsWithoutLE(
Expand Down Expand Up @@ -293,6 +311,17 @@ public function testLabelClass(): void
);
}

public function testLabelFor(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="checkbox-6582f2d099e8b" type="checkbox">
<label for="label-for">Red</label>
HTML,
Checkbox::widget()->id('checkbox-6582f2d099e8b')->labelContent('Red')->labelFor('label-for')->render()
);
}

public function testLoadDefaultDefinitionsContainerWithFalse(): void
{
Assert::equalsWithoutLE(
Expand Down
29 changes: 29 additions & 0 deletions tests/Input/Radio/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ public function testEnclosedByLabelWithLabelContentEmpty(): void
);
}

public function testEnclosedByLabelWithLabelFor(): void
{
Assert::equalsWithoutLE(
<<<HTML
<label for="label-for">
<input id="checkbox-6582f2d099e8b" type="radio">
Red
</label>
HTML,
Radio::widget()
->enclosedByLabel(true)
->id('checkbox-6582f2d099e8b')
->labelContent('Red')
->labelFor('label-for')
->render()
);
}

public function testForm(): void
{
Assert::equalsWithoutLE(
Expand Down Expand Up @@ -270,6 +288,17 @@ public function testLabelClass(): void
);
}

public function testLabelFor(): void
{
Assert::equalsWithoutLE(
<<<HTML
<input id="checkbox-6582f2d099e8b" type="radio">
<label for="label-for">Red</label>
HTML,
Radio::widget()->id('checkbox-6582f2d099e8b')->labelContent('Red')->labelFor('label-for')->render()
);
}

public function testLoadDefinitionContainerWithFalse(): void
{
Assert::equalsWithoutLE(
Expand Down

0 comments on commit 6863022

Please sign in to comment.