Skip to content

Commit 379c778

Browse files
committed
Migrating Search Methods into Separate Traits
1 parent a57de60 commit 379c778

File tree

10 files changed

+244
-234
lines changed

10 files changed

+244
-234
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search;
4+
5+
use Rappasoft\LaravelLivewireTables\Traits\Core\Search\Styling\{HasSearchIcon, HasSearchInput};
6+
7+
trait HandlesSearchFieldStyling
8+
{
9+
use HasSearchIcon,
10+
HasSearchInput;
11+
12+
}

src/Traits/Configuration/SearchConfiguration.php renamed to src/Traits/Core/Search/HandlesSearchModifiers.php

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,91 @@
11
<?php
22

3-
namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search;
44

55
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
66

7-
trait SearchConfiguration
7+
trait HandlesSearchModifiers
88
{
9-
public function setSearch(string $query): self
10-
{
11-
if ($this->shouldTrimSearchString()) {
12-
$this->search = trim($query);
13-
} else {
14-
$this->search = $query;
15-
}
9+
protected ?bool $searchFilterBlur = null;
1610

17-
return $this;
18-
}
11+
protected ?int $searchFilterDebounce = null;
1912

20-
public function setSearchStatus(bool $status): self
21-
{
22-
$this->searchStatus = $status;
13+
protected ?bool $searchFilterDefer = null;
2314

24-
return $this;
25-
}
15+
protected ?bool $searchFilterLazy = null;
2616

27-
public function setSearchEnabled(): self
17+
protected ?bool $searchFilterLive = null;
18+
19+
protected ?int $searchFilterThrottle = null;
20+
21+
public function hasSearchDebounce(): bool
2822
{
29-
$this->setSearchStatus(true);
23+
return $this->searchFilterDebounce !== null;
24+
}
3025

31-
return $this;
26+
public function getSearchDebounce(): ?int
27+
{
28+
return $this->searchFilterDebounce;
3229
}
3330

34-
/**
35-
* @return $this
36-
*/
37-
public function setSearchDisabled(): self
31+
public function hasSearchDefer(): bool
3832
{
39-
$this->setSearchStatus(false);
40-
$this->search = '';
33+
return $this->searchFilterDefer !== null;
34+
}
4135

42-
return $this;
36+
public function hasSearchLazy(): bool
37+
{
38+
return $this->searchFilterLazy !== null;
4339
}
4440

45-
public function setSearchVisibilityStatus(bool $status): self
41+
public function hasSearchLive(): bool
4642
{
47-
$this->searchVisibilityStatus = $status;
43+
return $this->searchFilterLive !== null;
44+
}
4845

49-
return $this;
46+
public function hasSearchThrottle(): bool
47+
{
48+
return $this->searchFilterThrottle !== null;
5049
}
5150

52-
public function setSearchVisibilityEnabled(): self
51+
public function getSearchThrottle(): ?int
5352
{
54-
$this->setSearchVisibilityStatus(true);
53+
return $this->searchFilterThrottle;
54+
}
5555

56-
return $this;
56+
public function hasSearchBlur(): bool
57+
{
58+
return $this->searchFilterBlur !== null;
5759
}
5860

59-
public function setSearchVisibilityDisabled(): self
61+
public function getSearchOptions(): string
6062
{
61-
$this->setSearchVisibilityStatus(false);
63+
if ($this->hasSearchDebounce()) {
64+
return '.live.debounce.'.$this->getSearchDebounce().'ms';
65+
}
6266

63-
return $this;
64-
}
67+
if ($this->hasSearchDefer()) {
68+
return '';
69+
}
70+
71+
if ($this->hasSearchLive()) {
72+
return '.live';
73+
}
74+
75+
if ($this->hasSearchBlur()) {
76+
return '.blur';
77+
}
6578

79+
if ($this->hasSearchLazy()) {
80+
return '.live.lazy';
81+
}
82+
83+
if ($this->hasSearchThrottle()) {
84+
return '.live.throttle.'.$this->getSearchThrottle().'ms';
85+
}
86+
87+
return '.live';
88+
}
6689
/**
6790
* @throws DataTableConfigurationException
6891
*/
@@ -147,24 +170,5 @@ public function setSearchLazy(): self
147170
return $this;
148171
}
149172

