Skip to content

Commit

Permalink
Add Option to Retain Selected when Searching/Filtering (#1762)
Browse files Browse the repository at this point in the history
* Initial Commit for Retaining Selected

* Update Test for Search/Filter

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Jul 10, 2024
1 parent 37691f9 commit 799ccfd
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 6 deletions.
72 changes: 72 additions & 0 deletions docs/bulk-actions/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,75 @@ public function configure(): void
$this->setShouldAlwaysHideBulkActionsDropdownOptionDisabled();
}
```


## setClearSelectedOnSearch

By default, any selected items for Bulk Actions are cleared upon searching. You may configure this behaviour here.

```php
public function configure(): void
{
$this->setClearSelectedOnSearch(true);
}
```


## setClearSelectedOnSearchEnabled

By default, any selected items for Bulk Actions are cleared upon searching. This enables this behaviour.

```php
public function configure(): void
{
$this->setClearSelectedOnSearchEnabled();
}
```


## setClearSelectedOnSearchDisabled

By default, any selected items for Bulk Actions are cleared upon searching. This disables this behaviour, ensuring that selected items are retained after searching.

```php
public function configure(): void
{
$this->setClearSelectedOnSearchDisabled();
}
```


## setClearSelectedOnFilter

By default, any selected items for Bulk Actions are cleared upon filtering. You may configure this behaviour here.

```php
public function configure(): void
{
$this->setClearSelectedOnFilter(true);
}
```


## setClearSelectedOnFilterEnabled

By default, any selected items for Bulk Actions are cleared upon filtering. This enables this behaviour.

```php
public function configure(): void
{
$this->setClearSelectedOnFilterEnabled();
}
```


## setClearSelectedOnFilterDisabled

By default, any selected items for Bulk Actions are cleared upon filtering. This disables this behaviour, ensuring that selected items are retained after filtering.

```php
public function configure(): void
{
$this->setClearSelectedOnFilterDisabled();
}
```
42 changes: 42 additions & 0 deletions src/Traits/Configuration/BulkActionsConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,46 @@ public function setShouldAlwaysHideBulkActionsDropdownOptionDisabled(): self

return $this;
}

public function setClearSelectedOnSearch(bool $status): self
{
$this->clearSelectedOnSearch = $status;

return $this;
}

public function setClearSelectedOnSearchEnabled(): self
{
$this->setClearSelectedOnSearch(true);

return $this;
}

public function setClearSelectedOnSearchDisabled(): self
{
$this->setClearSelectedOnSearch(false);

return $this;
}

public function setClearSelectedOnFilter(bool $status): self
{
$this->clearSelectedOnFilter = $status;

return $this;
}

public function setClearSelectedOnFilterEnabled(): self
{
$this->setClearSelectedOnFilter(true);

return $this;
}

public function setClearSelectedOnFilterDisabled(): self
{
$this->setClearSelectedOnFilter(false);

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Traits/Helpers/BulkActionsHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,14 @@ public function shouldAlwaysHideBulkActionsDropdownOption(): bool
{
return $this->alwaysHideBulkActionsDropdownOption ?? false;
}

public function getClearSelectedOnSearch(): bool
{
return $this->clearSelectedOnSearch ?? true;
}

public function getClearSelectedOnFilter(): bool
{
return $this->clearSelectedOnFilter ?? true;
}
}
4 changes: 4 additions & 0 deletions src/Traits/WithBulkActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ trait WithBulkActions

protected bool $alwaysHideBulkActionsDropdownOption = false;

public bool $clearSelectedOnSearch = true;

public bool $clearSelectedOnFilter = true;

public function bulkActions(): array
{
return property_exists($this, 'bulkActions') ? $this->bulkActions : [];
Expand Down
8 changes: 5 additions & 3 deletions src/Traits/WithFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public function updatedFilterComponents(string|array|null $value, string $filter
{
$this->resetComputedPage();

// Clear bulk actions on filter
$this->clearSelected();
$this->setSelectAllDisabled();
// Clear bulk actions on filter - if enabled
if ($this->getClearSelectedOnFilter()) {
$this->clearSelected();
$this->setSelectAllDisabled();
}

// Clear filters on empty value
$filter = $this->getFilterByKey($filterName);
Expand Down
8 changes: 5 additions & 3 deletions src/Traits/WithSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public function updatedSearch(string|array|null $value): void
{
$this->resetComputedPage();

// Clear bulk actions on search
$this->clearSelected();
$this->setSelectAllDisabled();
// Clear bulk actions on search - if enabled
if ($this->getClearSelectedOnSearch()) {
$this->clearSelected();
$this->setSelectAllDisabled();
}

if (is_null($value) || $value === '') {
$this->clearSearch();
Expand Down
70 changes: 70 additions & 0 deletions tests/Traits/Helpers/BulkActionsHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,74 @@ public function test_bulk_actions_th_checkbox_attributes_returns_default_true_if
{
$this->assertSame(['default' => true], $this->basicTable->getBulkActionsThCheckboxAttributes());
}

public function test_select_clears_by_default(): void
{
$this->basicTable->setSelected([1, 2, 3, 4, 5]);
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());

$this->basicTable->setSearch('Anthony');
$this->basicTable->updatedSearch('Anthony');

$this->assertSame([], $this->basicTable->getSelected());
}

public function test_select_does_not_clear_when_disabled(): void
{
$this->basicTable->setClearSelectedOnSearchDisabled();

$this->basicTable->setSelected([1, 2, 3, 4, 5]);
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());

$this->basicTable->setSearch('Anthony');
$this->basicTable->updatedSearch('Anthony');

$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());
}

public function test_select_does_clear_when_enabled(): void
{
$this->basicTable->setClearSelectedOnSearchEnabled();

$this->basicTable->setSelected([1, 2, 3, 4, 5]);
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());

$this->basicTable->setSearch('Anthony');
$this->basicTable->updatedSearch('Anthony');

$this->assertSame([], $this->basicTable->getSelected());

}

public function test_select_clears_by_default_when_filtering(): void
{
$this->basicTable->setSelected([1, 2, 3, 4, 5]);

$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());

$this->basicTable->setFilter('breed_id_filter', '2');
$this->basicTable->updatedFilterComponents('2', 'breed_id_filter');

$this->assertSame([], $this->basicTable->getSelected());
}

public function test_select_does_clear_when_filtering_when_enabled(): void
{
$this->basicTable->setSelected([1, 2, 3, 4, 5]);
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());
$this->basicTable->setClearSelectedOnFilterEnabled();
$this->basicTable->setFilter('breed_id_filter', '2');
$this->basicTable->updatedFilterComponents('2', 'breed_id_filter');
$this->assertSame([], $this->basicTable->getSelected());
}

public function test_select_does_not_clear_when_filtering_when_disabled(): void
{
$this->basicTable->setSelected([1, 2, 3, 4, 5]);
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());
$this->basicTable->setClearSelectedOnFilterDisabled();
$this->basicTable->setFilter('breed_id_filter', '2');
$this->basicTable->updatedFilterComponents('2', 'breed_id_filter');
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getSelected());
}
}

0 comments on commit 799ccfd

Please sign in to comment.