Skip to content

Commit

Permalink
setFilterDefaultValue, Theming Optimization, TypeHint (PHP7.4) Fixes (#…
Browse files Browse the repository at this point in the history
…1191)

* Extra semicolon in example PHP (#1177) (#60)

* fixed multiSelectDropdownFilter in menus (#1160)

deleted a </div> at the end of file

* Extra semicolon in example PHP

* Moving setTheme and adding setFilterDefaultValue

* Fix Setting of Filter Defaults - Allow QueryString

* Add resetFilter to mountFilterHelpers()

* Test Fixes

* Tests for Filter Default Value

* Add L7.4 test back in

* PHP7.4 Test Fixes

* Remove mixed PHP7.4 Typehint

* Remove TypeHint from getFilterDefaultValue()

* Migrate getFilterDefaultValue() into Filters

* Update FIlterHelpersTest - Array vs Text

* PHP7.4 Adjustment

* Update Requirements Doc

* CHANGELOG Updates

---------

Co-authored-by: Chris Thompson <christhompsontldr@gmail.com>
Co-authored-by: Anthony Rappa <rappa819@gmail.com>
Co-authored-by: SamanthaBainCornu <104172362+SamanthaBainCornu@users.noreply.github.com>
Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
5 people committed May 10, 2023
1 parent 2222e06 commit f1f7ac7
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 13 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-20.04]
php: [8.0, 8.1, 8.2]
php: [7.4, 8.0, 8.1, 8.2]
laravel: [8, 9, 10]
stability: [prefer-dist]
exclude:
- laravel: 10
php: 8.0
- laravel: 10
php: 7.4
- laravel: 9
php: 7.4

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
### Changed
- General
- Add capability for passing a custom model path to the MakeCommand
- Add setFilterDefaultValue() on a per-component basis
- Add getFilterDefaultValue() to each Filter Component (to maintain support for PHP7.4 - variable return types)
- Moved Setting of Filter Defaults to Traits/Helpers/FilterHelpers - mountFilterHelpers
- Moved Setting of Theme to Traits/ComponentUtilities - mountComponentUtilities for efficiency
- Fix for TypeHint to allow continued support of PHP 7.4

- Filters
- *Fix* - Changed the booting order to prevent repeated calling of filters() - https://github.com/rappasoft/laravel-livewire-tables/pull/1166
- *Fix* - fixed multiSelectDropdownFilter in menus - https://github.com/rappasoft/laravel-livewire-tables/pull/1160

- Documentation
- *Fix* - Minor wording tweak to documentation - https://github.com/rappasoft/laravel-livewire-tables/pull/1139
- *Fix* - Fix to example in applying-filters

- Tests
- Restore PHP 7.4 tests for L8 only (includes minor tweaks)
- Remove duplicate tests in ComponentHelpersTest
- Tweak to ReorderingVisualsTest
- Add TextFilter as a test in FilterHelpersTest


## [2.12.0] - 2023-04-08

Expand Down
4 changes: 2 additions & 2 deletions docs/start/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ weight: 1

The following are required to use this package regardless of what theme you choose:

- PHP 7.4+
- [Laravel 8.x](https://laravel.com)
- PHP 7.4+, 8.0+
- [Laravel 8.x, 9.x, 10.x](https://laravel.com)
- [Laravel Livewire 2.x](https://laravel-livewire.com)
- [Alpine.js 3.x](https://alpinejs.dev)
6 changes: 4 additions & 2 deletions src/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ public function booted(): void
$this->configure();

// Set the filter defaults based on the filter type
$this->setFilterDefaults();
// Moved to Traits/Helpers/FilterHelpers - mountFilterHelpers
//$this->setFilterDefaults();

// Sets the Theme - tailwind/bootstrap
$this->setTheme();
// Moved to Traits/ComponentUtilities - mountComponentUtilities
//$this->setTheme();

//Sets up the Builder Instance
$this->setBuilder($this->builder());
Expand Down
10 changes: 10 additions & 0 deletions src/Traits/ComponentUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,14 @@ public function hydrate(): void
{
$this->restartReorderingIfNecessary();
}

// Sets the Theme If Not Already Set
public function mountComponentUtilities()
{
// Sets the Theme - tailwind/bootstrap
if (is_null($this->theme)) {
$this->setTheme();
}

}
}
19 changes: 19 additions & 0 deletions src/Traits/Helpers/FilterHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,23 @@ public function getFiltersByRow(): array

return $orderedFilters;
}

/**
* Sets Filter Default Values
*/
public function mountFilterHelpers()
{
$appliedFilters = $this->getAppliedFiltersWithValues();
foreach ($this->getFilters() as $filter) {
if (! isset($appliedFilters[$filter->getKey()])) {
if ($filter->hasFilterDefaultValue()) {
$this->setFilter($filter->getKey(), $filter->getFilterDefaultValue());
} else {
$this->resetFilter($filter);
}
} else {
$this->setFilter($filter->getKey(), $appliedFilters[$filter->getKey()]);
}
}
}
}
3 changes: 2 additions & 1 deletion src/Views/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ abstract class Filter
protected ?int $filterSlidedownRow = null;
protected ?int $filterSlidedownColspan = null;
protected ?string $filterCustomPillBlade = null;

protected $filterDefaultValue;

public function __construct(string $name, string $key = null)
{
$this->name = $name;
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public function isEmpty($value): bool
return $value === '';
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return string|null
*/
public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function render(DataTableComponent $component)
{
return view('livewire-tables::components.tools.filters.date', [
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/DateTimeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public function isEmpty($value): bool
return $value === '';
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return string|null
*/
public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function render(DataTableComponent $component)
{
return view('livewire-tables::components.tools.filters.datetime', [
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/MultiSelectDropdownFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function getDefaultValue()
return [];
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return array<mixed>
*/
public function getFilterDefaultValue(): array
{
return $this->filterDefaultValue ?? [];
}

public function getFilterPillValue($value): ?string
{
$values = [];
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/MultiSelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function getDefaultValue()
return [];
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return array<mixed>
*/
public function getFilterDefaultValue(): array
{
return $this->filterDefaultValue ?? [];
}

public function getFilterPillValue($value): ?string
{
$values = [];
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public function isEmpty($value): bool
return $value === '';
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return string|null
*/
public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function render(DataTableComponent $component)
{
return view('livewire-tables::components.tools.filters.number', [
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/SelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public function isEmpty($value): bool
return $value === '';
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return string|null
*/
public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function render(DataTableComponent $component)
{
return view('livewire-tables::components.tools.filters.select', [
Expand Down
10 changes: 10 additions & 0 deletions src/Views/Filters/TextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public function isEmpty($value): bool
return $value === '';
}

/**
* Gets the Default Value for this Filter via the Component
*
* @return string|null
*/
public function getFilterDefaultValue(): ?string
{
return $this->filterDefaultValue ?? null;
}

public function render(DataTableComponent $component)
{
return view('livewire-tables::components.tools.filters.text-field', [
Expand Down
14 changes: 14 additions & 0 deletions src/Views/Traits/Configuration/FilterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,18 @@ public function setFilterPillBlade(string $blade): self

return $this;
}

/**
* Sets a Default Value via the Filter Component
*
* @param mixed $value
*
* @return self
*/
public function setFilterDefaultValue($value): self
{
$this->filterDefaultValue = $value;

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 @@ -311,4 +311,14 @@ public function getCustomPillBlade(): ?string
{
return $this->filterCustomPillBlade;
}

/**
* Determines if the Filter has a Default Value via the Component
*
* @return bool
*/
public function hasFilterDefaultValue(): bool
{
return ! is_null($this->filterDefaultValue);
}
}
5 changes: 4 additions & 1 deletion tests/DataTableComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function default_fingerprint_will_always_be_the_same_for_same_datatable()
$this->basicTable->getDataTableFingerprint(),
]
);
$this->assertSame($this->basicTable->getDataTableFingerprint(), $this->defaultFingerprintingAlgo($this->basicTable::class));
// Changed due to PHP 7.4
$this->assertSame($this->basicTable->getDataTableFingerprint(), $this->defaultFingerprintingAlgo(PetsTable::class));


}

/** @test */
Expand Down
10 changes: 5 additions & 5 deletions tests/Traits/Helpers/ComponentHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ public function can_get_hide_configurable_areas_when_reordering_status(): void
$this->assertFalse($this->basicTable->hideConfigurableAreasWhenReorderingIsDisabled());
}

/** @test */
public function can_get_dataTable_fingerprint(): void
{
$this->assertSame($this->defaultFingerPrintingAlgo($this->basicTable::class), $this->basicTable->getDataTableFingerprint());
}
// Exists in DataTableComponentTest
// public function can_get_dataTable_fingerprint(): void
//{
// $this->assertSame($this->defaultFingerPrintingAlgo($this->basicTable::class), $this->basicTable->getDataTableFingerprint());
// }

/** @test */
public function can_get_query_string_alias_and_it_will_be_the_same_as_table_name_by_default(): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Traits/Visuals/ReorderingVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function search_hides_on_reorder(array $filterDefaultArray): void
->assertDontSee('Search')
->call('disableReordering')
->assertSet('searchStatus', true)
->assertSet('table', ['search' => 'abc123', 'filters' => $filterDefaultArray])
->assertSet('table', ['search' => 'abc123'])
->assertSee('Search');
}

Expand Down Expand Up @@ -292,6 +292,7 @@ public function filters_are_disabled_on_reorder(array $filterDefaultArray): void
->assertDontSeeHtml('Filters')
->call('disableReordering')
->assertSet('filtersStatus', true)
->set('table.filters.breed', [])
->assertSet('table', ['filters' => $filterDefaultArray, 'sorts' => [], 'columns' => []])
->assertSeeHtml('Filters');
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Views/Traits/Helpers/FilterHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectDropdownFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;

class FilterHelpersTest extends TestCase
{
Expand Down Expand Up @@ -310,4 +311,36 @@ public function can_get_filter_slidedown_colspan(): void

$this->assertSame(3, $filter->getFilterSlidedownColspan());
}

/** @test */
public function can_get_filter_default_value_component_level_array(): void
{
$filter = MultiSelectFilter::make('Active')->options(['foo' => 'bar', 'lorem' => 'ipsum'])->setFilterDefaultValue(['lorem']);
$this->assertSame(['lorem'], $filter->getFilterDefaultValue());
}

/** @test */
public function can_get_filter_has_default_value_component_level_array(): void
{
$filter = MultiSelectFilter::make('Active')->options(['foo' => 'bar', 'lorem' => 'ipsum']);
$this->assertFalse($filter->hasFilterDefaultValue());
$filter->setFilterDefaultValue(['foo']);
$this->assertTrue($filter->hasFilterDefaultValue());
}

/** @test */
public function can_get_filter_default_value_component_level_text(): void
{
$filter = TextFilter::make('Active')->setFilterDefaultValue('lorem');
$this->assertSame('lorem', $filter->getFilterDefaultValue());
}

/** @test */
public function can_get_filter_has_default_value_component_level_text(): void
{
$filter = TextFilter::make('Active');
$this->assertFalse($filter->hasFilterDefaultValue());
$filter->setFilterDefaultValue('foo');
$this->assertTrue($filter->hasFilterDefaultValue());
}
}

0 comments on commit f1f7ac7

Please sign in to comment.