Skip to content

Commit

Permalink
Allow override label in Checkbox (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 2, 2023
1 parent 39cd245 commit 3a825d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/Field/Base/PartsField.php
Expand Up @@ -31,6 +31,7 @@ abstract class PartsField extends BaseField
protected string $templateBegin = "{label}\n{input}";
protected string $templateEnd = "{input}\n{hint}\n{error}";
protected string $template = "{label}\n{input}\n{hint}\n{error}";
protected ?string $label = null;
protected ?bool $hideLabel = null;

/**
Expand Down Expand Up @@ -236,7 +237,7 @@ final public function addLabelClass(?string ...$class): static
final public function label(?string $content): static
{
$new = clone $this;
$new->labelConfig['content()'] = [$content];
$new->label = $content;
return $new;
}

Expand Down Expand Up @@ -468,7 +469,12 @@ private function makeContent(string $template, array $parts): string

private function generateLabel(): string
{
$label = Label::widget([], $this->labelConfig);
$labelConfig = $this->labelConfig;
if ($this->label !== null) {
$labelConfig['content()'] = [$this->label];
}

$label = Label::widget([], $labelConfig);

$labelAttributes = $this->labelAttributes;
if (!empty($labelAttributes)) {
Expand Down
3 changes: 1 addition & 2 deletions src/Field/Checkbox.php
Expand Up @@ -236,9 +236,8 @@ protected function generateInput(): string

$checkbox = Html::checkbox($this->getName(), $inputValue, $inputAttributes);

$label = $this->inputLabel ?? $this->getInputData()->getLabel();

if ($this->enclosedByLabel) {
$label = $this->inputLabel ?? $this->label ?? $this->getInputData()->getLabel();
$checkbox = $checkbox
->label($label, $this->inputLabelAttributes)
->labelEncode($this->inputLabelEncode);
Expand Down
5 changes: 2 additions & 3 deletions tests/Field/CheckboxTest.php
Expand Up @@ -449,10 +449,9 @@ public function testDisabled(): void

public function testAriaDescibedBy(): void
{
$inputData = new PureInputData('test-name', label: 'Blue color');

$result = Checkbox::widget()
->inputData($inputData)
->name('test-name')
->label('Blue color')
->ariaDescribedBy('hint')
->uncheckValue(null)
->render();
Expand Down

0 comments on commit 3a825d3

Please sign in to comment.