Skip to content

Commit 6b3255a

Browse files
committed
header/footer filters
1 parent e3013a7 commit 6b3255a

File tree

8 files changed

+124
-44
lines changed

8 files changed

+124
-44
lines changed

resources/views/components/tools/filter-pills.blade.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@if ($theme === 'tailwind')
88
<div>
9-
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedFiltersWithValues())
9+
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedVisibleFiltersForPills())
1010
<div class="mb-4 px-4 md:p-0">
1111
<small class="text-gray-700 dark:text-white">@lang('Applied Filters'):</small>
1212

@@ -16,6 +16,7 @@
1616
@endphp
1717

1818
@continue(is_null($filter))
19+
@continue($filter->isHiddenFromPills())
1920

2021
<span
2122
wire:key="{{ $component->getTableName() }}-filter-pill-{{ $filter->getKey() }}"
@@ -49,7 +50,7 @@ class="focus:outline-none active:outline-none"
4950
</div>
5051
@elseif ($theme === 'bootstrap-4')
5152
<div>
52-
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedFiltersWithValues())
53+
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedVisibleFiltersForPills())
5354
<div class="mb-3">
5455
<small>@lang('Applied Filters'):</small>
5556

@@ -59,6 +60,7 @@ class="focus:outline-none active:outline-none"
5960
@endphp
6061

6162
@continue(is_null($filter))
63+
@continue($filter->isHiddenFromPills())
6264

6365
<span
6466
wire:key="{{ $component->getTableName() }}-filter-pill-{{ $filter->getKey() }}"
@@ -91,7 +93,7 @@ class="badge badge-pill badge-light"
9193
</div>
9294
@elseif ($theme === 'bootstrap-5')
9395
<div>
94-
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedFiltersWithValues())
96+
@if ($component->filtersAreEnabled() && $component->filterPillsAreEnabled() && $component->hasAppliedVisibleFiltersForPills())
9597
<div class="mb-3">
9698
<small>@lang('Applied Filters'):</small>
9799

@@ -101,6 +103,7 @@ class="badge badge-pill badge-light"
101103
@endphp
102104

103105
@continue(is_null($filter))
106+
@continue($filter->isHiddenFromPills())
104107

105108
<span
106109
wire:key="{{ $component->getTableName() }}-filter-pill-{{ $filter->getKey() }}"

resources/views/components/tools/toolbar.blade.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ class="inline-flex justify-center w-full rounded-md border border-gray-300 shado
7676
>
7777
@lang('Filters')
7878

79-
@if ($component->hasAppliedFiltersWithValues())
79+
@if ($count = $component->getFilterBadgeCount())
8080
<span class="ml-1 inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium leading-4 bg-indigo-100 text-indigo-800 capitalize dark:bg-indigo-200 dark:text-indigo-900">
81-
{{ $component->getAppliedFiltersWithValuesCount()}}
81+
{{ $count }}
8282
</span>
8383
@endif
8484

@@ -106,7 +106,7 @@ class="origin-top-left absolute left-0 mt-2 w-full md:w-56 rounded-md shadow-lg
106106
aria-labelledby="filters-menu"
107107
>
108108
@foreach($component->getFilters() as $filter)
109-
@if($filter->isVisible())
109+
@if($filter->isVisibleInMenu())
110110
<div class="py-1" role="none">
111111
<div class="block px-4 py-2 text-sm text-gray-700 space-y-1" role="menuitem">
112112
<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}"
@@ -120,7 +120,7 @@ class="block text-sm font-medium leading-5 text-gray-700 dark:text-white">
120120
@endif
121121
@endforeach
122122

123-
@if ($component->hasAppliedFiltersWithValues())
123+
@if ($component->hasAppliedVisibleFiltersWithValuesThatCanBeCleared())
124124
<div class="block px-4 py-3 text-sm text-gray-700 dark:text-white" role="menuitem">
125125
<button
126126
wire:click.prevent="setFilterDefaults"
@@ -307,7 +307,7 @@ class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150
307307
>
308308
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-6 px-4 md:p-0 mb-6">
309309
@foreach($component->getFilters() as $filter)
310-
@if($filter->isVisible())
310+
@if($filter->isVisibleInMenu())
311311
<div class="space-y-1">
312312
<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}"
313313
class="block text-sm font-medium leading-5 text-gray-700 dark:text-white">
@@ -394,9 +394,9 @@ class="btn dropdown-toggle d-block w-100 d-md-inline"
394394
>
395395
@lang('Filters')
396396

397-
@if ($component->hasAppliedFiltersWithValues())
397+
@if ($count = $component->getFilterBadgeCount())
398398
<span class="badge badge-info">
399-
{{ $component->getAppliedFiltersWithValuesCount()}}
399+
{{ $count }}
400400
</span>
401401
@endif
402402

@@ -412,7 +412,7 @@ class="dropdown-menu w-100 mt-md-5"
412412
role="menu"
413413
>
414414
@foreach($component->getFilters() as $filter)
415-
@if($filter->isVisible())
415+
@if($filter->isVisibleInMenu())
416416
<div wire:key="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}" class="p-2">
417417
<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}" class="mb-2">
418418
{{ $filter->getName() }}
@@ -423,7 +423,7 @@ class="dropdown-menu w-100 mt-md-5"
423423
@endif
424424
@endforeach
425425

