Skip to content

Commit

Permalink
Only enable the selection of records if actions are available (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonrietdijk authored Aug 29, 2024
1 parent 29a70a0 commit 5c25324
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
8 changes: 2 additions & 6 deletions docs/configuration/selection.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Selection

The selection of records in the table are enabled by default. This behaviour can be disabled by setting the property to false.
The selection of records in the table is enabled when non-standalone [actions](/usage/actions) have been added. However, it will be disabled during [reordering](/usage/reordering).

::: info
The selection could be disabled automatically when no regular actions are available. This is not implemented to preserve performance as much as possible.
:::
To permanently disable the selection, you can set the property `$useSelection` to false. Note that this property was introduced before the action logic stated above. This property will therefore be removed in the next major release.

```php
protected bool $useSelection = false;
Expand All @@ -20,5 +18,3 @@ protected function canSelect(): bool
return false;
}
```

Out of the box, the selection is always enabled, depending on the value of the `$useSelection` property. The selection will get disabled during [reordering](/usage/reordering). Make sure to pay attention to this when overriding the method.
10 changes: 9 additions & 1 deletion src/Concerns/HasSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RamonRietdijk\LivewireTables\Concerns;

use Illuminate\Database\Eloquent\Model;
use RamonRietdijk\LivewireTables\Actions\BaseAction;

trait HasSelection
{
Expand All @@ -11,6 +12,7 @@ trait HasSelection

public bool $selectedPage = false;

/** @deprecated */
protected bool $useSelection = true;

public function updatingSelectedPage(bool $selectedPage): void
Expand Down Expand Up @@ -100,6 +102,12 @@ public function selectTable(bool $select): void

protected function canSelect(): bool
{
return $this->useSelection && ! $this->isReordering();
$hasActions = $this->resolveActions()
->filter(fn (BaseAction $action): bool => ! $action->isStandalone())
->isNotEmpty();

return $this->useSelection
&& ! $this->isReordering()
&& $hasActions;
}
}
15 changes: 15 additions & 0 deletions tests/Fakes/Livewire/ReorderingBlogLivewireTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace RamonRietdijk\LivewireTables\Tests\Fakes\Livewire;

use Illuminate\Support\Enumerable;
use RamonRietdijk\LivewireTables\Actions\Action;
use RamonRietdijk\LivewireTables\Columns\Column;
use RamonRietdijk\LivewireTables\Livewire\LivewireTable;
use RamonRietdijk\LivewireTables\Tests\Fakes\Models\Blog;
Expand All @@ -20,4 +22,17 @@ protected function columns(): array
->searchable(),
];
}

protected function actions(): array
{
return [
Action::make(__('Publish'), 'publish', function (Enumerable $models): void {
/** @var Blog $model */
foreach ($models as $model) {
$model->published = true;
$model->save();
}
}),
];
}
}
33 changes: 33 additions & 0 deletions tests/Integration/Concerns/HasSelectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\Test;
use RamonRietdijk\LivewireTables\Tests\Fakes\Livewire\BlogLivewireTable;
use RamonRietdijk\LivewireTables\Tests\Fakes\Livewire\DisabledSelectionBlogLivewireTable;
use RamonRietdijk\LivewireTables\Tests\Fakes\Livewire\EmptyBlogLivewireTable;
use RamonRietdijk\LivewireTables\Tests\Fakes\Models\Blog;
use RamonRietdijk\LivewireTables\Tests\TestCase;

Expand Down Expand Up @@ -47,6 +48,18 @@ public function it_cant_select_a_single_item_when_selection_is_disabled(): void
->assertCount('selected', 0);
}

#[Test]
public function it_cant_select_a_single_item_when_no_actions_are_available(): void
{
Blog::factory()->count(3)->create();

Livewire::test(EmptyBlogLivewireTable::class)
->call('selectItem', '1')
->call('selectItem', '2')
->call('selectItem', '3')
->assertCount('selected', 0);
}

#[Test]
public function it_can_select_the_page(): void
{
Expand All @@ -70,6 +83,16 @@ public function it_cant_select_the_page_when_selection_is_disabled(): void
->assertCount('selected', 0);
}

#[Test]
public function it_cant_select_the_page_when_no_actions_are_available(): void
{
Blog::factory()->count(30)->create();

Livewire::test(EmptyBlogLivewireTable::class)
->call('selectPage', true)
->assertCount('selected', 0);
}

#[Test]
public function it_can_select_the_table(): void
{
Expand All @@ -92,4 +115,14 @@ public function it_cant_select_the_table_when_selection_is_disabled(): void
->call('selectTable', true)
->assertCount('selected', 0);
}

#[Test]
public function it_cant_select_the_table_when_no_actions_are_available(): void
{
Blog::factory()->count(30)->create();

Livewire::test(EmptyBlogLivewireTable::class)
->call('selectTable', true)
->assertCount('selected', 0);
}
}

0 comments on commit 5c25324

Please sign in to comment.