Skip to content

Commit

Permalink
Merge pull request #33 from pelmered/fix/resolve-closures-in-getLabel
Browse files Browse the repository at this point in the history
Resolve closures in getLabel()
  • Loading branch information
pelmered authored May 10, 2024
2 parents ca6dd35 + ee9a728 commit b9e3173
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function maxValue(mixed $value): static

public function getLabel(): string
{
return $this->label
return $this->evaluate($this->label)
?? (string)str($this->getName())
->afterLast('.')
->kebab()
Expand Down
19 changes: 17 additions & 2 deletions tests/FormInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testCurrencySymbolPlacementAfter()
$field = $component->getComponent('data.price');
$this->assertEquals('$', $field->getSuffixLabel());
$this->assertNull($field->getPrefixLabel());
}
}

public function testCurrencySymbolPlacementBefore()
{
Expand Down Expand Up @@ -179,7 +179,6 @@ public function testAllowLabelToBeOverrided(): void
{
$field = (new MoneyInput('price'))->label('Custom Label');


$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
Expand All @@ -189,4 +188,20 @@ public function testAllowLabelToBeOverrided(): void
$field = $component->getComponent('data.price');
$this->assertEquals('Custom Label', $field->getLabel());
}

public function testResolveLabelClosures(): void
{
$field = (new MoneyInput('price'))->label(function () {
return 'Custom Label in Closure';
});

$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
$field,
])->fill([$field->getName() => 45345]);

$field = $component->getComponent('data.price');
$this->assertEquals('Custom Label in Closure', $field->getLabel());
}
}

0 comments on commit b9e3173

Please sign in to comment.