FilamentF
Filament3y ago
14 replies
Mark Chaney

SelectFilter hidden()

So I have this SelectFilter that I want hidden based on $activeTab that is set by getTabs(). I have
SelectFilter::make('status')
    ->label('Status')
    ->hidden(function () {
        ray($this->activeTab)->label('status');

        return in_array($this->activeTab, ['all', 'active', 'inactive']);
    })
    ->options([
        'Active' => 'Active',
        'Prospect' => auth()->user()->isInternal() ? 'Prospect' : 'Draft',
        'Inactive' => 'Inactive',
    ])->query(function (Builder $query, array $data): Builder {
        $value = $data['value'] ?? null;

        return match ($value) {
            'Active' => $query->where('active', 1),
            'Prospect' => $query->where('active', 0)->where('draft', 1),
            'Inactive' => $query->where('active', 0)->where('draft', 0),
            default => $query,
        };
    }),
What I am finding odd is that while ray() is showing a matching activeTab value, its not hidding on first ocurrence when switching to one of the tabs that it should be hidden on even though ray shows a value that would be true. The in_array() is returning the proper true/false too. Suggestions? I had this issue with toggleable columns at one point and i started doing a $this->resetPage() when a tab was changed and that solved it, but this appears to fire before the filter hidden() logic is ran. Also, the hidden() method is seeing the right values, so thats not it.
Solution
@archilex this is working great. thanks!
->hidden(function () {
    $tabsToHideFilter = ['active', 'inactive', 'draft'];
    $shouldHideFilter = in_array($this->activeTab, $tabsToHideFilter);

    if ($shouldHideFilter) {
        $this->tableFilters['status'] = null;
    }

    return $shouldHideFilter;
})
public function updatedActiveTab(): void
{
    unset($this->cachedForms['tableFiltersForm']);
    $this->resetPage();
}
Was this page helpful?