Skip to content

Commit 772c49c

Browse files
authored
Add Icon to Search Input (rappasoft#2092)
* Initial Commit * Add note - only tailwind for now --------- Co-authored-by: lrljoe <[email protected]>
1 parent 76e4b77 commit 772c49c

File tree

9 files changed

+340
-44
lines changed

9 files changed

+340
-44
lines changed

docs/search/available-methods.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,39 @@ public function configure(): void
192192
$this->setTrimSearchStringDisabled();
193193
}
194194
```
195+
196+
## Search Icon
197+
198+
To help customise, a "Search Input Icon" has been added, allowing for the addition of an icon to the search input field.
199+
200+
At present, the Search Icon is only available as a "left aligned" icon.
201+
202+
This is presently only available for Tailwind implementations
203+
204+
### setSearchIcon
205+
206+
This adds an Icon to the Search Input Field, which expects an icon path (e.g. heroicon-m-magnifying-glass)
207+
208+
```php
209+
public function configure(): void
210+
{
211+
$this->setSearchIcon('heroicon-m-magnifying-glass');
212+
}
213+
```
214+
215+
### setSearchIconAttributes
216+
217+
This allows you to specify attributes for the Search Icon for the Input Field.
218+
219+
Note that classes will be injected prior to styles, due to the behaviour of icons.
220+
221+
```php
222+
public function configure(): void
223+
{
224+
$this->setSearchIconAttributes([
225+
'class' => 'h-4 w-4',
226+
'style' => 'color: #000000',
227+
]);
228+
}
229+
230+
```

resources/views/components/tools/toolbar/items/search-field.blade.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,34 @@
33
<div
44
@class([
55
'mb-3 mb-md-0 input-group' => $this->isBootstrap,
6-
'flex rounded-md shadow-sm' => $this->isTailwind,
6+
'rounded-md shadow-sm' => $this->isTailwind,
7+
'flex' => !$this->hasSearchIcon,
8+
'relative inline-flex flex-row' => $this->hasSearchIcon,
79
])>
10+
11+
@if($this->hasSearchIcon)
12+
<div class="relative inset-y-0 left-6
13+
inline-flex items-center
14+
pointer-events-none">
15+
16+
@svg($this->getSearchIcon, $this->getSearchIconClasses, $this->getSearchIconOtherAttributes())
17+
18+
</div>
19+
@endif
20+
821
<input
922
wire:model{{ $this->getSearchOptions() }}="search"
1023
placeholder="{{ $this->getSearchPlaceholder() }}"
1124
type="text"
1225
{{
1326
$attributes->merge($this->getSearchFieldAttributes())
1427
->class([
15-
'block w-full rounded-md shadow-sm transition duration-150 ease-in-out sm:text-sm sm:leading-5 rounded-none rounded-l-md focus:ring-0 focus:border-gray-300' => $this->isTailwind && $this->hasSearch() && (($this->getSearchFieldAttributes()['default'] ?? true) || ($this->getSearchFieldAttributes()['default-styling'] ?? true)),
16-
'block w-full rounded-md shadow-sm transition duration-150 ease-in-out sm:text-sm sm:leading-5 rounded-md focus:ring focus:ring-opacity-50' => $this->isTailwind && !$this->hasSearch() && (($this->getSearchFieldAttributes()['default'] ?? true) || ($this->getSearchFieldAttributes()['default-styling'] ?? true)),
28+
'rounded-md shadow-sm transition duration-150 ease-in-out sm:text-sm sm:leading-5 rounded-none rounded-l-md focus:ring-0 focus:border-gray-300' => $this->isTailwind && $this->hasSearch() && (($this->getSearchFieldAttributes()['default'] ?? true) || ($this->getSearchFieldAttributes()['default-styling'] ?? true)),
29+
'rounded-md shadow-sm transition duration-150 ease-in-out sm:text-sm sm:leading-5 rounded-md focus:ring focus:ring-opacity-50' => $this->isTailwind && !$this->hasSearch() && (($this->getSearchFieldAttributes()['default'] ?? true) || ($this->getSearchFieldAttributes()['default-styling'] ?? true)),
1730
'border-gray-300 dark:bg-gray-700 dark:text-white dark:border-gray-600 focus:border-gray-300' => $this->isTailwind && $this->hasSearch() && (($this->getSearchFieldAttributes()['default'] ?? true) || ($this->getSearchFieldAttributes()['default-colors'] ?? true)),
1831
'border-gray-300 dark:bg-gray-700 dark:text-white dark:border-gray-600 focus:border-indigo-300 focus:ring-indigo-200' => $this->isTailwind && !$this->hasSearch() && (($this->getSearchFieldAttributes()['default'] ?? true) || ($this->getSearchFieldAttributes()['default-colors'] ?? true)),
19-
32+
'block w-full' => !$this->hasSearchIcon,
33+
'pl-8 pr-4' => $this->hasSearchIcon,
2034
'form-control' => $this->isBootstrap && $this->getSearchFieldAttributes()['default'] ?? true,
2135
])
2236
->except(['default','default-styling','default-colors'])

src/Traits/Configuration/SearchConfiguration.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,6 @@ public function setSearchLazy(): self
147147
return $this;
148148
}
149149

150-
public function setSearchPlaceholder(string $placeholder): self
151-
{
152-
$this->searchPlaceholder = $placeholder;
153-
154-
return $this;
155-
}
156-
157-
public function setSearchFieldAttributes(array $attributes = []): self
158-
{
159-
$this->setCustomAttributes('searchFieldAttributes', array_merge(['default' => false, 'default-colors' => false, 'default-styling' => false], $attributes));
160-
161-
return $this;
162-
}
163-
164150
public function setTrimSearchString(bool $status): self
165151
{
166152
$this->trimSearchString = $status;

src/Traits/Helpers/SearchHelpers.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,6 @@ public function getSearchOptions(): string
121121
return '.live';
122122
}
123123

124-
public function getSearchPlaceholder(): string
125-
{
126-
if ($this->hasSearchPlaceholder()) {
127-
return $this->searchPlaceholder;
128-
}
129-
130-
return __($this->getLocalisationPath().'Search');
131-
}
132-
133-
public function hasSearchPlaceholder(): bool
134-
{
135-
return $this->searchPlaceholder !== null;
136-
}
137-
138-
public function getSearchFieldAttributes(): array
139-
{
140-
return $this->getCustomAttributes('searchFieldAttributes', true);
141-
}
142-
143124
public function shouldTrimSearchString(): bool
144125
{
145126
return $this->trimSearchString ?? false;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Styling;
4+
5+
use Rappasoft\LaravelLivewireTables\Traits\Styling\Search\{HasSearchIcon, HasSearchInput};
6+
7+
trait HasSearchFieldStyling
8+
{
9+
use HasSearchIcon,
10+
HasSearchInput;
11+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Search;
4+
5+
use Livewire\Attributes\Computed;
6+
7+
trait HasSearchIcon
8+
{
9+
protected bool $searchIconSet = false;
10+
11+
protected ?string $searchIcon = null;
12+
13+
protected array $searchIconAttributes = ['class' => 'h-4 w-4', 'style' => 'color: #000000'];
14+
15+
#[Computed]
16+
public function hasSearchIcon(): bool
17+
{
18+
return $this->searchIconSet;
19+
}
20+
21+
#[Computed]
22+
public function getSearchIcon(): string
23+
{
24+
return $this->hasSearchIcon() ? $this->searchIcon : 'heroicon-m-magnifying-glass';
25+
}
26+
27+
#[Computed]
28+
public function getSearchIconClasses(): string
29+
{
30+
return $this->getSearchIconAttributes()['class'];
31+
32+
}
33+
34+
#[Computed]
35+
public function getSearchIconAttributes(): array
36+
{
37+
return $this->searchIconAttributes;
38+
}
39+
40+
#[Computed]
41+
public function getSearchIconOtherAttributes(): array
42+
{
43+
return collect($this->getSearchIconAttributes())->except('class')->toArray();
44+
}
45+
46+
protected function setSearchIconStatus(bool $searchIconStatus): self
47+
{
48+
$this->searchIconSet = $searchIconStatus;
49+
50+
return $this;
51+
}
52+
53+
protected function searchIconEnabled(): self
54+
{
55+
return $this->setSearchIconStatus(true);
56+
}
57+
58+
protected function searchIconDisabled(): self
59+
{
60+
return $this->setSearchIconStatus(false);
61+
}
62+
63+
protected function setSearchIcon(string $searchIcon): self
64+
{
65+
$this->searchIcon = $searchIcon;
66+
67+
return $this->searchIconEnabled();
68+
}
69+
70+
protected function setSearchIconAttributes(array $searchIconAttributes): self
71+
{
72+
$this->searchIconAttributes = array_merge($this->searchIconAttributes, $searchIconAttributes);
73+
74+
return $this->searchIconEnabled();
75+
}
76+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Search;
4+
5+
use Livewire\Attributes\Computed;
6+
7+
trait HasSearchInput
8+
{
9+
protected array $searchFieldAttributes = [];
10+
11+
protected ?string $searchPlaceholder = null;
12+
13+
protected function setSearchFieldAttributes(array $attributes = []): self
14+
{
15+
$this->setCustomAttributes('searchFieldAttributes', array_merge(['default' => false, 'default-colors' => false, 'default-styling' => false], $attributes));
16+
17+
return $this;
18+
}
19+
20+
public function getSearchFieldAttributes(): array
21+
{
22+
return $this->getCustomAttributes('searchFieldAttributes', true);
23+
}
24+
25+
public function setSearchPlaceholder(string $placeholder): self
26+
{
27+
$this->searchPlaceholder = $placeholder;
28+
29+
return $this;
30+
}
31+
32+
public function getSearchPlaceholder(): string
33+
{
34+
if ($this->hasSearchPlaceholder()) {
35+
return $this->searchPlaceholder;
36+
}
37+
38+
return __($this->getLocalisationPath().'Search');
39+
}
40+
41+
public function hasSearchPlaceholder(): bool
42+
{
43+
return $this->searchPlaceholder !== null;
44+
}
45+
}

src/Traits/WithSearch.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
use Rappasoft\LaravelLivewireTables\Traits\Configuration\SearchConfiguration;
99
use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSearch;
1010
use Rappasoft\LaravelLivewireTables\Traits\Helpers\SearchHelpers;
11+
use Rappasoft\LaravelLivewireTables\Traits\Styling\HasSearchFieldStyling;
1112

1213
trait WithSearch
1314
{
1415
use SearchConfiguration,
1516
SearchHelpers;
1617
use HasQueryStringForSearch;
18+
use HasSearchFieldStyling;
1719

1820
public string $search = '';
1921

2022
#[Locked]
2123
public bool $searchStatus = true;
2224

23-
protected ?string $searchPlaceholder = null;
24-
2525
protected bool $searchVisibilityStatus = true;
2626

2727
protected ?bool $searchFilterBlur = null;
@@ -36,8 +36,6 @@ trait WithSearch
3636

3737
protected ?int $searchFilterThrottle = null;
3838

39-
protected array $searchFieldAttributes = [];
40-
4139
protected bool $trimSearchString = false;
4240

4341
// TODO

0 commit comments

Comments
 (0)