Skip to content

Commit

Permalink
Add setFilterLabelAttributes Capability (#1397)
Browse files Browse the repository at this point in the history
* Add setFilterLabelAttributes Capability

* Update filter-label to use customLabelAttributes to avoid any potential conflicts

* Adjust documentation for setCustomFilterLabel

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Oct 8, 2023
1 parent 154f726 commit 16ccfe1
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 81 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

All notable changes to `laravel-livewire-tables` will be documented in this file

## [3.0.0-beta.2] - Removing superfluous item from blade
## [UNRELEASED]
- Removes superfluous @endphp from the DateRangeFilter blade
- Removes reference to remote/published 3rd party assets
- Add setFilterLabelAttributes Capability Per-Filter

## [3.0.0-beta.1] - 3.x Amends
- Amending Documentation for Reordering
Expand Down
52 changes: 39 additions & 13 deletions docs/filters/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,26 +332,52 @@ SelectFilter::make('Active')
->setCustomFilterLabel('path.to.blade')
```

You will receive two properties to your blade, filter (the filter instance), and theme (your chosen theme). You may access the filter layout as shown below

Example blade:
You will receive several properties to your blade, explained here:
- $filter (the filter instance)
- $filterLayout ('slide-down' or 'popover')
- $tableName (the table name)
- $isTailwind (bool - is theme Tailwind)
- $isBootstrap (bool - is theme Bootstrap 4 or Bootstrap 5)
- $isBootstrap4 (bool - is theme Bootstrap 4)
- $isBootstrap5 (bool - is theme Bootstrap 5)
- $customLabelAttributes (array -> any customLabel attributes set using setFilterLabelAttributes())

Example label blade:
```php
@aware(['component'])
@props(['filter'])
@props(['filter', 'filterLayout' => 'popover', 'tableName' => 'table', 'isTailwind' => false, 'isBootstrap' => false, 'isBootstrap4' => false, 'isBootstrap5' => false, 'customLabelAttributes' => [])


<label for="{{ $tableName }}-filter-{{ $filter->getKey() }}"

{{
$attributes->merge($customLabelAttributes)
->class(['block text-sm font-medium leading-5 text-gray-700 dark:text-white' => $isTailwind && $customLabelAttributes['default'] ?? true])
->class(['d-block' => $isBootstrap && $filterLayout == 'slide-down' && $customLabelAttributes['default'] ?? true])
->class(['mb-2' => $isBootstrap && $filterLayout == 'popover' && $customLabelAttributes['default'] ?? true])
->except('default')
}}

<label for="{{ $component->getTableName() }}-filter-{{ $filter->getKey() }}"
@class([
'block text-sm font-large leading-5 text-red-700 dark:text-red-700' => $component->isTailwind(),
'd-block' => $component->isBootstrap4() && $component->isFilterLayoutSlideDown(),
'mb-2' => $component->isBootstrap4() && $component->isFilterLayoutPopover(),
'd-block display-4' => $component->isBootstrap5() && $component->isFilterLayoutSlideDown(),
'mb-2 display-4' => $component->isBootstrap5() && $component->isFilterLayoutPopover(),
])
>
{{ $filter->getName() }}
</label>
```

### setFilterLabelAttributes
Set custom attributes for a Filter Label. At present it is recommended to only use this for "class" and "style" attributes to avoid conflicts.

By default, this replaces the default classes on the Filter Label wrapper, if you would like to keep them, set the default flag to true.

```php
TextFilter::make('Name')
->setFilterLabelAttributes(
[
'class' => 'text-xl',
'default' => true,
]
),
```


### Config

If the filter takes any config options, you can set them with the `config` method:
Expand Down
35 changes: 24 additions & 11 deletions resources/views/components/tools/filter-label.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
@aware(['component', 'tableName'])
@props(['filter', 'filterLayout' => 'popover', 'tableName' => 'table'])

<label for="{{ $tableName }}-filter-{{ $filter->getKey() }}"
@class([
'block text-sm font-medium leading-5 text-gray-700 dark:text-white' => $component->isTailwind(),
'd-block' => $component->isBootstrap() && $filterLayout == 'slide-down',
'mb-2' => $component->isBootstrap() && $filterLayout == 'popover',
])
>
{{ $filter->getName() }}
</label>
@props(['filter', 'filterLayout' => 'popover', 'tableName' => 'table', 'isTailwind' => false, 'isBootstrap' => false, 'isBootstrap4' => false, 'isBootstrap5' => false])

@php
$customLabelAttributes = $filter->getFilterLabelAttributes();
@endphp

@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName, 'isTailwind' => $isTailwind, 'isBootstrap' => $isBootstrap, 'isBootstrap4' => $isBootstrap4, 'isBootstrap5' => $isBootstrap5, 'customLabelAttributes' => $customLabelAttributes])
@elseif(!$filter->hasCustomPosition())
<label for="{{ $tableName }}-filter-{{ $filter->getKey() }}"

{{
$attributes->merge($customLabelAttributes)
->class(['block text-sm font-medium leading-5 text-gray-700 dark:text-white' => $isTailwind && $customLabelAttributes['default'] ?? true])
->class(['d-block' => $isBootstrap && $filterLayout == 'slide-down' && $customLabelAttributes['default'] ?? true])
->class(['mb-2' => $isBootstrap && $filterLayout == 'popover' && $customLabelAttributes['default'] ?? true])
->except('default')
}}

>
{{ $filter->getName() }}
</label>

@endif
11 changes: 3 additions & 8 deletions resources/views/components/tools/filters/date-range.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
}
}
@endphp

<div x-cloak id="{{ $tableName }}-dateRangeFilter-{{ $filterKey }}" x-data="flatpickrFilter($wire, '{{ $filterKey }}', @js($filter->getConfigs()), $refs.dateRangeInput, '{{ App::currentLocale() }}')" >


<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(), ['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />


<div
@class([
Expand All @@ -55,4 +50,4 @@
</div>

</div>
</div>
</div>
9 changes: 3 additions & 6 deletions resources/views/components/tools/filters/date.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(), ['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />


<div @class([
"rounded-md shadow-sm" => $isTailwind,
Expand All @@ -22,4 +19,4 @@
])
/>
</div>
</div>
</div>
8 changes: 2 additions & 6 deletions resources/views/components/tools/filters/datetime.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(), ['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />

<div @class([
"rounded-md shadow-sm" => $isTailwind,
Expand All @@ -22,4 +18,4 @@
])
/>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />


@if ($isTailwind)
<div class="rounded-md shadow-sm">
Expand Down Expand Up @@ -52,4 +49,4 @@ class="{{ $isBootstrap4 ? 'form-control' : 'form-select' }}"
@endforeach
</select>
@endif
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />


@if ($isTailwind)
<div class="rounded-md">
Expand Down Expand Up @@ -62,4 +59,4 @@ class="form-check-input"
</div>
@endforeach
@endif
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
$currentMax = $maxRange = $filter->getConfig('maxRange');
@endphp
<div id="{{ $tableName }}-numberRange-{{ $filterKey }}" x-data="numberRangeFilter($wire,'{{ $filterKey }}', '{{ $tableName }}-numberRange-{{ $filterKey }}-wrapper', @js($filter->getConfigs()), '{{ $tableName }}-numberRange-{{ $filterKey }}')" x-on:mousedown.away.throttle.2000ms="updateWireable" x-on:touchstart.away.throttle.2000ms="updateWireable" x-on:mouseleave.throttle.2000ms="updateWireable">
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName ])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />


<div @class([
'mt-4 h-22 pt-8 pb-4 grid gap-10' => $isTailwind,
Expand Down Expand Up @@ -38,4 +35,4 @@
<div class='range-slider__progress'></div>
</div>
</div>
</div>
</div>
9 changes: 3 additions & 6 deletions resources/views/components/tools/filters/number.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />


<div @class([
"rounded-md shadow-sm" => $isTailwind,
Expand All @@ -22,4 +19,4 @@
])
/>
</div>
</div>
</div>
8 changes: 2 additions & 6 deletions resources/views/components/tools/filters/select.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />

<div @class([
'rounded-md shadow-sm' => $isTailwind,
Expand Down Expand Up @@ -32,4 +28,4 @@
@endforeach
</select>
</div>
</div>
</div>
8 changes: 2 additions & 6 deletions resources/views/components/tools/filters/text-field.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<div>
@if($filter->hasCustomFilterLabel() && !$filter->hasCustomPosition())
@include($filter->getCustomFilterLabel(),['filter' => $filter, 'filterLayout' => $filterLayout, 'tableName' => $tableName])
@elseif(!$filter->hasCustomPosition())
<x-livewire-tables::tools.filter-label :filter="$filter" :filterLayout="$filterLayout" :tableName="$tableName" />
@endif
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />

<div @class([
"rounded-md shadow-sm" => $isTailwind,
Expand All @@ -22,4 +18,4 @@
])
/>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions src/Views/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class Filter

protected ?string $filterCustomLabel = null;

protected array $filterLabelAttributes = [];

protected ?int $filterSlidedownRow = null;

protected ?int $filterSlidedownColspan = null;
Expand Down
7 changes: 7 additions & 0 deletions src/Views/Traits/Configuration/FilterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ public function setFilterDefaultValue($value): self

return $this;
}

public function setFilterLabelAttributes(array $filterLabelAttributes): self
{
$this->filterLabelAttributes = [...['default' => false], ...$filterLabelAttributes];

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Views/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,14 @@ public function hasFilterDefaultValue(): bool
{
return ! is_null($this->filterDefaultValue);
}

public function getFilterLabelAttributes(): array
{
return [...['default' => true], ...$this->filterLabelAttributes];
}

public function hasFilterLabelAttributes(): bool
{
return $this->getFilterLabelAttributes() != ['default' => true] && $this->getFilterLabelAttributes() != ['default' => false];
}
}
27 changes: 27 additions & 0 deletions tests/Views/Traits/Helpers/FilterHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,31 @@ public function can_get_filter_custom_filter_pills_blade(): void
$this->assertTrue($filter->hasCustomPillBlade());
$this->assertSame('foo', $filter->getCustomPillBlade());
}

/** @test */
public function can_get_filter_label_attributes(): void
{
$filter1 = TextFilter::make('Filter1');
$filter2 = TextFilter::make('Filter2')->setFilterLabelAttributes(
['class' => 'text-xl', 'default' => true]
);
$filter3 = TextFilter::make('Filter3')->setFilterLabelAttributes(
['class' => 'text-2xl', 'default' => false]
);

$this->assertFalse($filter1->hasFilterLabelAttributes());
$this->assertTrue($filter2->hasFilterLabelAttributes());
$this->assertTrue($filter3->hasFilterLabelAttributes());

$this->assertSame($filter1->getFilterLabelAttributes(), ['default' => true]);
$this->assertSame($filter2->getFilterLabelAttributes(), ['default' => true, 'class' => 'text-xl']);
$this->assertSame($filter3->getFilterLabelAttributes(), ['default' => false, 'class' => 'text-2xl']);

$filter1->setFilterLabelAttributes(
['class' => 'text-3xl', 'default' => false]
);
$this->assertTrue($filter1->hasFilterLabelAttributes());
$this->assertSame($filter1->getFilterLabelAttributes(), ['default' => false, 'class' => 'text-3xl']);

}
}

0 comments on commit 16ccfe1

Please sign in to comment.