426-
@if ($component->hasAppliedFiltersWithValues())
426+
@if ($component->hasAppliedVisibleFiltersWithValuesThatCanBeCleared())
427427
<div class="dropdown-divider"></div>
428428

429429
<button
@@ -551,7 +551,7 @@ class="form-control"
551551
<div class="container">
552552
<div class="row">
553553
@foreach($component->getFilters() as $filter)
554-
@if($filter->isVisible())
554+
@if($filter->isVisibleInMenu())
555555
<div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-4">
556556
<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}"
557557
class="d-block">
@@ -637,9 +637,9 @@ class="btn dropdown-toggle d-block w-100 d-md-inline"
637637
>
638638
@lang('Filters')
639639

640-
@if ($component->hasAppliedFiltersWithValues())
640+
@if ($count = $component->getFilterBadgeCount())
641641
<span class="badge bg-info">
642-
{{ $component->getAppliedFiltersWithValuesCount()}}
642+
{{ $count }}
643643
</span>
644644
@endif
645645

@@ -655,7 +655,7 @@ class="dropdown-menu w-100"
655655
role="menu"
656656
>
657657
@foreach($component->getFilters() as $filter)
658-
@if($filter->isVisible())
658+
@if($filter->isVisibleInMenu())
659659
<div wire:key="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}" class="p-2">
660660
<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}" class="mb-2">
661661
{{ $filter->getName() }}
@@ -666,7 +666,7 @@ class="dropdown-menu w-100"
666666
@endif
667667
@endforeach
668668

669-
@if ($component->hasAppliedFiltersWithValues())
669+
@if ($component->hasAppliedVisibleFiltersWithValuesThatCanBeCleared())
670670
<div class="dropdown-divider"></div>
671671

672672
<button
@@ -794,7 +794,7 @@ class="form-control"
794794
<div class="container">
795795
<div class="row">
796796
@foreach($component->getFilters() as $filter)
797-
@if($filter->isVisible())
797+
@if($filter->isVisibleInMenu())
798798
<div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-4">
799799
<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}"
800800
class="d-block">

src/Traits/Helpers/FilterHelpers.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function hasFilters(): bool
6161
// TODO: Test
6262
public function hasVisibleFilters(): bool
6363
{
64-
return $this->getFilters()->reject(fn(Filter $filter) => $filter->isHidden())->count();
64+
return $this->getFilters()
65+
->reject(fn(Filter $filter) => $filter->isHiddenFromMenus())
66+
->count();
6567
}
6668

6769
public function getFilters(): Collection
@@ -106,7 +108,9 @@ public function selectAllFilterOptions(string $filterKey): void
106108
public function setFilterDefaults(): void
107109
{
108110
foreach ($this->getFilters() as $filter) {
109-
$this->resetFilter($filter);
111+
if ($filter->isResetByClearButton()) {
112+
$this->resetFilter($filter);
113+
}
110114
}
111115
}
112116

@@ -120,6 +124,31 @@ public function hasAppliedFiltersWithValues(): bool
120124
return count($this->getAppliedFiltersWithValues());
121125
}
122126