150-
public function setTrimSearchString(bool $status): self
151-
{
152-
$this->trimSearchString = $status;
153-
154-
return $this;
155-
}
156-
157-
public function setTrimSearchStringEnabled(): self
158-
{
159-
$this->setTrimSearchString(true);
160-
161-
return $this;
162-
}
163-
164-
public function setTrimSearchStringDisabled(): self
165-
{
166-
$this->setTrimSearchString(false);
167173

168-
return $this;
169-
}
170-
}
174+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search;
4+
5+
use Livewire\Attributes\{Computed, Locked};
6+
7+
trait HandlesSearchStatus
8+
{
9+
#[Locked]
10+
public bool $searchStatus = true;
11+
12+
public function getSearchStatus(): bool
13+
{
14+
return $this->searchStatus;
15+
}
16+
17+
#[Computed]
18+
public function searchIsEnabled(): bool
19+
{
20+
return $this->getSearchStatus() === true;
21+
}
22+
23+
public function searchIsDisabled(): bool
24+
{
25+
return $this->getSearchStatus() === false;
26+
}
27+
28+
public function setSearchStatus(bool $status): self
29+
{
30+
$this->searchStatus = $status;
31+
32+
return $this;
33+
}
34+
35+
public function setSearchEnabled(): self
36+
{
37+
return $this->setSearchStatus(true);
38+
}
39+
40+
/**
41+
* @return $this
42+
*/
43+
public function setSearchDisabled(): self
44+
{
45+
$this->search = '';
46+
47+
return $this->setSearchStatus(false);
48+
}
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search;
4+
5+
trait HandlesSearchTrim
6+
{
7+
protected bool $trimSearchString = false;
8+
9+
public function shouldTrimSearchString(): bool
10+
{
11+
return $this->trimSearchString ?? false;
12+
}
13+
14+
public function setTrimSearchString(bool $status): self
15+
{
16+
$this->trimSearchString = $status;
17+
18+
return $this;
19+
}
20+
21+
public function setTrimSearchStringEnabled(): self
22+
{
23+
$this->setTrimSearchString(true);
24+
25+
return $this;
26+
}
27+
28+
public function setTrimSearchStringDisabled(): self
29+
{
30+
$this->setTrimSearchString(false);
31+
32+
return $this;
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search;
4+
5+
use Livewire\Attributes\Computed;
6+
7+
trait HandlesSearchVisibility
8+
{
9+
protected bool $searchVisibilityStatus = true;
10+
11+
public function getSearchVisibilityStatus(): bool
12+
{
13+
return $this->searchVisibilityStatus;
14+
}
15+
16+
#[Computed]
17+
public function searchVisibilityIsEnabled(): bool
18+
{
19+
return $this->getSearchVisibilityStatus() === true;
20+
}
21+
22+
public function searchVisibilityIsDisabled(): bool
23+
{
24+
return $this->getSearchVisibilityStatus() === false;
25+
}
26+
27+
public function setSearchVisibilityStatus(bool $status): self
28+
{
29+
$this->searchVisibilityStatus = $status;
30+
31+
return $this;
32+
}
33+
34+
public function setSearchVisibilityEnabled(): self
35+
{
36+
return $this->setSearchVisibilityStatus(true);
37+
}
38+
39+
public function setSearchVisibilityDisabled(): self
40+
{
41+
return $this->setSearchVisibilityStatus(false);
42+
}
43+
44+
}

src/Traits/Styling/Search/HasSearchIcon.php renamed to src/Traits/Core/Search/Styling/HasSearchIcon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Search;
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search\Styling;
44

55
use Livewire\Attributes\Computed;
66

src/Traits/Styling/Search/HasSearchInput.php renamed to src/Traits/Core/Search/Styling/HasSearchInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Search;
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\Search\Styling;
44

55
trait HasSearchInput
66
{

0 commit comments

Comments
 (0)