Skip to content

Commit 6531d45

Browse files
committed
feat: add perPage value to perPageValues if not present
1 parent dbe0a72 commit 6531d45

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Components/SetUp/Footer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ final class Footer implements Wireable
2424

2525
public function showPerPage(int $perPage = 10, array $perPageValues = [10, 25, 50, 100, 0]): Footer
2626
{
27+
if (! in_array($perPage, $perPageValues, true)) {
28+
$hasZero = in_array(0, $perPageValues, true);
29+
30+
if ($hasZero) {
31+
$perPageValues = array_filter($perPageValues);
32+
}
33+
34+
$perPageValues[] = $perPage;
35+
sort($perPageValues);
36+
37+
if ($hasZero) {
38+
$perPageValues[] = 0;
39+
}
40+
}
41+
2742
$this->perPage = $perPage;
2843
$this->perPageValues = $perPageValues;
2944

tests/Feature/FooterTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use PowerComponents\LivewirePowerGrid\Components\SetUp\Footer;
4+
5+
/*it('sets default perPage values', function () {
6+
$footer = new Footer();
7+
8+
expect($footer->perPage)->toBe(10)
9+
->and($footer->perPageValues)->toBe([10, 25, 50, 100, 0]);
10+
});*/
11+
12+
it('adds custom perPage value when not in default array', function () {
13+
$footer = new Footer();
14+
$result = $footer->showPerPage(30);
15+
16+
expect($result->perPage)->toBe(30)
17+
->and($result->perPageValues)->toBe([10, 25, 30, 50, 100, 0]);
18+
});
19+
20+
it('accepts custom perPageValues', function () {
21+
$footer = new Footer();
22+
$perPageValues = [5, 15, 30, 50];
23+
$result = $footer->showPerPage(15, $perPageValues);
24+
25+
expect($result->perPage)->toBe(15)
26+
->and($result->perPageValues)->toBe([5, 15, 30, 50]);
27+
});
28+
29+
it('adds custom perPage value when not in custom perPageValues without zero', function () {
30+
$footer = new Footer();
31+
$perPageValues = [5, 15, 50];
32+
$result = $footer->showPerPage(30, $perPageValues);
33+
34+
expect($result->perPage)->toBe(30)
35+
->and($result->perPageValues)->toBe([5, 15, 30, 50]);
36+
});
37+
38+
it('adds custom perPage value when not in custom perPageValues with zero', function () {
39+
$footer = new Footer();
40+
$perPageValues = [5, 15, 50, 0];
41+
$result = $footer->showPerPage(30, $perPageValues);
42+
43+
expect($result->perPage)->toBe(30)
44+
->and($result->perPageValues)->toBe([5, 15, 30, 50, 0]);
45+
});

0 commit comments

Comments
 (0)