Skip to content

Commit e077ea0

Browse files
authored
AddSearchQueryString
1 parent 92dc8fb commit e077ea0

File tree

4 files changed

+251
-11
lines changed

4 files changed

+251
-11
lines changed

docs/datatable/query-string.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,55 @@ public function configure(): void
102102
{
103103
$this->setQueryStringAliasForFilter('filtervalues');
104104
}
105+
```
106+
107+
## Search
108+
109+
The search query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods:
110+
111+
### setQueryStringStatusForSearch
112+
113+
Enable/disable the query string for search
114+
115+
```php
116+
public function configure(): void
117+
{
118+
$this->setQueryStringStatusForSearch(true);
119+
$this->setQueryStringStatusForSearch(false);
120+
}
121+
```
122+
123+
### setQueryStringForSearchEnabled
124+
125+
Enable the query string for search
126+
127+
```php
128+
public function configure(): void
129+
{
130+
// Shorthand for $this->setQueryStringStatusForSearch(true)
131+
$this->setQueryStringForSearchEnabled();
132+
}
133+
```
134+
135+
### setQueryStringForSearchDisabled
136+
137+
Disable the query string for search
138+
139+
```php
140+
public function configure(): void
141+
{
142+
// Shorthand for $this->setQueryStringStatusForSearch(false)
143+
$this->setQueryStringForSearchDisabled();
144+
}
145+
```
146+
147+
### setQueryStringAliasForSearch
148+
149+
Change the Alias in the URL for the search, otherwise defaults to "$tablename-search"
150+
151+
```php
152+
public function configure(): void
153+
{
154+
$this->setQueryStringAliasForSearch('search');
155+
}
105156
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;
4+
5+
use Livewire\Attributes\Locked;
6+
7+
trait HasQueryStringForSearch
8+
{
9+
#[Locked]
10+
public ?bool $queryStringStatusForSearch;
11+
12+
protected ?string $queryStringAliasForSearch;
13+
14+
protected function queryStringHasQueryStringForSearch(): array
15+
{
16+
if ($this->queryStringForSearchEnabled() && $this->searchIsEnabled()) {
17+
return [
18+
'search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSearch()],
19+
];
20+
}
21+
22+
return [];
23+
}
24+
25+
26+
public function setupQueryStringStatusForSearch(): void
27+
{
28+
if (! $this->hasQueryStringStatusForSearch()) {
29+
$this->setQueryStringForSearchEnabled();
30+
}
31+
}
32+
33+
public function hasQueryStringStatusForSearch(): bool
34+
{
35+
return isset($this->queryStringStatusForSearch);
36+
}
37+
38+
public function getQueryStringStatusForSearch(): bool
39+
{
40+
return $this->queryStringStatusForSearch ?? true;
41+
}
42+
43+
public function queryStringForSearchEnabled(): bool
44+
{
45+
$this->setupQueryStringStatusForSearch();
46+
47+
return $this->getQueryStringStatusForSearch() && $this->searchIsEnabled();
48+
}
49+
50+
public function setQueryStringStatusForSearch(bool $status): self
51+
{
52+
$this->queryStringStatusForSearch = $status;
53+
54+
return $this;
55+
}
56+
57+
public function setQueryStringForSearchEnabled(): self
58+
{
59+
$this->setQueryStringStatusForSearch(true);
60+
61+
return $this;
62+
}
63+
64+
public function setQueryStringForSearchDisabled(): self
65+
{
66+
$this->setQueryStringStatusForSearch(false);
67+
68+
return $this;
69+
}
70+
71+
public function hasQueryStringAliasForSearch(): bool
72+
{
73+
return isset($this->queryStringAliasForSearch);
74+
}
75+
76+
public function getQueryStringAliasForSearch(): string
77+
{
78+
return $this->queryStringAliasForSearch ?? $this->getQueryStringAlias().'-search';
79+
}
80+
81+
public function setQueryStringAliasForSearch(string $alias): self
82+
{
83+
$this->queryStringAliasForSearch = $alias;
84+
85+
return $this;
86+
}
87+
88+
}

