Skip to content
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

Fix CheckboxList does not display descriptions when using an enum for dynamic options #14501

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions packages/forms/src/Components/Concerns/HasDescriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ public function getDescriptions(): array
$descriptions = $descriptions->toArray();
}

$options = $this->evaluate($this->options);

if (
empty($descriptions) &&
is_string($this->options) &&
enum_exists($this->options) &&
is_a($this->options, HasDescription::class, allow_string: true)
is_string($options) &&
enum_exists($options) &&
is_a($options, HasDescription::class, allow_string: true)
) {
$descriptions = array_reduce($this->options::cases(), function (array $carry, HasDescription & UnitEnum $case): array {
$descriptions = array_reduce($options::cases(), function (array $carry, HasDescription & UnitEnum $case): array {
if (filled($description = $case->getDescription())) {
$carry[$case?->value ?? $case->name] = $description;
}
Expand Down
93 changes: 93 additions & 0 deletions tests/src/Forms/Components/CheckboxListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Form;
use Filament\Support\Contracts\HasDescription;
use Filament\Support\Contracts\HasLabel;
use Filament\Tests\Forms\Fixtures\Livewire;
use Filament\Tests\TestCase;
use Illuminate\Contracts\View\View;

use function Filament\Tests\livewire;

uses(TestCase::class);

it('can render labels when enum implements HasLabel', function () {
expect(Options::class)->toImplement(HasLabel::class);
livewire(TestComponentWithFixedOptions::class)
->assertSeeText('Foo Label')
->assertSeeText('Bar Label');
});

it('can render descriptions when enum implements HasDescription', function () {
expect(Options::class)->toImplement(HasDescription::class);
livewire(TestComponentWithFixedOptions::class)
->assertSeeText('Foo Description')
->assertSeeText('Bar Description');
});

it('can render labels and descriptions for dynamic options', function () {
expect(Options::class)->toImplement(HasLabel::class);
expect(Options::class)->toImplement(HasDescription::class);
livewire(TestComponentWithDynamicOptions::class)
->assertSeeText('Foo Label')
->assertSeeText('Bar Label')
->assertSeeText('Foo Description')
->assertSeeText('Bar Description');
});

enum Options: string implements HasDescription, HasLabel
{
case FOO = 'foo';
case BAR = 'bar';

public function getDescription(): ?string
{
return match ($this) {
self::FOO => 'Foo Description',
self::BAR => 'Bar Description',
};
}

public function getLabel(): ?string
{
return match ($this) {
self::FOO => 'Foo Label',
self::BAR => 'Bar Label',
};
}
}

class TestComponentWithFixedOptions extends Livewire
{
public function form(Form $form): Form
{
return $form
->schema([
CheckboxList::make('test')->options(Options::class),
]);
}

public function render(): View
{
return view('forms.fixtures.form');
}
}

class TestComponentWithDynamicOptions extends Livewire
{
public function form(Form $form): Form
{
return $form
->schema([
CheckboxList::make('test')->options(function () {
return Options::class;
}),
]);
}

public function render(): View
{
return view('forms.fixtures.form');
}
}
Loading