From 1d295c884dabf650b372bb2dc941e5949560376f Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 21 May 2025 08:49:43 +0100 Subject: [PATCH] clean up --- .../support/src/Components/ViewComponent.php | 11 ++++---- tests/src/Forms/Components/ViewTest.php | 28 +++++++------------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/packages/support/src/Components/ViewComponent.php b/packages/support/src/Components/ViewComponent.php index 3df13db589..8d4944f754 100644 --- a/packages/support/src/Components/ViewComponent.php +++ b/packages/support/src/Components/ViewComponent.php @@ -22,7 +22,7 @@ abstract class ViewComponent extends Component implements Htmlable protected string | Closure | null $defaultView = null; /** - * @var array|Closure> + * @var array | Closure> */ protected array $viewData = []; @@ -66,7 +66,7 @@ abstract class ViewComponent extends Component implements Htmlable } /** - * @param array|Closure $data + * @param array | Closure $data */ public function viewData(array | Closure $data): static { @@ -104,9 +104,10 @@ abstract class ViewComponent extends Component implements Htmlable */ public function getViewData(): array { - return Arr::mapWithKeys($this->viewData, function (mixed $value) { - return $this->evaluate($value); - }); + return Arr::mapWithKeys( + $this->viewData, + fn (mixed $data): array => $this->evaluate($data) ?? [], + ); } public function toHtml(): string diff --git a/tests/src/Forms/Components/ViewTest.php b/tests/src/Forms/Components/ViewTest.php index fe924fb139..01590a212c 100644 --- a/tests/src/Forms/Components/ViewTest.php +++ b/tests/src/Forms/Components/ViewTest.php @@ -6,38 +6,30 @@ use Filament\Tests\TestCase; uses(TestCase::class); it('can have view data', function () { - $view = View::make('test') + $component = View::make('test') ->viewData([ 'key_a' => 'Value A', 'key_b' => 'Value B', ]) ->viewData([]); - expect($view) + expect($component) ->getViewData()->toBe([ 'key_a' => 'Value A', 'key_b' => 'Value B', ]); }); -it('can have view data with closure on numeric keys', function () { - $view = View::make('test') - ->viewData([ - 'key_a' => 'Value A', - ]) - ->viewData(function () { - // Closure result will be merged with the top-level array... - return ['string_keyed_closure' => 'string_keyed_closure']; - }) - ->viewData(function () { - // Closure result will be merged with the top-level array... - return ['string_keyed_closure_b' => 'string_keyed_closure_b']; - }); +it('can have view data inside closures', function () { + $component = View::make('test') + ->viewData(['key_a' => 'Value A']) + ->viewData(fn (): array => ['closure_key_a' => 'Closure Value A']) + ->viewData(fn (): array => ['closure_key_b' => 'Closure Value B']); - expect($view) + expect($component) ->getViewData()->toBe([ 'key_a' => 'Value A', - 'string_keyed_closure' => 'string_keyed_closure', - 'string_keyed_closure_b' => 'string_keyed_closure_b', + 'closure_key_a' => 'Closure Value A', + 'closure_key_b' => 'Closure Value B', ]); });