Skip to content

Commit

Permalink
V3 add custom filter view (#1518)
Browse files Browse the repository at this point in the history
* Add viewPath for Filters

* Update Filter ViewPath

* Add available methods for setCustomView

* Add Tests

* Migrate Column/Filter View properties to IsColumn & IsFilter traits to standardise

* Fix styling

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Nov 2, 2023
1 parent 29885e6 commit 03a19f0
Show file tree
Hide file tree
Showing 28 changed files with 294 additions and 123 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
- Modify Filters to use DTO for Generic Property List by @lrljoe in [#1503](https://github.com/rappasoft/laravel-livewire-tables/pull/1503)
- Split ConfigurableAreas, CollapsingColumns and TableAttributes into own Traits/Config/Helper Files for Maintainability by @lrljoe in [#1514](https://github.com/rappasoft/laravel-livewire-tables/pull/1514)
- Add "HasAllTraits" for Maintainability by @lrljoe in [#1514](https://github.com/rappasoft/laravel-livewire-tables/pull/1514)
- Add setCustomView for Filters
- Add IsFilter and IsColumn Traits for Filter/Column Classes

## [v3.1.0] - 2023-10-31
- Restore wire:confirm for Bulk Actions
Expand Down
9 changes: 9 additions & 0 deletions docs/filters/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ TextFilter::make('Name')
),
```

### setCustomView
Use a fully custom view for a filter. This will utilise solely your view when rendering this filter. Note that the following methods will no longer apply to a filter using this:
- setCustomFilterLabel
- setFilterLabelAttributes

```php
TextFilter::make('Name')
->setCustomView('text-custom-view'),
```

### Config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
])
id="{{ $tableName }}-filter-{{ $filter->getKey() }}-wrapper"
>
{{ $filter->setFilterGenericData($filterGenericData)->render($filterGenericData) }}
{{ $filter->setGenericDisplayData($filterGenericData)->render() }}
</div>
@endforeach

Expand Down
75 changes: 2 additions & 73 deletions src/Views/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,11 @@
namespace Rappasoft\LaravelLivewireTables\Views;

use Illuminate\Support\Str;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ColumnConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ColumnHelpers;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\RelationshipHelpers;
use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn;

class Column
{
use ColumnConfiguration,
ColumnHelpers,
RelationshipHelpers;

protected ?DataTableComponent $component = null;

// What displays in the columns header
protected string $title;

// Act as a unique identifier for the column
protected string $hash;

// The columns or relationship location: i.e. name, or address.group.name
protected ?string $from = null;

// The underlying columns name: i.e. name
protected ?string $field = null;

// The table of the columns or relationship
protected ?string $table = null;

// An array of relationships: i.e. address.group.name => ['address', 'group']
protected array $relations = [];

protected bool $sortable = false;

protected mixed $sortCallback = null;

protected bool $searchable = false;

protected mixed $searchCallback = null;

protected bool $collapseOnMobile = false;

protected bool $collapseOnTablet = false;

protected bool $collapseAlways = false;

protected ?string $sortingPillTitle = null;

protected ?string $sortingPillDirectionAsc = null;

protected ?string $sortingPillDirectionDesc = null;

protected bool $eagerLoadRelations = false;

protected mixed $formatCallback = null;

protected bool $html = false;

protected mixed $labelCallback = null;

protected bool $hidden = false;

protected bool $selectable = true;

protected bool $selected = true;

protected bool $secondaryHeader = false;

protected mixed $secondaryHeaderCallback = null;

protected bool $footer = false;

protected mixed $footerCallback = null;

protected bool $clickable = true;

protected ?string $customSlug = null;
use IsColumn;

public function __construct(string $title, string $from = null)
{
Expand Down
42 changes: 2 additions & 40 deletions src/Views/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,11 @@
namespace Rappasoft\LaravelLivewireTables\Views;

use Illuminate\Support\Str;
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\FilterConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\FilterHelpers;
use Rappasoft\LaravelLivewireTables\Views\Traits\IsFilter;

abstract class Filter
{
use FilterConfiguration,
FilterHelpers;

protected string $name;

protected string $key;

protected bool $hiddenFromMenus = false;

protected bool $hiddenFromPills = false;

protected bool $hiddenFromFilterCount = false;

protected bool $resetByClearButton = true;

protected mixed $filterCallback = null;

public array $config = [];

protected ?string $filterPillTitle = null;

protected array $filterPillValues = [];

public ?string $filterPosition = null;

protected ?string $filterCustomLabel = null;

protected array $filterLabelAttributes = [];

protected ?int $filterSlidedownRow = null;

protected ?int $filterSlidedownColspan = null;

protected ?string $filterCustomPillBlade = null;

protected mixed $filterDefaultValue = null;

public array $genericDisplayData = [];
use IsFilter;

public function __construct(string $name, string $key = null)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Views/Filters/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class DateFilter extends Filter
{
public string $viewPath = 'livewire-tables::components.tools.filters.date';

public function config(array $config = []): DateFilter
{
$this->config = [...config('livewire-tables.dateFilter.defaultConfig'), ...$config];
Expand Down Expand Up @@ -47,6 +49,6 @@ public function getFilterDefaultValue(): ?string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.date', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class DateRangeFilter extends Filter
{
public array $options = [];

public string $viewPath = 'livewire-tables::components.tools.filters.date-range';

public function config(array $config = []): DateRangeFilter
{
$this->config = [...config('livewire-tables.dateRange.defaultConfig'), ...$config];
Expand Down Expand Up @@ -190,6 +192,6 @@ public function getDateString(string|array $dateInput): string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory

Check warning on line 193 in src/Views/Filters/DateRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/DateRangeFilter.php#L193

Added line #L193 was not covered by tests
{
return view('livewire-tables::components.tools.filters.date-range', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());

Check warning on line 195 in src/Views/Filters/DateRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/DateRangeFilter.php#L195

Added line #L195 was not covered by tests
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class DateTimeFilter extends Filter
{
public string $viewPath = 'livewire-tables::components.tools.filters.datetime';

public function config(array $config = []): DateTimeFilter
{
$this->config = [...config('livewire-tables.dateTimeFilter.defaultConfig'), ...$config];
Expand Down Expand Up @@ -47,6 +49,6 @@ public function getFilterDefaultValue(): ?string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.datetime', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/MultiSelectDropdownFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class MultiSelectDropdownFilter extends Filter
{
public array $options = [];

public string $viewPath = 'livewire-tables::components.tools.filters.multi-select-dropdown';

protected string $firstOption = '';

public function setFirstOption(string $firstOption): MultiSelectDropdownFilter
Expand Down Expand Up @@ -107,6 +109,6 @@ public function isEmpty($value): bool

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.multi-select-dropdown', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/MultiSelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class MultiSelectFilter extends Filter
{
public array $options = [];

public string $viewPath = 'livewire-tables::components.tools.filters.multi-select';

protected string $firstOption = '';

public function setFirstOption(string $firstOption): MultiSelectFilter
Expand Down Expand Up @@ -96,6 +98,6 @@ public function isEmpty($value): bool

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.multi-select', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class NumberFilter extends Filter
{
public string $viewPath = 'livewire-tables::components.tools.filters.number';

public function validate(mixed $value): int|bool
{
return is_numeric($value) ? $value : false;
Expand All @@ -26,6 +28,6 @@ public function getFilterDefaultValue(): ?string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.number', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/NumberRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class NumberRangeFilter extends Filter
{
public array $options = [];

public string $viewPath = 'livewire-tables::components.tools.filters.number-range';

public function options(array $options = []): NumberRangeFilter
{
$this->options = [...config('livewire-tables.numberRange.defaultOptions'), ...$options];
Expand Down Expand Up @@ -88,6 +90,6 @@ public function getFilterPillValue($values): ?string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory

Check warning on line 91 in src/Views/Filters/NumberRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/NumberRangeFilter.php#L91

Added line #L91 was not covered by tests
{
return view('livewire-tables::components.tools.filters.number-range', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());

Check warning on line 93 in src/Views/Filters/NumberRangeFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Views/Filters/NumberRangeFilter.php#L93

Added line #L93 was not covered by tests
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/SelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class SelectFilter extends Filter
{
public array $options = [];

public string $viewPath = 'livewire-tables::components.tools.filters.select';

public function options(array $options = []): SelectFilter
{
$this->options = $options;
Expand Down Expand Up @@ -63,6 +65,6 @@ public function getFilterDefaultValue(): ?string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.select', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
4 changes: 3 additions & 1 deletion src/Views/Filters/TextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class TextFilter extends Filter
{
public string $viewPath = 'livewire-tables::components.tools.filters.text-field';

public function validate(string $value): string|bool
{
if ($this->hasConfig('maxlength')) {
Expand All @@ -30,6 +32,6 @@ public function getFilterDefaultValue(): ?string

public function render(): string|\Illuminate\Contracts\Foundation\Application|\Illuminate\View\View|\Illuminate\View\Factory
{
return view('livewire-tables::components.tools.filters.text-field', $this->getFilterDisplayData());
return view($this->getViewPath(), $this->getFilterDisplayData());
}
}
7 changes: 7 additions & 0 deletions src/Views/Traits/Configuration/FilterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,11 @@ public function setGenericDisplayData(array $genericDisplayData = []): self

return $this;
}

public function setCustomView(string $customView): self
{
$this->viewPath = $customView;

return $this;
}
}
5 changes: 5 additions & 0 deletions src/Views/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,9 @@ public function getFilterDisplayData(): array
{
return array_merge($this->getGenericDisplayData(), ['filter' => $this]);
}

public function getViewPath(): string
{
return $this->viewPath;
}
}
Loading

0 comments on commit 03a19f0

Please sign in to comment.