#ToggleButtons default not working

7 messages · Page 1 of 1 (latest)

cosmic matrix
#

When I try to set a default to ToggleButtons form with multiple options, it does not select it.

  ToggleButtons::make('technologies')
                    ->default('tailwind')
                    ->multiple()
                    ->options([
                        'tailwind' => 'Tailwind CSS',
                        'alpine' => 'Alpine.js',
                        'laravel' => 'Laravel',
                        'livewire' => 'Laravel Livewire',
                    ]),
frosty krakenBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

hollow spire
#

Did you add $this->form->fill() in the mount method?

cosmic matrix
#

It works but I noticed that this piece of code messed up with setting the default state: ```class ToggleButtons extends Field implements Contracts\CanDisableOptions
{
use Concerns\CanDisableOptions;
use Concerns\CanDisableOptionsWhenSelectedInSiblingRepeaterItems;
use Concerns\CanFixIndistinctState;
use Concerns\HasColors;
use Concerns\HasExtraInputAttributes;
use Concerns\HasGridDirection;
use Concerns\HasIcons;
use Concerns\HasOptions;

public const GROUPED_VIEW = 'filament-forms::components.toggle-buttons.grouped';

protected bool | Closure $isMultiple = false;

/**
 * @var view-string
 */
protected string $view = 'filament-forms::components.toggle-buttons.index';

protected bool | Closure $isInline = false;

protected function setUp(): void
{
    parent::setUp();

    $this->default(fn (ToggleButtons $component): mixed => $component->isMultiple() ? [] : null);

    $this->afterStateHydrated(static function (ToggleButtons $component, $state): void {
        if (! $component->isMultiple()) {
            return;
        }

        if (is_array($state)) {
            return;
        }

        $component->state([]);
    });
}
#

afterStateHydrated is called on the component during its first lifecycle and $state is null so it sets $component->state([]) at the end of the function to an empty array overrding defaut state defined in the components builder php ToggleButtons::make('technologies') ->default('tailwind')

magic flint
#
                    ->default(['tailwind', 'alpine'])
                    ->multiple()
                    ->options([
                        'tailwind' => 'Tailwind CSS',
                        'alpine' => 'Alpine.js',
                        'laravel' => 'Laravel',
                        'livewire' => 'Laravel Livewire',
                    ])

use default data in array because you use multiple

cosmic matrix
#

Okay works perfectly in a tradictional Form. Im in a filtersForm. php public function filtersForm(Form $form): Form Might be an issue. I will investigate more.