Skip to content

Commit

Permalink
Development to Master for v3.5.3 (#2068)
Browse files Browse the repository at this point in the history
Co-authored-by: lrljoe <[email protected]>

* Remove Waiting For Tests

* Use CODECOV_TOKEN

* Fix styling

* Update phpdoc for boot (#2055)

* Update phpdoc for boot

* Fix styling

---------

Co-authored-by: lrljoe <[email protected]>

* Add test for No Columns defined - throws correct Exception (#2057)

* Add test for No Columns

* Fix styling

---------

Co-authored-by: lrljoe <[email protected]>

* Add Simple updatedSearch tests (#2058)

* Add Simple updatedSearch tests

* Fix styling

---------

Co-authored-by: lrljoe <[email protected]>

* Add test for FilterApplied Event being dispatched (#2059)

* Add test for FilterApplied being dispatched

* Fix styling

---------

Co-authored-by: lrljoe <[email protected]>

* Add updatedSelectedColumns test for Event (#2060)

* Add test for ColumnsSelected Event dispatch


* Apply Separate Tests

---------

Co-authored-by: lrljoe <[email protected]>

* Adjustment for DateRangeFilter (#2064)

* Adjust SessionStorageHelpersTest (#2065)

* FixSetDefaultPerPage (#2067)

* FixSetDefaultPerPage

* Update getDefaultPerPage to respect getPerPageAccepted

* Fix missing )

* Fix styling

* Add test fix

* Add final tests

---------

Co-authored-by: lrljoe <[email protected]>

* Fix BooleanColumn unexpected truthy behaviour (#2066)

* Fix BooleanColumn unexpected truthy behaviour

* Support previous iteration

* Update ChangeLog

---------
  • Loading branch information
lrljoe authored Nov 18, 2024
1 parent 393013b commit 5cb0c87
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 37 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

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

## [v3.5.3] - 2024-11-18
## Bug Fixes
- FixSetDefaultPerPage by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2067
- Fix BooleanColumn unexpected truthy behaviour by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2066
- Adjustment for DateRangeFilter by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2064

## Testing
- Adjust SessionStorageHelpersTest by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2065
- Add updatedSelectedColumns test for Event by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2060
- Add test for FilterApplied Event being dispatched by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2059
- Add Simple updatedSearch tests by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2058
- Add test for No Columns defined - throws correct Exception by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2057

## [v3.5.2] - 2024-11-09
## Bug Fixes
- Migrate Localisation back to PHP Files from JSON by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/2038
Expand Down
43 changes: 41 additions & 2 deletions resources/js/laravel-livewire-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ document.addEventListener('alpine:init', () => {

}
}));


Alpine.data('booleanFilter', (wire,filterKey,tableName,defaultValue) => ({
switchOn: false,
Expand All @@ -270,6 +269,47 @@ document.addEventListener('alpine:init', () => {
}
}));

Alpine.data('newBooleanFilter', (filterKey,tableName,defaultValue) => ({
switchOn: false,
value: false,
toggleStatus()
{
let tempValue = Boolean(Number(this.$wire.get('filterComponents.'+filterKey) ?? this.value));
let newBoolean = !tempValue;
this.switchOn = this.value = newBoolean;
return Number(newBoolean);
},
toggleStatusWithUpdate()
{
let newValue = this.toggleStatus();
this.$wire.set('filterComponents.'+filterKey, newValue);
},
toggleStatusWithReset()
{
let newValue = this.toggleStatus();
this.$wire.call('resetFilter',filterKey);
},
setSwitchOn(val)
{
let number = Number(val ?? 0);
this.switchOn = Boolean(number);
},
init() {
this.$nextTick(() => {
this.value = this.$wire.get('filterComponents.'+filterKey) ?? defaultValue;
this.setSwitchOn(this.value ?? 0);
});

this.listeners.push(
Livewire.on('filter-was-set', (detail) => {
if(detail.tableName == tableName && detail.filterKey == filterKey) {
this.switchOn = detail.value ?? defaultValue;
}
})
);
}
}));

Alpine.data('numberRangeFilter', (wire, filterKey, parentElementPath, filterConfig, childElementRoot) => ({
allFilters: wire.entangle('filterComponents', false),
originalMin: 0,
Expand Down Expand Up @@ -407,7 +447,6 @@ document.addEventListener('alpine:init', () => {

}));


Alpine.data('tableWrapper', (wire, showBulkActionsAlpine) => ({
shouldBeDisplayed: wire.entangle('shouldBeDisplayed'),
listeners: [],
Expand Down
2 changes: 1 addition & 1 deletion resources/js/laravel-livewire-tables.min.js

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions resources/views/components/tools/filters/boolean.blade.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
@php($defaultValue = ($filter->hasFilterDefaultValue() ? (bool) $filter->getFilterDefaultValue() : false))
<div class="flex flex-cols"
x-data="booleanFilter($wire,'{{ $filter->getKey() }}', '{{ $tableName }}', '{{ $defaultValue }}')"
x-data="newBooleanFilter('{{ $filter->getKey() }}', '{{ $tableName }}', '{{ $defaultValue }}')"
>
<x-livewire-tables::tools.filter-label :$filter :$filterLayout :$tableName :$isTailwind :$isBootstrap4 :$isBootstrap5 :$isBootstrap />
<input id="thisId" type="checkbox" name="switch" class="hidden" :checked="switchOn" >
<input id="thisId" type="checkbox" name="switch" class="hidden" :checked="value" />

<button id="{{ $tableName }}-filter-{{ $filter->getKey() }}"
x-ref="switchButton"
type="button"
@click="switchOn = ! switchOn; value = (switchOn ? '1' : '0')"
:class="switchOn ? 'bg-blue-600' : 'bg-neutral-200'"
@click="toggleStatusWithUpdate"
:class="(value == 1 || value == true) ? 'bg-blue-600' : 'bg-neutral-200'"
class="relative inline-flex h-6 py-0.5 ml-4 focus:outline-none rounded-full w-10"
x-cloak>
<span :class="switchOn ? 'translate-x-[18px]' : 'translate-x-0.5'" class="w-5 h-5 duration-200 ease-in-out bg-white rounded-full shadow-md"></span>
<span :class="(value == 1 || value == true) ? 'translate-x-[18px]' : 'translate-x-0.5'" class="w-5 h-5 duration-200 ease-in-out bg-white rounded-full shadow-md"></span>
</button>
<template x-if="value === '1' || value === '0'">
<button
x-on:click="switchOn = {{ $defaultValue }};"
wire:click="resetFilter('{{ $filter->getKey() }}')"
type="button"
<template x-if="(value == 1 || value == true)">
<button @click="toggleStatusWithReset" type="button"
class="flex-shrink-0 ml-1 h-6 w-6 rounded-full inline-flex items-center justify-center text-indigo-400 hover:bg-indigo-200 hover:text-indigo-500 focus:outline-none focus:bg-indigo-500 focus:text-white"
>

Expand Down
15 changes: 10 additions & 5 deletions src/Traits/Configuration/PaginationConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public function setPerPage(int $perPage): self
return $this;
}

public function unsetPerPage(): self
{
$this->perPage = null;

return $this;
}

public function setPaginationMethod(string $paginationMethod): self
{
$this->paginationMethod = $paginationMethod;
Expand Down Expand Up @@ -138,12 +145,10 @@ public function setDisplayPaginationDetailsDisabled(): self
/**
* Set a default per-page value (if not set already by session or querystring)
*/
public function setDefaultPerPage(int $perPage): self
public function setDefaultPerPage(int $defaultPerPage): self
{
$defaultPerPage = $perPage;

if ($this->perPage == 10) {
$this->setPerPage($perPage);
if (in_array((int) $defaultPerPage, $this->getPerPageAccepted())) {
$this->defaultPerPage = $defaultPerPage;
}

return $this;
Expand Down
9 changes: 7 additions & 2 deletions src/Traits/Helpers/PaginationHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ public function getComputedPageName(): string

public function getPerPage(): int
{
return $this->perPage;
return $this->perPage ?? $this->getDefaultPerPage();
}

public function getDefaultPerPage(): int
{
return in_array((int) $this->defaultPerPage, $this->getPerPageAccepted()) ? $this->defaultPerPage : ($this->getPerPageAccepted()[0] ?? 10);
}

/**
Expand Down Expand Up @@ -128,7 +133,7 @@ public function setupPagination(): void
if (in_array(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()), $this->getPerPageAccepted(), true)) {
$this->setPerPage(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()));
} else {
$this->setPerPage($this->getPerPageAccepted()[0] ?? 10);
$this->setPerPage($this->getDefaultPerPage());
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Traits/WithPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ trait WithPagination

public ?string $pageName = null;

public int $perPage = 10;
public ?int $perPage;

#[Locked]
public int $defaultPerPage = 10;

#[Locked]
public array $perPageAccepted = [10, 25, 50];
Expand Down Expand Up @@ -57,9 +60,9 @@ trait WithPagination

public function mountWithPagination(): void
{
$sessionPerPage = session()->get($this->getPerPagePaginationSessionKey(), $this->getPerPageAccepted()[0] ?? 10);
$sessionPerPage = session()->get($this->getPerPagePaginationSessionKey(), $this->getPerPage());
if (! in_array((int) $sessionPerPage, $this->getPerPageAccepted(), false)) {
$sessionPerPage = $this->getPerPageAccepted()[0] ?? 10;
$sessionPerPage = $this->getDefaultPerPage();
}
$this->setPerPage($sessionPerPage);
}
Expand All @@ -68,7 +71,7 @@ public function mountWithPagination(): void
public function updatedPerPage(int|string $value): void
{
if (! in_array((int) $value, $this->getPerPageAccepted(), false)) {
$value = $this->getPerPageAccepted()[0] ?? 10;
$value = $this->getDefaultPerPage();
}

if (in_array(session($this->getPerPagePaginationSessionKey(), (int) $value), $this->getPerPageAccepted(), true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getKeys(): array
return ['minDate' => '', 'maxDate' => ''];
}

public function validate(array|string $values): array|bool
public function validate(array|string|null $values): array|bool
{
$this->getOptions();
$this->getConfigs();
Expand Down Expand Up @@ -197,7 +197,7 @@ public function getFilterPillValue($value): array|string|bool|null
return '';
}

public function isEmpty(array|string $value): bool
public function isEmpty(array|string|null $value): bool
{
$values = [];
if (is_array($value)) {
Expand Down
18 changes: 13 additions & 5 deletions tests/Unit/Traits/Configuration/PaginationConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ public function test_can_set_per_page_manually(): void

public function test_can_set_default_per_page(): void
{
$this->assertSame(10, $this->unpaginatedTable->getPerPage());
$this->unpaginatedTable->setDefaultPerPage(50);
$this->assertSame(50, $this->unpaginatedTable->getPerPage());
$this->unpaginatedTable->perPage = 25;
$this->assertSame(25, $this->unpaginatedTable->getPerPage());
$this->assertSame(10, $this->basicTable->getPerPage());
$this->basicTable->unsetPerPage();
$this->basicTable->setDefaultPerPage(50);
$this->assertSame(50, $this->basicTable->getDefaultPerPage());
$this->assertSame(50, $this->basicTable->getPerPage());
$this->basicTable->perPage = 25;
$this->assertSame(25, $this->basicTable->getPerPage());
$this->basicTable->setPerPage(10);
$this->assertSame(10, $this->basicTable->getPerPage());
$this->assertSame(50, $this->basicTable->getDefaultPerPage());
$this->basicTable->unsetPerPage();
$this->assertSame(50, $this->basicTable->getPerPage());

}
}
25 changes: 25 additions & 0 deletions tests/Unit/Traits/Helpers/ColumnHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Traits\Helpers;

use Rappasoft\LaravelLivewireTables\Exceptions\NoColumnsException;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
use Rappasoft\LaravelLivewireTables\Views\Column;
Expand Down Expand Up @@ -360,4 +362,27 @@ public function test_can_check_if_column_label_has_attributes(): void
$this->assertSame(['class' => 'text-xl', 'default' => true, 'default-colors' => false, 'default-styling' => false], $column->getLabelAttributes());

}

public function test_throws_error_if_no_columns_are_defined(): void
{
$this->expectException(NoColumnsException::class);

$testTable = new class extends PetsTable
{
public function columns(): array
{
return [];
}
};

$testTable->configure();
$testTable->boot();
$testTable->bootedComponentUtilities();
$testTable->bootedWithData();
$testTable->bootedWithColumns();
$testTable->bootedWithColumnSelect();
$testTable->bootedWithSecondaryHeader();
$testTable->booted();

}
}
98 changes: 98 additions & 0 deletions tests/Unit/Traits/Helpers/ColumnSelectHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Traits\Helpers;

use Illuminate\Support\Facades\Event;
use Rappasoft\LaravelLivewireTables\Events\ColumnsSelected;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class ColumnSelectHelpersTest extends TestCase
Expand Down Expand Up @@ -103,4 +106,99 @@ public function test_get_currently_selected_cols_works(): void
$this->assertSame(8, count($this->basicTable->getDefaultVisibleColumns()));

}

public function test_event_fires_for_columnselect(): void
{
Event::fake();

$testTable = new class extends PetsTable
{
public function configure(): void
{
parent::configure();
}
};
$testTable->configure();
$testTable->boot();
$testTable->bootedComponentUtilities();
$testTable->bootedWithData();
$testTable->bootedWithColumns();
$testTable->bootedWithColumnSelect();
$testTable->bootedWithSecondaryHeader();
$testTable->booted();

$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'], $testTable->selectedColumns);

$testTable->disableColumnSelectEvent();
$testTable->selectedColumns = ['id', 'sort', 'name', 'age', 'breed', 'other', 'rowimg'];
$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'rowimg'], $testTable->selectedColumns);
$testTable->updatedSelectedColumns();
Event::assertNotDispatched(ColumnsSelected::class);

$testTable->enableColumnSelectEvent();

$testTable->selectedColumns = ['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'];
$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'], $testTable->selectedColumns);
$testTable->updatedSelectedColumns();
Event::assertDispatched(ColumnsSelected::class);

}

public function test_event_does_fire_for_columnselect(): void
{
Event::fake();

$testTable = new class extends PetsTable
{
public function configure(): void
{
parent::configure();
$this->enableColumnSelectEvent();
}
};
$testTable->configure();
$testTable->boot();
$testTable->bootedComponentUtilities();
$testTable->bootedWithData();
$testTable->bootedWithColumns();
$testTable->bootedWithColumnSelect();
$testTable->bootedWithSecondaryHeader();
$testTable->booted();

$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'], $testTable->selectedColumns);

$testTable->selectedColumns = ['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'];
$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'], $testTable->selectedColumns);
$testTable->updatedSelectedColumns();
Event::assertDispatched(ColumnsSelected::class);

}

public function test_event_does_not_fire_for_columnselect(): void
{
Event::fake();

$testTable = new class extends PetsTable
{
public function configure(): void
{
parent::configure();
$this->disableColumnSelectEvent();
}
};
$testTable->configure();
$testTable->boot();
$testTable->bootedComponentUtilities();
$testTable->bootedWithData();
$testTable->bootedWithColumns();
$testTable->bootedWithColumnSelect();
$testTable->bootedWithSecondaryHeader();
$testTable->booted();

$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'link', 'rowimg'], $testTable->selectedColumns);
$testTable->selectedColumns = ['id', 'sort', 'name', 'age', 'breed', 'other', 'rowimg'];
$this->assertSame(['id', 'sort', 'name', 'age', 'breed', 'other', 'rowimg'], $testTable->selectedColumns);
$testTable->updatedSelectedColumns();
Event::assertNotDispatched(ColumnsSelected::class);
}
}
Loading

0 comments on commit 5cb0c87

Please sign in to comment.