Skip to content

Commit 99f29d7

Browse files
committed
Fix 0 passed to multiple select filter
1 parent e93c52c commit 99f29d7

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/Filters/FilterContainer/FilterContainer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public function getDefaultFilterValues(): Collection
102102
])
103103
->mapWithKeys(function (Filter $handler) {
104104
return [
105-
// we pass through fromQueryParam() & toQueryParam() to ensure the value is formatted correctly
106105
$handler->getKey() => $handler->toQueryParam($handler->defaultValue()),
107106
];
108107
});

src/Filters/SelectMultipleFilter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
namespace Code16\Sharp\Filters;
44

5-
use Illuminate\Support\Arr;
6-
75
abstract class SelectMultipleFilter extends SelectFilter
86
{
97
public function fromQueryParam($value): array
108
{
11-
return $value ? explode(',', $value) : [];
9+
return $value !== null
10+
? explode(',', $value)
11+
: [];
1212
}
1313

1414
public function toQueryParam($value): ?string
1515
{
16-
return $value ? implode(',', Arr::wrap($value)) : null;
16+
return $value && count($value)
17+
? collect($value)->implode(',')
18+
: null;
1719
}
1820
}

tests/Http/FiltersInRequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public function buildFilterConfig(): void
132132
public function values(): array
133133
{
134134
return [
135+
'0' => 'Void',
135136
'physicist' => 'Physicist',
136137
'physician' => 'Physician',
137138
];
@@ -145,6 +146,7 @@ public function getListData(): array|Arrayable
145146
return collect([
146147
['id' => 1, 'name' => 'Marie Curie', 'job' => 'physicist'],
147148
['id' => 2, 'name' => 'Louis Pasteur', 'job' => 'physician'],
149+
['id' => 3, 'name' => 'John Void', 'job' => '0'],
148150
])
149151
->filter(fn ($item) => in_array($item['job'], $this->queryParams->filterFor('job')))
150152
->values();
@@ -167,6 +169,18 @@ public function getListData(): array|Arrayable
167169
)
168170
->count('entityList.data', 2)
169171
);
172+
173+
$this
174+
->get('/sharp/s-list/person?filter_job=0')
175+
->assertOk()
176+
->assertInertia(fn (Assert $page) => $page
177+
->has('entityList.data.0', fn (Assert $json) => $json
178+
->where('id', 3)
179+
->where('name', 'John Void')
180+
->etc()
181+
)
182+
->count('entityList.data', 1)
183+
);
170184
});
171185

172186
it('saves retained filters in the session when set', function () {

0 commit comments

Comments
 (0)