Skip to content

Commit b24c661

Browse files
committed
Merge branch 'macbookandrew-feature/select-optgroups' into develop
2 parents b254694 + 6b39a5f commit b24c661

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

docs/examples/advanced-example.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ class UsersTable extends DataTableComponent
171171
$builder->where('active', false);
172172
}
173173
}),
174+
SelectFilter::make('Address Group')
175+
->options([
176+
'' => 'All',
177+
AddressGroup::query()
178+
->orderBy('type')
179+
->get()
180+
->groupBy('type')
181+
->map(fn ($addressGroup) => $addressGroup->pluck('type', 'id')->filter())
182+
->toArray(),
183+
])
184+
->filter(function(Builder $builder, string $value) {
185+
$builder->where('address_groups.type', $value);
186+
}),
174187
DateFilter::make('Verified From')
175188
->config([
176189
'min' => '2020-01-01',

docs/filters/creating-filters.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ public function filters(): array
3838

3939
You should supply the first option as the default value. I.e. nothing selected, so the filter is not applied. This value should be an empty string. When this value is selected, the filter will be removed from the query and the query string.
4040

41+
### Option Groups
42+
43+
To use `<optgroup>` elements, pass a nested array of options to the select filter.
44+
45+
```php
46+
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
47+
48+
public function filters(): array
49+
{
50+
return [
51+
SelectFilter::make('Active')
52+
->options([
53+
'' => 'All',
54+
'Open' => [
55+
1 => 'Type A',
56+
2 => 'Type B',
57+
3 => 'Type C',
58+
],
59+
'Closed' => [
60+
24 => 'Type X',
61+
25 => 'Type Y',
62+
26 => 'Type Z',
63+
],
64+
]),
65+
];
66+
}
67+
```
68+
4169
## Multi-select Filters
4270

4371
Multi-select filters are a list of checkboxes. The user can select multiple options from the list. There is also an 'All' option that will select all values.

resources/views/components/tools/filters/select.blade.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150 ease-in-out focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-800 dark:text-white dark:border-gray-600"
1212
>
1313
@foreach($filter->getOptions() as $key => $value)
14-
<option value="{{ $key }}">{{ $value }}</option>
14+
@if (is_iterable($value))
15+
<optgroup label="{{ $key }}">
16+
@foreach ($value as $optionKey => $optionValue)
17+
<option value="{{ $optionKey }}">{{ $optionValue }}</option>
18+
@endforeach
19+
</optgroup>
20+
@else
21+
<option value="{{ $key }}">{{ $value }}</option>
22+
@endif
1523
@endforeach
1624
</select>
1725
</div>
@@ -23,7 +31,15 @@ class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150
2331
class="form-control"
2432
>
2533
@foreach($filter->getOptions() as $key => $value)
26-
<option value="{{ $key }}">{{ $value }}</option>
34+
@if (is_iterable($value))
35+
<optgroup label="{{ $key }}">
36+
@foreach ($value as $optionKey => $optionValue)
37+
<option value="{{ $optionKey }}">{{ $optionValue }}</option>
38+
@endforeach
39+
</optgroup>
40+
@else
41+
<option value="{{ $key }}">{{ $value }}</option>
42+
@endif
2743
@endforeach
2844
</select>
2945
@endif

src/Views/Filters/SelectFilter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ public function getOptions(): array
2424
public function getKeys(): array
2525
{
2626
return collect($this->getOptions())
27-
->keys()
27+
->map(fn ($value, $key) => is_iterable($value) ? collect($value)->keys() : $key)
28+
->flatten()
2829
->map(fn ($value) => (string)$value)
29-
->filter(fn ($value) => strlen($value))
30+
->filter(fn ($value) => strlen($value) > 0)
3031
->values()
3132
->toArray();
3233
}
@@ -42,7 +43,10 @@ public function validate($value)
4243

4344
public function getFilterPillValue($value): ?string
4445
{
45-
return $this->getCustomFilterPillValue($value) ?? $this->getOptions()[$value] ?? null;
46+
return $this->getCustomFilterPillValue($value)
47+
?? collect($this->getOptions())
48+
->mapWithKeys(fn ($options, $optgroupLabel) => is_iterable($options) ? $options : [$optgroupLabel => $options])[$value]
49+
?? null;
4650
}
4751

4852
public function isEmpty($value): bool

tests/Views/Traits/Helpers/FilterHelpersTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ public function can_get_filter_keys(): void
5858
$this->assertSame(['foo'], $filter->getKeys());
5959
}
6060

61+
/** @test */
62+
public function can_get_nested_filter_keys(): void
63+
{
64+
$filter = SelectFilter::make('Active');
65+
66+
$this->assertSame([], $filter->getKeys());
67+
68+
$filter->options(['foo' => ['bar' => 'baz']]);
69+
70+
$this->assertSame(['bar'], $filter->getKeys());
71+
72+
$filter->options(['foo' => collect(['bar' => 'baz'])]);
73+
74+
$this->assertSame(['bar'], $filter->getKeys());
75+
}
76+
6177
/** @test */
6278
public function can_get_filter_default_value(): void
6379
{
@@ -114,6 +130,21 @@ public function can_get_filter_pill_value(): void
114130
$this->assertSame('baz', $filter->getFilterPillValue('foo'));
115131
}
116132

133+
/** @test */
134+
public function can_get_nested_filter_pill_value(): void
135+
{
136+
$filter = SelectFilter::make('Active')
137+
->options(['foo' => ['bar' => 'baz']]);
138+
139+
$this->assertSame('baz', $filter->getFilterPillValue('bar'));
140+
141+
$filter = SelectFilter::make('Active')
142+
->options(['foo' => ['bar' => 'baz']])
143+
->setFilterPillValues(['bar' => 'etc']);
144+
145+
$this->assertSame('etc', $filter->getFilterPillValue('bar'));
146+
}
147+
117148
/** @test */
118149
public function can_check_if_filter_has_configs(): void
119150
{

0 commit comments

Comments
 (0)