Skip to content

Commit

Permalink
Enhancement/disable rule select all value (#1137)
Browse files Browse the repository at this point in the history
* fix bootstrap spinner (#1134)

* If the checkbox rule is disabled, the value should not be selected while clicking on select all.

* add test

---------

Co-authored-by: Luan Freitas <33601626+luanfreitasdev@users.noreply.github.com>
Co-authored-by: luanfreitasdev <luanfreitas10@protonmail.com>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent 5eb0fc4 commit 7825116
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Traits/WithCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function selectCheckboxAll(): void
collect($data->items())->each(function (array|Model|\stdClass $model) use ($actionRulesClass) {
$rules = $actionRulesClass->recoverFromAction($model);

if (isset($rules['hide'])) {
if (isset($rules['hide']) || isset($rules['disable'])) {
return;
}
$value = $model->{$this->checkboxAttribute};
Expand Down
72 changes: 64 additions & 8 deletions tests/Feature/CheckboxTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
<?php

use PowerComponents\LivewirePowerGrid\Facades\Rule;
use PowerComponents\LivewirePowerGrid\Tests\DishTableBase;

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

;
$componentWithActionRules = new class () extends DishTableBase {
public function actionRules($row): array
{
$idsWithCheckboxDisabled = [1, 2, 3];

use PowerComponents\LivewirePowerGrid\Tests\{DishesTable, DishesTableWithJoin};
return [
Rule::checkbox()
->when(fn ($dish) => in_array($dish->id, $idsWithCheckboxDisabled))
->disable(),
];
}
};

it('selectCheckboxAll works properly', function (string $component, object $params) {
$component = livewire($component)
Expand Down Expand Up @@ -55,11 +67,55 @@

expect($component->checkboxValues)
->toBe([]);
})->with('checkbox_join');
})->with([
'tailwind -> id' => [DishTableBase::class, (object) ['theme' => 'tailwind', 'field' => 'id']],
'bootstrap -> id' => [DishTableBase::class, (object) ['theme' => 'bootstrap', 'field' => 'id']],
]);

it('selectCheckboxAll works properly with actionRules disable', function (string $component, object $params) {
$component = livewire($component)
->call($params->theme)
->set('checkboxAll', true)
->call('selectCheckboxAll');

dataset('checkbox_join', [
'tailwind -> id' => [DishesTable::class, (object) ['theme' => 'tailwind', 'field' => 'id']],
'bootstrap -> id' => [DishesTable::class, (object) ['theme' => 'bootstrap', 'field' => 'id']],
'tailwind -> dishes.id' => [DishesTableWithJoin::class, (object) ['theme' => 'tailwind', 'field' => 'dishes.id']],
'bootstrap -> dishes.id' => [DishesTableWithJoin::class, (object) ['theme' => 'bootstrap', 'field' => 'dishes.id']],
expect($component->checkboxValues)
->toMatchArray([
0 => "4",
1 => "5",
2 => "6",
3 => "7",
4 => "8",
5 => "9",
6 => "10",
]);

$component->call('setPage', 2)
->set('checkboxAll', true)
->call('selectCheckboxAll');

expect($component->checkboxValues)
->toMatchArray([
0 => "4",
1 => "5",
2 => "6",
3 => "7",
4 => "8",
5 => "9",
6 => "10",
7 => "11",
8 => "12",
9 => "13",
10 => "14",
11 => "15",
]);

$component->call('setPage', 1)
->set('checkboxAll', false)
->call('selectCheckboxAll');

expect($component->checkboxValues)
->toBe([]);
})->with([
'tailwind -> id' => [$componentWithActionRules::class, (object) ['theme' => 'tailwind', 'field' => 'id']],
'bootstrap -> id' => [$componentWithActionRules::class, (object) ['theme' => 'bootstrap', 'field' => 'id']],
]);

0 comments on commit 7825116

Please sign in to comment.