Skip to content

Commit

Permalink
CVA apply function accept null arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
WebMamba committed Mar 1, 2024
1 parent 2ddc47b commit 8ebf469
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/TwigComponent/src/CVA.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function __construct(
) {
}

public function apply(array $recipes, string ...$classes): string
public function apply(array $recipes, ?string ...$classes): string
{
return trim($this->resolve($recipes).' '.implode(' ', $classes));
return trim($this->resolve($recipes).' '.implode(' ', array_filter($classes)));
}

public function resolve(array $recipes): string
Expand Down
23 changes: 23 additions & 0 deletions src/TwigComponent/tests/Unit/CVATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ public function testApply(): void
$this->assertEquals('font-semibold border rounded text-primary text-sm text-red-500', $recipe->apply(['colors' => 'primary', 'sizes' => 'sm']));
}

public function testApplyWithNullString(): void
{
$recipe = new CVA('font-semibold border rounded', [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'sizes' => [
'sm' => 'text-sm',
'md' => 'text-md',
'lg' => 'text-lg',
],
], [
[
'colors' => ['primary'],
'sizes' => ['sm'],
'class' => 'text-red-500',
],
]);

$this->assertEquals('font-semibold border rounded text-primary text-sm text-red-500 flex justify-center', $recipe->apply(['colors' => 'primary', 'sizes' => 'sm'], 'flex', null, 'justify-center'));
}

public static function recipeProvider(): iterable
{
yield 'base null' => [
Expand Down

0 comments on commit 8ebf469

Please sign in to comment.