127+
// TODO: Test
128+
public function hasAppliedVisibleFiltersWithValuesThatCanBeCleared(): bool
129+
{
130+
return collect($this->getAppliedFiltersWithValues())
131+
->map(fn($_item, $key) => $this->getFilterByKey($key))
132+
->reject(fn(Filter $filter) => $filter->isHiddenFromMenus() && !$filter->isResetByClearButton())
133+
->count();
134+
}
135+
136+
public function getFilterBadgeCount(): int
137+
{
138+
return collect($this->getAppliedFiltersWithValues())
139+
->map(fn($_item, $key) => $this->getFilterByKey($key))
140+
->reject(fn(Filter $filter) => $filter->isHiddenFromFilterCount())
141+
->count();
142+
}
143+
144+
public function hasAppliedVisibleFiltersForPills(): bool
145+
{
146+
return collect($this->getAppliedFiltersWithValues())
147+
->map(fn($_item, $key) => $this->getFilterByKey($key))
148+
->reject(fn(Filter $filter) => $filter->isHiddenFromPills())
149+
->count();
150+
}
151+
123152
public function getAppliedFiltersWithValues(): array
124153
{
125154
return array_filter($this->getAppliedFilters(), function ($item) {

src/Views/Filter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ abstract class Filter
1414

1515
protected string $name;
1616
protected string $key;
17-
protected bool $hidden = false;
17+
protected bool $hiddenFromMenus = false;
18+
protected bool $hiddenFromPills = false;
19+
protected bool $hiddenFromFilterCount = false;
20+
protected bool $resetByClearButton = true;
1821
protected $filterCallback = null;
1922
protected array $config = [];
2023
protected ?string $filterPillTitle = null;

src/Views/Traits/Configuration/FilterConfiguration.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function config(array $config = []): Filter
2222
/**
2323
* @param string $key
2424
*
25-
* @return mixed
25+
* @return $this
2626
*/
2727
public function setFilterPillTitle(string $title): self
2828
{
@@ -34,7 +34,7 @@ public function setFilterPillTitle(string $title): self
3434
/**
3535
* @param string $key
3636
*
37-
* @return mixed
37+
* @return $this
3838
*/
3939
public function setFilterPillValues(array $values): self
4040
{
@@ -43,12 +43,40 @@ public function setFilterPillValues(array $values): self
4343
return $this;
4444
}
4545

46-
/**
47-
* @return mixed
48-
*/
49-
public function hidden(): self
46+
// TODO: Test
47+
public function hiddenFromMenus(): self
48+
{
49+
$this->hiddenFromMenus = true;
50+
51+
return $this;
52+
}
53+
54+
public function hiddenFromPills(): self
55+
{
56+
$this->hiddenFromPills = true;
57+
58+
return $this;
59+
}
60+
61+
public function hiddenFromFilterCount(): self
62+
{
63+
$this->hiddenFromFilterCount = true;
64+
65+
return $this;
66+
}
67+
68+
public function hiddenFromAll(): self
69+
{
70+
$this->hiddenFromMenus = true;
71+
$this->hiddenFromPills = true;
72+
$this->hiddenFromFilterCount = true;
73+
74+
return $this;
75+
}
76+
77+
public function notResetByClearButton(): self
5078
{
51-
$this->hidden = true;
79+
$this->resetByClearButton = false;
5280

5381
return $this;
5482
}

src/Views/Traits/Helpers/ColumnHelpers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
99
use Rappasoft\LaravelLivewireTables\Views\Column;
1010
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
11-
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
11+
use Rappasoft\LaravelLivewireTables\Views\Filter;
1212

1313
trait ColumnHelpers
1414
{
@@ -460,7 +460,7 @@ public function getSecondaryHeaderContents($rows)
460460
if ($this->isHtml()) {
461461
return new HtmlString($value);
462462
}
463-
} elseif ($this->getSecondaryHeaderCallback() instanceof SelectFilter) {
463+
} elseif ($this->getSecondaryHeaderCallback() instanceof Filter) {
464464
return $this->getSecondaryHeaderCallback()->render($this->getComponent());
465465
} else {
466466
throw new DataTableConfigurationException('The secondary header callback must be a closure or a filter object.');
@@ -508,7 +508,7 @@ public function getFooterContents($rows)
508508
if ($this->isHtml()) {
509509
return new HtmlString($value);
510510
}
511-
} elseif ($this->getFooterCallback() instanceof SelectFilter) {
511+
} elseif ($this->getFooterCallback() instanceof Filter) {
512512
return $this->getFooterCallback()->render($this->getComponent());
513513
} else {
514514
throw new DataTableConfigurationException('The footer callback must be a closure or a filter object.');

src/Views/Traits/Helpers/FilterHelpers.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,39 @@ public function hasConfig(string $key): bool
171171
}
172172

173173
/**
174-
* Check if the hidden propery is set to true.
175-
*
176174
* @return bool
177175
*/
178-
public function isHidden(): bool
176+
public function isHiddenFromMenus(): bool
179177
{
180-
return $this->hidden === true;
178+
return $this->hiddenFromMenus === true;
181179
}
182180

183181
/**
184-
* Check if the hidden propery is set to false.
185-
*
186182
* @return bool
187183
*/
188-
public function isVisible(): bool
184+
public function isVisibleInMenu(): bool
185+
{
186+
return $this->hiddenFromMenus === false;
187+
}
188+
189+
// TODO: Test
190+
public function isHiddenFromPills(): bool
191+
{
192+
return $this->hiddenFromPills === true;
193+
}
194+
195+
public function isVisibleInPills(): bool
196+
{
197+
return $this->hiddenFromPills === false;
198+
}
199+
200+
public function isHiddenFromFilterCount(): bool
201+
{
202+
return $this->hiddenFromFilterCount === true;
203+
}
204+
205+
public function isResetByClearButton(): bool
189206
{
190-
return $this->hidden === false;
207+
return $this->resetByClearButton === true;
191208
}
192209
}

tests/Views/Traits/Helpers/FilterHelpersTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ public function can_check_if_filter_is_hidden(): void
142142
{
143143
$filter = SelectFilter::make('Active');
144144

145-
$this->assertFalse($filter->isHidden());
146-
$this->assertTrue($filter->isVisible());
145+
$this->assertFalse($filter->isHiddenFromMenus());
146+
$this->assertTrue($filter->isVisibleInMenus());
147147

148-
$filter->hidden();
148+
$filter->hiddenFromMenus();
149149

150-
$this->assertTrue($filter->isHidden());
151-
$this->assertFalse($filter->isVisible());
150+
$this->assertTrue($filter->isHiddenFromMenus());
151+
$this->assertFalse($filter->isVisibleInMenus());
152152
}
153153
}

0 commit comments

Comments
 (0)