filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
17.65k stars 2.76k forks source link

hidden() not working in custom filter forms #1473

Closed michaelgatuma closed 2 years ago

michaelgatuma commented 2 years ago

Package

filament/tables

Package Version

v2.9.17

Laravel Version

v8.82.0

Livewire Version

v2.10.1

PHP Version

PHP 8.0.12

Bug description

I tried to use a hidden() function in custom filters but it did not work. The function was using a closure that references the value of another custom filter to determine the binary of either true or false.

Steps to reproduce

Filter::make('affiliatable_type')->form([
                Select::make('affiliate_type')
                    ->label('Filter by Affiliation')
                    ->reactive()
                    ->afterStateUpdated(function (Closure $set, $state) {
                        $set('affiliateType', $state);
                    })
                    ->options([
                        'County' => 'County',
                        'NGO' => 'NGO',
                        'Institution' => 'Institution',
                    ]),
            ])->query(
                function (Builder $query, array $data): Builder {
                    return $this->filterByAffiliateType($query, $data);
                }
            ),
            Filter::make('county')
                ->hidden(fn(Closure $get) => $get('affiliate_type') !== "County") //This line has a closure that references the first field 👆
                ->form([
                    Select::make('affiliation_id')
                        ->label('Select County')
                        ->options(County::all()->pluck('name', 'id')->toArray()),
                ])->query(
                    function (Builder $query, array $data): Builder {
                        return $this->filterByAffiliation($query, $data);
                    }
                ),

Relevant log output

No response

danharrin commented 2 years ago

If filters are dependant, please combine them into 1 filter. This will fix your issue too.