Skip to content

Commit

Permalink
[1.x] Allow setting title using a closure (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
RVxLab authored Nov 28, 2023
1 parent 55f6884 commit ba0dcf7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function layout(string $layout): void
/**
* Define the component's title.
*/
function title(string $title): void
function title(Closure|string $title): void
{
CompileContext::instance()->title = $title;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Actions/ReturnTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Livewire\Volt\Actions;

use Closure;
use Illuminate\Container\Container;
use Livewire\Volt\CompileContext;
use Livewire\Volt\Component;
use Livewire\Volt\Contracts\Action;
Expand All @@ -13,6 +15,12 @@ class ReturnTitle implements Action
*/
public function execute(CompileContext $context, Component $component, array $arguments): ?string
{
if ($context->title instanceof Closure) {
return Container::getInstance()->call(
Closure::bind($context->title, $component, $component::class),
);
}

return $context->title;
}
}
2 changes: 1 addition & 1 deletion src/CompileContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
public array $variables,
public array $state,
public ?string $layout,
public ?string $title,
public Closure|string|null $title,
public ?Closure $listeners,
public array $inlineListeners,
public Closure|array $rules,
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/Actions/ReturnTitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,32 @@

expect($result)->toBe('my title');
});

it('returns a static title using a closure', function () {
$context = CompileContext::make();

$context->title = fn () => 'my title from a closure';

$component = new class extends Component
{
};

$result = (new ReturnTitle)->execute($context, $component, []);

expect($result)->toBe('my title from a closure');
});

it('returns a computed title using a closure', function () {
$context = CompileContext::make();

$context->title = fn () => "welcome back $this->username";

$component = new class extends Component
{
public string $username = 'Tom';
};

$result = (new ReturnTitle)->execute($context, $component, []);

expect($result)->toBe('welcome back Tom');
});
12 changes: 12 additions & 0 deletions tests/Feature/CompilerContext/TitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@

expect($context->title)->toBe('my custom title');
});

it('may be set using closures', function () {
$context = CompileContext::instance();

title(fn () => 'my custom title from a closure');

expect($context->title)
->toBeCallable()
->and($context->title)
->resolve()
->toBe('my custom title from a closure');
});

0 comments on commit ba0dcf7

Please sign in to comment.