Skip to content

Commit

Permalink
Add Separator customisation for Array Filters for FilterPills (#1772)
Browse files Browse the repository at this point in the history
* Add Filter Pills Separation Methods

* Add FilterVisuals test for Separator


---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Jul 13, 2024
1 parent a365801 commit 73d761b
Show file tree
Hide file tree
Showing 25 changed files with 327 additions and 34 deletions.
2 changes: 0 additions & 2 deletions docs/filter-types/filters-date.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Date Filters
weight: 2
---

## Date Filters

Date filters are HTML date elements.

```php
Expand Down
2 changes: 0 additions & 2 deletions docs/filter-types/filters-daterange.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: DateRange Filters
weight: 3
---

## DateRange Filters

DateRange filters are Flatpickr based components, and simply filtering by a date range. If you would like to more smoothly filter your query by a start and end date, you can use the DateRangeFilter:

```php
Expand Down
2 changes: 0 additions & 2 deletions docs/filter-types/filters-datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: DateTime Filters
weight: 4
---

## DateTime Filters

DateTime filters are HTML datetime-local elements and act the same as date filters.

```php
Expand Down
2 changes: 0 additions & 2 deletions docs/filter-types/filters-livewire-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Livewire Custom Filter (Beta)
weight: 11
---

## Livewire Custom Filter

**IN BETA**
This feature is currently in beta, and use in production is not recommended.

Expand Down
26 changes: 23 additions & 3 deletions docs/filter-types/filters-multiselect-dropdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ title: Multi-Select Dropdown Filters
weight: 5
---


## Multi-select dropdown Filters

Multi-select dropdown filters are a simple dropdown list. The user can select multiple options from the list. There is also an 'All' option that will select all values

```php
Expand All @@ -27,3 +24,26 @@ public function filters(): array
];
}
```

## Filter Pills Separator

As this filter returns one or more values, you have the option to utilise a custom separator for the values displayed in the Filter Pills section at the top of the table. The default is ", ", but you may use any HTML string to separate the selected values

```php
public function filters(): array
{
return [
MultiSelectFilter::make('Tags')
->options(
Tag::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn($tag) => $tag->name)
->toArray()
)
->setPillsSeparator('<br />'),
];
}

```
25 changes: 23 additions & 2 deletions docs/filter-types/filters-multiselect.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Multi-Select Filters
weight: 6
---

## Multi-select Filters

Multi-select filters are a list of checkboxes. The user can select multiple options from the list. There is also an 'All' option that will select all values.

```php
Expand All @@ -25,3 +23,26 @@ public function filters(): array
];
}
```

## Filter Pills Separator

As this filter returns one or more values, you have the option to utilise a custom separator for the values displayed in the Filter Pills section at the top of the table. The default is ", ", but you may use any HTML string to separate the selected values

```php
public function filters(): array
{
return [
MultiSelectFilter::make('Tags')
->options(
Tag::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn($tag) => $tag->name)
->toArray()
)
->setPillsSeparator('<br />'),
];
}

```
2 changes: 0 additions & 2 deletions docs/filter-types/filters-number-range.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: NumberRange Filters
weight: 7
---

## NumberRange Filters

NumberRange filters allow for a minimum and maximum value to be input on a single slider.

```php
Expand Down
2 changes: 0 additions & 2 deletions docs/filter-types/filters-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Number Filters
weight: 8
---

## Number Filters

Number filters are just HTML number inputs.

```php
Expand Down
2 changes: 0 additions & 2 deletions docs/filter-types/filters-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Select Filters
weight: 9
---

## Select Filters

Select filters are a simple dropdown list. The user selects one choice from the list.

```php
Expand Down
2 changes: 0 additions & 2 deletions docs/filter-types/filters-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: Text Filters
weight: 10
---

## Text Filters

Text filters are just HTML text fields.

