Skip to content

Commit

Permalink
Add setTrimSearchStringEnabled/setTrimSearchStringDisabled (#1843)
Browse files Browse the repository at this point in the history
* Add setTrimSearchStringEnabled/setTrimSearchStringDisabled

* Fix styling

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored Aug 14, 2024
1 parent c560ee4 commit 05d4c48
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
24 changes: 24 additions & 0 deletions docs/search/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@ public function configure(): void
$this->setSearchThrottle(1000);
}
```

## setTrimSearchStringEnabled

A new behaviour, which will trim search strings of whitespace at either end

```php
public function configure(): void
{
// Will trim whitespace from either end of search strings
$this->setTrimSearchStringEnabled();
}
```

## setTrimSearchStringDisabled

The default behaviour, does not trim search strings of whitespace.

```php
public function configure(): void
{
// Will not trim whitespace from either end of search strings
$this->setTrimSearchStringDisabled();
}
```
27 changes: 26 additions & 1 deletion src/Traits/Configuration/SearchConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ trait SearchConfiguration
{
public function setSearch(string $query): self
{
$this->search = $query;
if ($this->shouldTrimSearchString()) {
$this->search = trim($query);
} else {
$this->search = $query;
}

return $this;
}
Expand Down Expand Up @@ -156,4 +160,25 @@ public function setSearchFieldAttributes(array $attributes = []): self

return $this;
}

public function setTrimSearchString(bool $status): self
{
$this->trimSearchString = $status;

return $this;
}

public function setTrimSearchStringEnabled(): self
{
$this->setTrimSearchString(true);

return $this;
}

public function setTrimSearchStringDisabled(): self
{
$this->setTrimSearchString(false);

return $this;
}
}
5 changes: 5 additions & 0 deletions src/Traits/Helpers/SearchHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,9 @@ public function getSearchFieldAttributes(): array
{
return count($this->searchFieldAttributes) ? $this->searchFieldAttributes : ['default' => true];
}

public function shouldTrimSearchString(): bool
{
return $this->trimSearchString ?? false;
}
}
26 changes: 19 additions & 7 deletions src/Traits/WithSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ trait WithSearch

protected array $searchFieldAttributes = [];

protected bool $trimSearchString = false;

protected function queryStringWithSearch(): array
{
if ($this->queryStringIsEnabled() && $this->searchIsEnabled()) {
Expand All @@ -49,21 +51,27 @@ protected function queryStringWithSearch(): array
public function applySearch(): Builder
{
if ($this->searchIsEnabled() && $this->hasSearch()) {

$searchableColumns = $this->getSearchableColumns();
$search = $this->getSearch();

if ($this->shouldTrimSearchString()) {
$search = trim($search);
}

$this->callHook('searchUpdated', ['value' => $this->getSearch()]);
$this->callTraitHook('searchUpdated', ['value' => $this->getSearch()]);
if ($this->getEventStatusSearchApplied() && $this->getSearch() != null) {
event(new SearchApplied($this->getTableName(), $this->getSearch()));
$this->callHook('searchUpdated', ['value' => $search]);
$this->callTraitHook('searchUpdated', ['value' => $search]);
if ($this->getEventStatusSearchApplied() && $search != null) {
event(new SearchApplied($this->getTableName(), $search));
}

if ($searchableColumns->count()) {
$this->setBuilder($this->getBuilder()->where(function ($query) use ($searchableColumns) {
$this->setBuilder($this->getBuilder()->where(function ($query) use ($searchableColumns, $search) {
foreach ($searchableColumns as $index => $column) {
if ($column->hasSearchCallback()) {
($column->getSearchCallback())($query, $this->getSearch());
($column->getSearchCallback())($query, $search);
} else {
$query->{$index === 0 ? 'where' : 'orWhere'}($column->getColumn(), 'like', '%'.$this->getSearch().'%');
$query->{$index === 0 ? 'where' : 'orWhere'}($column->getColumn(), 'like', '%'.$search.'%');
}
}
}));
Expand All @@ -75,6 +83,10 @@ public function applySearch(): Builder

public function updatedSearch(string|array|null $value): void
{
if ($this->shouldTrimSearchString() && $this->search != trim($value)) {
$this->search = $value = trim($value);
}

$this->resetComputedPage();

// Clear bulk actions on search - if enabled
Expand Down
36 changes: 36 additions & 0 deletions tests/Traits/Helpers/SearchHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,40 @@ public function test_can_check_if_has_search_placeholder(): void

$this->assertTrue($this->basicTable->hasSearchPlaceholder());
}

public function test_can_trim_whitespace_from_search(): void
{
$this->basicTable->setSearch('Anthony ');

$this->assertSame('Anthony ', $this->basicTable->getSearch());

$this->basicTable->setTrimSearchStringEnabled();

$this->basicTable->clearSearch();

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

$this->assertSame('Anthony', $this->basicTable->getSearch());

$this->basicTable->clearSearch();

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

$this->assertSame('Anthony', $this->basicTable->getSearch());

$this->basicTable->clearSearch();

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

$this->assertSame('Anthony', $this->basicTable->getSearch());

$this->basicTable->clearSearch();

$this->basicTable->setTrimSearchStringDisabled();

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

$this->assertSame(' Anthony ', $this->basicTable->getSearch());

}
}

0 comments on commit 05d4c48

Please sign in to comment.