Skip to content

[Twig] Resolving boolean as variant keys #1710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/TwigComponent/src/CVA.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public function apply(array $recipes, ?string ...$additionalClasses): string

// Resolve recipes against variants
foreach ($recipes as $recipeName => $recipeValue) {
if (\is_bool($recipeValue)) {
$recipeValue = $recipeValue ? 'true' : 'false';
}
$recipeClasses = $this->variants[$recipeName][$recipeValue] ?? [];
$classes = [...$classes, ...(array) $recipeClasses];
}
Expand Down
128 changes: 128 additions & 0 deletions src/TwigComponent/tests/Unit/CVATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,134 @@ public static function recipeProvider(): iterable
[],
'font-semibold border rounded text-primary text-sm rounded-md',
];

yield 'boolean string variants true / true' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'true' => 'disable',
],
],
],
['colors' => 'primary', 'disabled' => true],
'text-primary disable',
];

yield 'boolean string variants true / false' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'true' => 'disable',
],
],
],
['colors' => 'primary', 'disabled' => false],
'text-primary',
];

yield 'boolean string variants false / true' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'false' => 'disable',
],
],
],
['colors' => 'primary', 'disabled' => true],
'text-primary',
];

yield 'boolean string variants false / false' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'false' => 'disable',
],
],
],
['colors' => 'primary', 'disabled' => false],
'text-primary disable',
];

yield 'boolean string variants missing' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'true' => 'disable',
],
],
],
['colors' => 'primary'],
'text-primary',
];

yield 'boolean list variants true' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'true' => ['disable', 'opacity-50'],
],
],
],
['colors' => 'primary', 'disabled' => true],
'text-primary disable opacity-50',
];

yield 'boolean list variants false' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'true' => ['disable', 'opacity-50'],
],
],
],
['colors' => 'primary', 'disabled' => false],
'text-primary',
];

yield 'boolean list variants missing' => [
[
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'disabled' => [
'true' => ['disable', 'opacity-50'],
],
],
],
['colors' => 'primary'],
'text-primary',
];
}

/**
Expand Down