```php
Expand Down
33 changes: 33 additions & 0 deletions docs/filter-types/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,36 @@ weight: 1
---

There are several Filter types available for use, offering a range of capabilities to filter your data.

<ul>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-date">Date Filter</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-daterange">DateRange Filter</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-datetime">DateTime Filter</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-livewire-component">Livewire Component (BETA)</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-multiselect-dropdown">Multi Select Filter (Dropdown)</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-multiselect">Multi-Select Filter (Checkbox)</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-number-range">Number Range Filter</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-number">Number Filter</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-select">Select Filter</a>
</li>
<li>
<a href="https://rappasoft.com/docs/laravel-livewire-tables/v3/filter-types/filters-text">Text Filter</a>
</li>
</ul>
12 changes: 11 additions & 1 deletion resources/views/components/tools/filter-pills.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@
'badge rounded-pill bg-info d-inline-flex align-items-center' => $component->isBootstrap5(),
])
>
{{ $filter->getFilterPillTitle() }}: {{ $filter->getFilterPillValue($value) }}
{{ $filter->getFilterPillTitle() }}:
@php( $filterPillValue = $filter->getFilterPillValue($value))
@php( $separator = method_exists($filter, 'getPillsSeparator') ? $filter->getPillsSeparator() : ', ')

@if(is_array($filterPillValue) && !empty($filterPillValue))
@foreach($filterPillValue as $filterPillArrayValue)
{{ $filterPillArrayValue }}{!! $separator !!}
@endforeach
@else
{{ $filterPillValue }}
@endif

@if ($component->isTailwind())
<button
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Filters/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function validate(string $value): string|bool
return $value;
}

public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{
if ($this->validate($value)) {
return DateTime::createFromFormat('Y-m-d', $value)->format($this->getConfig('pillFormat'));
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getDefaultValue(): array
return [];
}

public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{
$validatedValue = $this->validate($value);

Expand Down
2 changes: 1 addition & 1 deletion src/Views/Filters/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function validate(string $value): string|bool
return $value;
}

public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{
if ($this->validate($value)) {
return DateTime::createFromFormat('Y-m-d\TH:i', $value)->format($this->getConfig('pillFormat'));
Expand Down
4 changes: 2 additions & 2 deletions src/Views/Filters/MultiSelectDropdownFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function validate(int|string|array $value): array|int|string|bool
return (is_string($value) || is_numeric($value)) ? $value : false;
}

public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{
$values = [];

Expand All @@ -52,7 +52,7 @@ public function getFilterPillValue($value): ?string
}
}

return implode(', ', $values);
return $values;
}

public function isEmpty(mixed $value): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Views/Filters/MultiSelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function validate(int|string|array $value): array|int|string|bool
return $value;
}

public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{
$values = [];

Expand All @@ -46,6 +46,6 @@ public function getFilterPillValue($value): ?string
}
}

return implode(', ', $values);
return $values;
}
}
2 changes: 1 addition & 1 deletion src/Views/Filters/NumberRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getDefaultValue(): array|string
return [];
}

public function getFilterPillValue($values): ?string
public function getFilterPillValue($values): string|array|null
{
if ($this->validate($values)) {
return __('Min:').$values['min'].', '.__('Max:').$values['max'];
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Filters/SelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function validate(string $value): array|string|bool
return $value;
}

public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{

return $this->getCustomFilterPillValue($value)
Expand Down
14 changes: 14 additions & 0 deletions src/Views/Traits/Filters/IsArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

trait IsArrayFilter
{
public string $pillsSeparator = ', ';

/**
* Get the filter default options.
*/
Expand All @@ -32,4 +34,16 @@ public function isEmpty(mixed $value): bool

return empty($value);
}

public function getPillsSeparator(): string
{
return $this->pillsSeparator ?? ', ';
}

public function setPillsSeparator(string $pillsSeparator): self
{
$this->pillsSeparator = $pillsSeparator;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Views/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getFilterPillTitle(): string
/**
* @param mixed $value
*/
public function getFilterPillValue($value): ?string
public function getFilterPillValue($value): string|array|null
{
return $value;
}
Expand Down
Loading

0 comments on commit 73d761b

Please sign in to comment.