src/Traits/WithSearch.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use Rappasoft\LaravelLivewireTables\Events\SearchApplied;
88
use Rappasoft\LaravelLivewireTables\Traits\Configuration\SearchConfiguration;
99
use Rappasoft\LaravelLivewireTables\Traits\Helpers\SearchHelpers;
10+
use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSearch;
1011

1112
trait WithSearch
1213
{
1314
use SearchConfiguration,
1415
SearchHelpers;
15-
16+
use HasQueryStringForSearch;
17+
1618
public string $search = '';
1719

1820
#[Locked]
@@ -38,16 +40,6 @@ trait WithSearch
3840

3941
protected bool $trimSearchString = false;
4042

41-
protected function queryStringWithSearch(): array
42-
{
43-
if ($this->queryStringIsEnabled() && $this->searchIsEnabled()) {
44-
return [
45-
'search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-search'],
46-
];
47-
}
48-
49-
return [];
50-
}
5143

5244
// TODO
5345
public function applySearch(): Builder
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Core\QueryStrings;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Depends;
7+
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
8+
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
9+
10+
final class QueryStringForSearchTest extends TestCase
11+
{
12+
public function test_can_get_default_search_query_string_status(): void
13+
{
14+
$mock = new class extends PetsTable
15+
{
16+
public ?array $testAttributesArray;
17+
18+
public function configure(): void
19+
{
20+
$this->setDataTableFingerprint('test');
21+
}
22+
};
23+
24+
$mock->configure();
25+
$mock->boot();
26+
27+
$this->assertSame(true, $mock->getQueryStringStatusForSearch());
28+
}
29+
30+
public function test_can_disable_search_query_string_status(): void
31+
{
32+
$mock = new class extends PetsTable
33+
{
34+
public ?array $testAttributesArray;
35+
36+
public function configure(): void
37+
{
38+
$this->setDataTableFingerprint('test');
39+
$this->setQueryStringForSearchDisabled();
40+
}
41+
};
42+
43+
$mock->configure();
44+
$mock->boot();
45+
46+
$this->assertSame(false, $mock->getQueryStringStatusForSearch());
47+
}
48+
49+
public function test_can_enable_search_query_string_status(): void
50+
{
51+
$mock = new class extends PetsTable
52+
{
53+
public ?array $testAttributesArray;
54+
55+
public function configure(): void
56+
{
57+
$this->setDataTableFingerprint('test');
58+
$this->setQueryStringForSearchDisabled();
59+
}
60+
};
61+
62+
$mock->configure();
63+
$mock->boot();
64+
65+
$this->assertSame(false, $mock->getQueryStringStatusForSearch());
66+
$mock->setQueryStringForSearchEnabled();
67+
$this->assertSame(true, $mock->getQueryStringStatusForSearch());
68+
69+
}
70+
71+
public function test_can_get_default_search_query_string_alias(): void
72+
{
73+
$mock = new class extends PetsTable
74+
{
75+
public ?array $testAttributesArray;
76+
77+
public function configure(): void
78+
{
79+
$this->setDataTableFingerprint('test');
80+
}
81+
};
82+
83+
$mock->configure();
84+
$mock->boot();
85+
86+
$this->assertSame('table-search', $mock->getQueryStringAliasForSearch());
87+
88+
}
89+
90+
public function test_can_change_default_search_query_string_alias(): void
91+
{
92+
$mock = new class extends PetsTable
93+
{
94+
public ?array $testAttributesArray;
95+
96+
public function configure(): void
97+
{
98+
$this->setDataTableFingerprint('test');
99+
}
100+
};
101+
102+
$mock->configure();
103+
$mock->boot();
104+
105+
$this->assertSame('table-search', $mock->getQueryStringAliasForSearch());
106+
$mock->setQueryStringAliasForSearch('pet-search');
107+
$this->assertSame('pet-search', $mock->getQueryStringAliasForSearch());
108+
}
109+
}

0 commit comments

Comments
 (0)