Skip to content

Commit b14f456

Browse files
authored
Add setFilterPillTitleAsHtml (#2204)
* Add setFilterPillTitleAsHtml * Fix styling * Add Filter Pill Handler * Fix styling * Adjust for new Pill View Component * Adjust FilterPillDataTest * Fix styling * Temporarily disable filter pill separator test
1 parent b2d36c5 commit b14f456

File tree

9 files changed

+174
-123
lines changed

9 files changed

+174
-123
lines changed

resources/views/components/tools/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@if ($filterPillData->hasCustomPillBlade)
2323
@include($filterPillData->getCustomPillBlade(), ['filter' => $this->getFilterByKey($filterKey), 'filterPillData' => $filterPillData])
2424
@else
25-
<x-livewire-tables::tools.filter-pills.pills-item :$filterKey :$filterPillData :shouldWatch="$filterPillData->isAnExternalLivewireFilter" />
25+
<x-livewire-tables::filter-pill :$filterKey :$filterPillData />
2626
@endif
2727
@endtableloop
2828

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@aware(['tableName','isTailwind','isBootstrap4','isBootstrap5'])
2+
3+
<div x-data="filterPillsHandler(@js($setupData))" x-bind="trigger"
4+
wire:key="{{ $tableName }}-filter-pill-{{ $filterKey }}" {{
5+
$attributes->merge($filterPillsItemAttributes)
6+
->class([
7+
'inline-flex items-center px-2.5 py-0.5 rounded-full leading-4' => $isTailwind && ($filterPillsItemAttributes['default-styling'] ?? true),
8+
'text-xs font-medium capitalize' => $isTailwind && ($filterPillsItemAttributes['default-text'] ?? ($filterPillsItemAttributes['default-styling'] ?? true)),
9+
'bg-indigo-100 text-indigo-800 dark:bg-indigo-200 dark:text-indigo-900' => $isTailwind && ($filterPillsItemAttributes['default-colors'] ?? true),
10+
'badge badge-pill badge-info d-inline-flex align-items-center' => $isBootstrap4 && ($filterPillsItemAttributes['default-styling'] ?? true),
11+
'badge rounded-pill bg-info d-inline-flex align-items-center' => $isBootstrap5 && ($filterPillsItemAttributes['default-styling'] ?? true),
12+
])
13+
->except(['default', 'default-styling', 'default-colors'])
14+
}}
15+
>
16+
<span {{ $attributes->merge($pillTitleDisplayDataArray) }}></span>
17+
18+
<span {{ $attributes->merge($pillDisplayDataArray) }}></span>
19+
20+
<x-livewire-tables::tools.filter-pills.buttons.reset-filter :$filterKey :$filterPillData/>
21+
22+
</div>

src/DataTransferObjects/Filters/FilterPillData.php

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,142 +6,147 @@
66

77
class FilterPillData
88
{
9+
public string $separatedValues = '';
10+
911
public function __construct(
12+
protected string $filterKey,
1013
protected string $filterPillTitle,
11-
protected string $filterSelectName,
1214
protected string|array|null $filterPillValue,
1315
protected string $separator,
1416
public bool $isAnExternalLivewireFilter,
1517
public bool $hasCustomPillBlade,
1618
protected ?string $customPillBlade,
1719
protected array $filterPillsItemAttributes,
18-
protected ?string $separatedValues,
1920
protected bool $renderPillsAsHtml,
2021
protected bool $watchForEvents,
21-
protected array $customResetButtonAttributes, ) {}
22+
protected array $customResetButtonAttributes,
23+
protected bool $renderPillsTitleAsHtml) {}
2224

23-
public static function make(string $filterPillTitle, string $filterSelectName, string|array|null $filterPillValue, string $separator = ', ', bool $isAnExternalLivewireFilter = false, bool $hasCustomPillBlade = false, ?string $customPillBlade = null, array $filterPillsItemAttributes = [], ?string $separatedValues = null, bool $renderPillsAsHtml = false, bool $watchForEvents = false, array $customResetButtonAttributes = []): FilterPillData
25+
public static function make(string $filterKey, string $filterPillTitle, string|array|null $filterPillValue, string $separator = ', ', bool $isAnExternalLivewireFilter = false, bool $hasCustomPillBlade = false, ?string $customPillBlade = null, array $filterPillsItemAttributes = [], bool $renderPillsAsHtml = false, bool $watchForEvents = false, array $customResetButtonAttributes = [], bool $renderPillsTitleAsHtml = false): FilterPillData
2426
{
2527
if ($isAnExternalLivewireFilter) {
2628
$watchForEvents = true;
2729
}
2830

29-
return new self($filterPillTitle, $filterSelectName, $filterPillValue, $separator, $isAnExternalLivewireFilter, $hasCustomPillBlade, $customPillBlade, $filterPillsItemAttributes, $separatedValues, $renderPillsAsHtml, $watchForEvents, $customResetButtonAttributes);
31+
return new self($filterKey, $filterPillTitle, $filterPillValue, $separator, $isAnExternalLivewireFilter, $hasCustomPillBlade, $customPillBlade, $filterPillsItemAttributes, $renderPillsAsHtml, $watchForEvents, $customResetButtonAttributes, $renderPillsTitleAsHtml);
3032
}
3133

3234
public function getTitle(): string
3335
{
3436
return $this->filterPillTitle;
3537
}
3638

37-
public function getSelectName(): string
39+
public function getPillValue(): array|string|null
3840
{
39-
return $this->filterSelectName;
41+
return $this->filterPillValue;
4042
}
4143

42-
public function getPillValue(): array|string|null
44+
public function getHasCustomPillBlade(): bool
4345
{
44-
return $this->filterPillValue;
46+
return $this->hasCustomPillBlade ?? false;
4547
}
4648

47-
public function isPillValueAnArray(): bool
49+
public function getCustomPillBlade(): ?string
4850
{
49-
return ! is_null($this->filterPillValue) && is_array($this->filterPillValue);
51+
return $this->customPillBlade;
5052
}
5153

52-
public function getSeparatedPillValue(): array|string|null
54+
public function getCustomResetButtonAttributes(): array
5355
{
54-
if ($this->isPillValueAnArray()) {
55-
return implode($this->getSeparator(), $this->getPillValue());
56-
} else {
57-
return $this->getPillValue();
58-
}
56+
return $this->customResetButtonAttributes ?? [];
5957
}
6058

61-
public function getValueFromPillData(): array|string|null
59+
public function getIsAnExternalLivewireFilter(): int
6260
{
63-
if ($this->isPillValueAnArray()) {
64-
return implode($this->getSeparator(), $this->getPillValue());
65-
} else {
66-
return $this->getPillValue();
67-
}
61+
return intval($this->isAnExternalLivewireFilter ?? false);
6862
}
6963

70-
public function getHasCustomPillBlade(): bool
64+
public function getSeparator(): string
7165
{
72-
return $this->hasCustomPillBlade ?? false;
66+
return $this->separator ?? ', ';
7367
}
7468

75-
public function getCustomPillBlade(): ?string
69+
public function shouldUsePillsAsHtml(): int
7670
{
77-
return $this->customPillBlade;
71+
return intval($this->renderPillsAsHtml ?? false);
7872
}
7973

80-
public function getIsAnExternalLivewireFilter(): int
74+
public function shouldUsePillsTitleAsHtml(): int
8175
{
82-
return intval($this->isAnExternalLivewireFilter ?? false);
76+
return intval($this->renderPillsTitleAsHtml ?? false);
8377
}
8478

85-
public function getSeparator(): string
79+
public function shouldWatchForEvents(): int
8680
{
87-
return $this->separator ?? ', ';
81+
return intval($this->watchForEvents ?? false);
8882
}
8983

90-
public function getSeparatedValues(): string
84+
public function isPillValueAnArray(): bool
9185
{
92-
return $this->separatedValues ?? $this->getSeparatedPillValue();
86+
return ! is_null($this->filterPillValue) && is_array($this->filterPillValue);
9387
}
9488

95-
public function getFilterPillsItemAttributes(): array
89+
public function getSeparatedPillValue(): ?string
9690
{
97-
return array_merge(['default' => true, 'default-colors' => true, 'default-styling' => true, 'default-text' => true], $this->filterPillsItemAttributes);
91+
if ($this->isPillValueAnArray()) {
92+
return implode($this->getSeparator(), $this->getPillValue());
93+
} else {
94+
return $this->getPillValue();
95+
}
9896
}
9997

100-
public function shouldUsePillsAsHtml(): int
98+
public function getSafeSeparatedPillValue(): ?string
10199
{
102-
return intval($this->renderPillsAsHtml ?? false);
100+
$string = $this->getSeparatedPillValue();
101+
102+
return htmlentities($string, ENT_QUOTES, 'UTF-8');
103+
103104
}
104105

105-
public function shouldWatchForEvents(): int
106+
public function getFilterPillsItemAttributes(): array
106107
{
107-
return intval($this->watchForEvents ?? false);
108+
return array_merge(['default' => true, 'default-colors' => true, 'default-styling' => true, 'default-text' => true], $this->filterPillsItemAttributes);
108109
}
109110

110-
public function getFilterPillDisplayData(): ComponentAttributeBag
111+
public function getFilterPillDisplayDataArray(): array
111112
{
113+
$array = [];
112114
if ($this->getIsAnExternalLivewireFilter()) {
113-
return $this->getExternalFilterPillDisplayData();
115+
return $this->getExternalFilterPillDisplayDataArray($array);
114116
}
115117

116-
return $this->getInternalFilterPillDisplayData();
118+
return $this->getInternalFilterPillDisplayDataArray($array);
117119
}
118120

119-
public function getInternalFilterPillDisplayData(): ComponentAttributeBag
121+
public function getExternalFilterPillDisplayDataArray(array $array = []): array
120122
{
121-
return new ComponentAttributeBag([
122-
'x-data' => "{ internalDisplayString: ''}",
123-
'x-init' => "internalDisplayString = updatePillValues('".$this->getSeparatedValues()."');",
124-
$this->shouldUsePillsAsHtml() ? 'x-html' : 'x-text' => 'internalDisplayString',
125-
]);
123+
$array[$this->shouldUsePillsAsHtml() ? 'x-html' : 'x-text'] = 'displayString';
124+
125+
return $array;
126126
}
127127

128-
public function getExternalFilterPillDisplayData(): ComponentAttributeBag
128+
public function getInternalFilterPillDisplayDataArray(array $array = []): array
129129
{
130-
return new ComponentAttributeBag([
131-
$this->shouldUsePillsAsHtml() ? 'x-html' : 'x-text' => 'displayString',
132-
]);
130+
131+
$array['x-data'] = "{ internalDisplayString: ''}";
132+
$array['x-init'] = 'internalDisplayString = updatePillValues('.json_encode($this->getSafeSeparatedPillValue()).')';
133+
$array[$this->shouldUsePillsAsHtml() ? 'x-html' : 'x-text'] = 'internalDisplayString';
134+
135+
return $array;
133136
}
134137

135-
public function getPillSetupData(string $filterKey = '', bool $shouldWatch = false): array
138+
public function getFilterTitleDisplayDataArray(array $array = []): array
136139
{
137-
$array = array_merge(['filterKey' => $filterKey, 'watchForEvents' => $shouldWatch], $this->toArray());
140+
$array[$this->shouldUsePillsTitleAsHtml() ? 'x-html' : 'x-text'] = "localFilterTitle + ':&nbsp;'";
138141

139142
return $array;
140143
}
141144

142-
public function getCustomResetButtonAttributes(): array
145+
public function getPillSetupData(string $filterKey = '', bool $shouldWatch = false): array
143146
{
144-
return $this->customResetButtonAttributes ?? [];
147+
$array = array_merge(['filterKey' => $filterKey, 'watchForEvents' => $shouldWatch], $this->toArray());
148+
149+
return $array;
145150
}
146151

147152
public function getCalculatedCustomResetButtonAttributes(string $filterKey, array $filterPillsResetFilterButtonAttributes): array
@@ -163,16 +168,17 @@ public function getCalculatedCustomResetButtonAttributes(string $filterKey, arra
163168
public function toArray(): array
164169
{
165170
return [
171+
'filterKey' => $this->filterKey,
166172
'filterPillTitle' => $this->getTitle(),
167-
'filterSelectName' => $this->getSelectName(),
168173
'filterPillValue' => $this->getPillValue(),
169174
'isAnExternalLivewireFilter' => $this->getIsAnExternalLivewireFilter(),
170175
'hasCustomPillBlade' => $this->getHasCustomPillBlade(),
171176
'customPillBlade' => $this->getCustomPillBlade(),
172177
'separator' => $this->getSeparator(),
178+
'separatedValues' => $this->getSafeSeparatedPillValue(),
173179
'filterPillsItemAttributes' => $this->getFilterPillsItemAttributes(),
174-
'separatedValues' => $this->getSeparatedValues(),
175180
'renderPillsAsHtml' => $this->shouldUsePillsAsHtml(),
181+
'renderPillsTitleAsHtml' => $this->shouldUsePillsTitleAsHtml(),
176182
'watchForEvents' => $this->shouldWatchForEvents(),
177183
];
178184
}

src/LaravelLivewireTablesServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function boot(): void
4343
$this->addBladeLoopDirective();
4444

4545
$this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-tables');
46+
Blade::componentNamespace('Rappasoft\\LaravelLivewireTables\\View\\Components', 'livewire-tables');
4647

4748
$this->consoleCommands();
4849

src/Traits/Filters/HandlesPillsData.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,21 @@ public function getPillDataForFilter(): array
1010
{
1111
$filters = [];
1212

13-
foreach ($this->getAppliedFiltersWithValuesForPills() as $filterSelectName => $value) {
14-
if (! is_null($filter = $this->getFilterByKey($filterSelectName))) {
15-
if ($filter->isEmpty($value)) {
16-
continue;
17-
}
18-
$customPillBlade = null;
19-
$isAnExternalLivewireFilter = (method_exists($filter, 'isAnExternalLivewireFilter') && $filter->isAnExternalLivewireFilter());
20-
$separator = method_exists($filter, 'getPillsSeparator') ? $filter->getPillsSeparator() : ', ';
21-
$separatedValues = null;
22-
23-
// dd($value);
24-
25-
if ($hasCustomPillBlade = $filter->hasCustomPillBlade()) {
26-
$customPillBlade = $filter->getCustomPillBlade();
27-
}
28-
29-
if (is_array($value) && ! empty($value)) {
30-
$separatedValues = implode($separator, $filter->getFilterPillValue($value));
31-
}
32-
13+
foreach ($this->getAppliedFiltersWithValuesForPills() as $filterKey => $value) {
14+
if (! is_null($filter = $this->getFilterByKey($filterKey))) {
3315
$filters[$filter->getKey()] = FilterPillData::make(
34-
customPillBlade: $customPillBlade,
16+
filterKey: $filter->getKey(),
17+
customPillBlade: $filter->getCustomPillBlade() ?? null,
3518
filterPillsItemAttributes: array_merge($this->getFilterPillsItemAttributes(), ($filter->hasPillAttributes() ? $filter->getPillAttributes() : [])),
3619

3720
filterPillTitle: $filter->getFilterPillTitle(),
3821
filterPillValue: $filter->getFilterPillValue($value),
3922

40-
filterSelectName: $filterSelectName,
41-
42-
hasCustomPillBlade: $hasCustomPillBlade,
43-
isAnExternalLivewireFilter: $isAnExternalLivewireFilter,
44-
separatedValues: $separatedValues,
23+
hasCustomPillBlade: $filter->hasCustomPillBlade(),
24+
isAnExternalLivewireFilter: (method_exists($filter, 'isAnExternalLivewireFilter') && $filter->isAnExternalLivewireFilter()),
4525
separator: method_exists($filter, 'getPillsSeparator') ? $filter->getPillsSeparator() : ', ',
4626
renderPillsAsHtml: $filter->getPillsAreHtml() ?? false,
27+
renderPillsTitleAsHtml: $filter->getFilterPillTitleAsHtml() ?? false,
4728
customResetButtonAttributes: $filter->getPillResetButtonAttributes(),
4829

4930
);

src/View/Components/FilterPill.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\View\Components;
4+
5+
use Illuminate\View\Component;
6+
use Rappasoft\LaravelLivewireTables\DataTransferObjects\Filters\FilterPillData;
7+
8+
class FilterPill extends Component
9+
{
10+
public bool $shouldWatch = false;
11+
12+
public function __construct(public string $filterKey, public FilterPillData $filterPillData)
13+
{
14+
$this->shouldWatch = $this->filterPillData->shouldWatchForEvents() ?? 0;
15+
}
16+
17+
public function render(): null|string|\Illuminate\Support\HtmlString|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
18+
{
19+
return view('livewire-tables::includes.filter-pill')
20+
->with([
21+
'filterPillsItemAttributes' => $this->filterPillData->getFilterPillsItemAttributes(),
22+
'pillDisplayDataArray' => $this->filterPillData->getFilterPillDisplayDataArray(),
23+
'pillTitleDisplayDataArray' => $this->filterPillData->getFilterTitleDisplayDataArray(),
24+
'setupData' => $this->filterPillData->getPillSetupData($this->filterKey, $this->shouldWatch),
25+
]);
26+
27+
}
28+
}

src/Views/Filters/Traits/Styling/HandlesFilterPillsAttributes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ trait HandlesFilterPillsAttributes
2020
*/
2121
protected array $pillResetButtonAttributes = [];
2222

23+
protected bool $pillTitleAsHtml = false;
24+
2325
public function getPillAttributesBag(): ComponentAttributeBag
2426
{
2527
return new ComponentAttributeBag($this->getPillAttributes());
@@ -82,4 +84,16 @@ public function getFilterPillResetButtonAttributesMerged(array $resetFilterButto
8284
$this->getPillResetButtonAttributes()
8385
);
8486
}
87+
88+
public function setFilterPillTitleAsHtml(bool $pillTitleAsHtml): self
89+
{
90+
$this->pillTitleAsHtml = $pillTitleAsHtml;
91+
92+
return $this;
93+
}
94+
95+
public function getFilterPillTitleAsHtml(): bool
96+
{
97+
return $this->pillTitleAsHtml;
98+
}
8599
}

0 commit comments

Comments
 (0)