Skip to content

Commit 582cc3b

Browse files
feat: add DishesIterableTable component and update tests for iterable support (#2031)
1 parent 2719607 commit 582cc3b

File tree

7 files changed

+94
-257
lines changed

7 files changed

+94
-257
lines changed

src/DataSource/Processors/CollectionProcessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class CollectionProcessor extends DataSourceBase
1313
{
1414
public static function match(mixed $key): bool
1515
{
16-
return $key instanceof Collection;
16+
return $key instanceof Collection
17+
|| is_iterable($key);
1718
}
1819

1920
public function process(array $properties = []): array

tests/Concerns/Components/DishesCollectionTable.php

Lines changed: 0 additions & 168 deletions
This file was deleted.

tests/Concerns/Components/DishesArrayTable.php renamed to tests/Concerns/Components/DishesIterableTable.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
PowerGridComponent,
1111
PowerGridFields};
1212

13-
class DishesArrayTable extends PowerGridComponent
13+
class DishesIterableTable extends PowerGridComponent
1414
{
15-
public string $tableName = 'testing-dishes-array-table';
15+
public string $tableName = 'testing-dishes-iterable-table';
1616

1717
public array $eventId = [];
1818

1919
public array $testFilters = [];
2020

21+
public string $iterableType = 'array';
22+
2123
protected function getListeners()
2224
{
2325
return array_merge(
@@ -33,9 +35,9 @@ public function openModal(array $params)
3335
$this->eventId = $params;
3436
}
3537

36-
public function datasource(): array
38+
public function datasource()
3739
{
38-
return [
40+
$data = [
3941
[
4042
'id' => 1,
4143
'name' => 'Name 1',
@@ -77,6 +79,12 @@ public function datasource(): array
7779
'chef_name' => 'Luan',
7880
],
7981
];
82+
83+
if ($this->iterableType === 'collection') {
84+
return collect($data);
85+
}
86+
87+
return $data;
8088
}
8189

8290
public function setUp(): array

tests/Feature/CollectionSearchTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3-
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\{DishesCollectionTable};
3+
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishesIterableTable;
44
use PowerComponents\LivewirePowerGrid\Themes\{Bootstrap5, Tailwind};
55

66
use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;
77

88
it('searches', function (string $component, object $params) {
9-
livewire($component)
9+
livewire($component, [
10+
'iterableType' => 'collection',
11+
])
1012
->call('setTestThemeClass', $params->theme)
1113
->set('search', 'Name 1')
1214
->assertSee('Name 1')
@@ -29,6 +31,6 @@
2931
})->with('search');
3032

3133
dataset('search', [
32-
'tailwind' => [DishesCollectionTable::class, (object) ['theme' => Tailwind::class]],
33-
'bootstrap' => [DishesCollectionTable::class, (object) ['theme' => Bootstrap5::class]],
34+
'tailwind' => [DishesIterableTable::class, (object) ['theme' => Tailwind::class]],
35+
'bootstrap' => [DishesIterableTable::class, (object) ['theme' => Bootstrap5::class]],
3436
]);

tests/Feature/Filters/FilterBooleanTest.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use PowerComponents\LivewirePowerGrid\Facades\Filter;
4-
use PowerComponents\LivewirePowerGrid\Tests\{Concerns\Components\DishesArrayTable,
5-
Concerns\Components\DishesCollectionTable,
4+
use PowerComponents\LivewirePowerGrid\Tests\{
5+
Concerns\Components\DishesIterableTable,
66
Concerns\Components\DishesQueryBuilderTable,
77
Concerns\Components\DishesTable,
88
Concerns\Components\DishesTableWithJoin};
@@ -131,7 +131,7 @@ public function filters(): array
131131
expect($component->filters)
132132
->toMatchArray([]);
133133
})->group('filters', 'filterBoolean')
134-
->with('filter_boolean_themes_collection', 'filter_boolean_themes_array');
134+
->with('iterable_datasource');
135135

136136
it('properly filters by bool true - using collection', function (string $component, string $theme) {
137137
$component = livewire($component, [
@@ -169,7 +169,7 @@ public function filters(): array
169169
expect($component->filters)
170170
->toMatchArray([]);
171171
})->group('filters', 'filterBoolean')
172-
->with('filter_boolean_themes_collection');
172+
->with('iterable_datasource');
173173

174174
it('properly filters by bool true - using collection - custom builder', function (string $componentName, string $theme) {
175175
$component = livewire($componentName, [
@@ -192,9 +192,9 @@ public function filters(): array
192192
->assertSee('Name 4')
193193
->assertDontSee('Name 5');
194194
})->group('filters', 'filterBoolean')
195-
->with('filter_boolean_themes_collection');
195+
->with('iterable_datasource');
196196

197-
$customCollection = new class() extends DishesCollectionTable
197+
$customCollection = new class() extends DishesIterableTable
198198
{
199199
public int $dishId;
200200

@@ -214,7 +214,9 @@ public function filters(): array
214214
};
215215

216216
it('properly filters by bool true - using collection - custom builder - using tablename in field', function (string $component, string $theme) {
217-
$component = livewire($component)
217+
$component = livewire($component, [
218+
'iterableType' => 'collection',
219+
])
218220
->call('setTestThemeClass', $theme)
219221
->assertSeeHtml('wire:input.live.debounce.600ms="filterBoolean(\'in_stock\', $event.target.value, \'In Stock\')"');
220222

@@ -320,7 +322,7 @@ public function filters(): array
320322
expect($component->filters)
321323
->toMatchArray([]);
322324
})->group('filters', 'filterBoolean')
323-
->with('filter_boolean_themes_collection', 'filter_boolean_themes_array');
325+
->with('iterable_datasource');
324326

325327
it('properly filters by bool "all"', function (string $component, object $params) {
326328
$component = livewire($component, [
@@ -352,8 +354,10 @@ public function filters(): array
352354
})->group('filters', 'filterBoolean')
353355
->with('filter_boolean_join', 'filter_boolean_query_builder');
354356

355-
it('properly filters by bool "all" - using collection & array table', function (string $component, string $theme) {
356-
$component = livewire($component)
357+
it('properly filters by bool "all" - using collection', function (string $component, string $theme, string $iterableType) {
358+
$component = livewire($component, [
359+
'iterableType' => $iterableType,
360+
])
357361
->call('setTestThemeClass', $theme);
358362

359363
expect($component->filters)
@@ -373,7 +377,7 @@ public function filters(): array
373377
],
374378
]);
375379
})->group('filters', 'filterBoolean')
376-
->with('filter_boolean_themes_collection', 'filter_boolean_themes_array');
380+
->with('iterable_datasource');
377381

378382
dataset('filter_boolean_join', [
379383
'tailwind -> id' => [DishesTable::class, (object) ['theme' => \PowerComponents\LivewirePowerGrid\Themes\Tailwind::class, 'field' => 'id']],
@@ -390,14 +394,11 @@ public function filters(): array
390394
'daisyui query builder -> id' => [DishesQueryBuilderTable::class, (object) ['theme' => \PowerComponents\LivewirePowerGrid\Themes\DaisyUI::class, 'field' => 'id']],
391395
]);
392396

393-
dataset('filter_boolean_themes_array', [
394-
[DishesArrayTable::class, \PowerComponents\LivewirePowerGrid\Themes\Tailwind::class],
395-
[DishesArrayTable::class, \PowerComponents\LivewirePowerGrid\Themes\Bootstrap5::class],
396-
[DishesArrayTable::class, \PowerComponents\LivewirePowerGrid\Themes\DaisyUI::class],
397-
]);
398-
399-
dataset('filter_boolean_themes_collection', [
400-
'tailwind' => [DishesCollectionTable::class, \PowerComponents\LivewirePowerGrid\Themes\Tailwind::class],
401-
'bootstrap' => [DishesCollectionTable::class, \PowerComponents\LivewirePowerGrid\Themes\Bootstrap5::class],
402-
'daisyui' => [DishesCollectionTable::class, \PowerComponents\LivewirePowerGrid\Themes\DaisyUI::class],
397+
dataset('iterable_datasource', [
398+
'tailwind datasource array' => [DishesIterableTable::class, \PowerComponents\LivewirePowerGrid\Themes\Tailwind::class, 'array'],
399+
'bootstrap datasource array' => [DishesIterableTable::class, \PowerComponents\LivewirePowerGrid\Themes\Bootstrap5::class, 'array'],
400+
'daisyui datasource array' => [DishesIterableTable::class, \PowerComponents\LivewirePowerGrid\Themes\DaisyUI::class, 'array'],
401+
'tailwind datasource collection' => [DishesIterableTable::class, \PowerComponents\LivewirePowerGrid\Themes\Tailwind::class, 'collection'],
402+
'bootstrap datasource collection' => [DishesIterableTable::class, \PowerComponents\LivewirePowerGrid\Themes\Bootstrap5::class, 'collection'],
403+
'daisyui datasource collection' => [DishesIterableTable::class, \PowerComponents\LivewirePowerGrid\Themes\DaisyUI::class, 'collection'],
403404
]);

0 commit comments

Comments
 (0)