Skip to content

Commit

Permalink
Fix choice list. (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 6, 2024
1 parent 18dd6f1 commit 3deded7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/Input/Base/AbstractChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPForge\Html\Input\CheckedValueInterface;
use PHPForge\Html\Input\Contract\ChoiceInterface;
use PHPForge\Html\Input\Radio;
use PHPForge\Html\Label;
use PHPForge\Html\Tag;
use PHPForge\Widget\Element;

Expand All @@ -24,6 +23,7 @@ abstract class AbstractChoiceList extends Element implements CheckedValueInterfa
use Attribute\Custom\HasEnclosedByLabel;
use Attribute\Custom\HasLabel;
use Attribute\Custom\HasSeparator;
use Attribute\Custom\HasTemplate;
use Attribute\Custom\HasWidgetValidation;
use Attribute\HasClass;
use Attribute\HasId;
Expand All @@ -46,6 +46,7 @@ public function loadDefaultDefinitions(): array
{
return [
'container()' => [true],
'template()' => ['{label}\n{tag}'],
];
}

Expand Down Expand Up @@ -101,25 +102,23 @@ protected function run(): string
$listItems[] = $listItem;
}

$listTagItems = implode(PHP_EOL, $listItems);

if ($this->labelContent !== '') {
$labelTag = Label::widget()
->attributes($this->labelAttributes)
->content($this->labelContent)
->for($id)
->render() . PHP_EOL;
}

return match ($this->container) {
true => $labelTag .
Tag::widget()
->attributes($containerAttributes)
->content($listTagItems)
->id($id)
->tagName($this->containerTag)
->render(),
default => $listTagItems,
$choiceTag = implode(PHP_EOL, $listItems);
$tag = match ($this->container) {
true => Tag::widget()
->attributes($containerAttributes)
->content($choiceTag)
->id($id)
->tagName($this->containerTag)
->render(),
default => $choiceTag,
};

return $this->renderTemplate(
$this->template,
[
'{label}' => $this->renderLabelTag($id),
'{tag}' => $tag,
],
);
}
}
51 changes: 51 additions & 0 deletions tests/Input/ChoiceList/CheckboxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,31 @@ public function testName(): void
);
}

public function testNotLabel(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="choice-list-65858c272ea89">
<input name="CheckboxForm[text]" type="checkbox" value="1">
<label>Female</label>
<input name="CheckboxForm[text]" type="checkbox" value="2">
<label>Male</label>
</div>
HTML,
Choicelist::widget()
->id('choice-list-65858c272ea89')
->items(
Checkbox::widget()->labelContent('Female')->value(1),
Checkbox::widget()->labelContent('Male')->value(2),
)
->labelContent('Select your gender?')
->labelClass('class')
->name('CheckboxForm[text]')
->notLabel()
->render(),
);
}

public function testRender(): void
{
Assert::equalsWithoutLE(
Expand Down Expand Up @@ -451,6 +476,32 @@ public function testTabindex(): void
);
}

public function testTemplate(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="choice-list-65858c272ea89">
<input name="CheckboxForm[text]" type="checkbox" value="red">
<label>Red</label>
<input name="CheckboxForm[text]" type="checkbox" value="blue">
<label>Blue</label>
</div>
<label class="class" for="choice-list-65858c272ea89">Select your gender?</label>
HTML,
Choicelist::widget()
->id('choice-list-65858c272ea89')
->items(
Checkbox::widget()->labelContent('Red')->value('red'),
Checkbox::widget()->labelContent('Blue')->value('blue'),
)
->name('CheckboxForm[text]')
->labelContent('Select your gender?')
->labelClass('class')
->template('{tag}\n{label}')
->render(),
);
}

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

0 comments on commit 3deded7

Please sign in to comment.