Skip to content

Commit a9afdd8

Browse files
authored
FixSetDefaultPerPage (rappasoft#2067)
* FixSetDefaultPerPage * Update getDefaultPerPage to respect getPerPageAccepted * Fix missing ) * Fix styling * Add test fix * Add final tests --------- Co-authored-by: lrljoe <[email protected]>
1 parent eec8385 commit a9afdd8

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

src/Traits/Configuration/PaginationConfiguration.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ public function setPerPage(int $perPage): self
107107
return $this;
108108
}
109109

110+
public function unsetPerPage(): self
111+
{
112+
$this->perPage = null;
113+
114+
return $this;
115+
}
116+
110117
public function setPaginationMethod(string $paginationMethod): self
111118
{
112119
$this->paginationMethod = $paginationMethod;
@@ -138,12 +145,10 @@ public function setDisplayPaginationDetailsDisabled(): self
138145
/**
139146
* Set a default per-page value (if not set already by session or querystring)
140147
*/
141-
public function setDefaultPerPage(int $perPage): self
148+
public function setDefaultPerPage(int $defaultPerPage): self
142149
{
143-
$defaultPerPage = $perPage;
144-
145-
if ($this->perPage == 10) {
146-
$this->setPerPage($perPage);
150+
if (in_array((int) $defaultPerPage, $this->getPerPageAccepted())) {
151+
$this->defaultPerPage = $defaultPerPage;
147152
}
148153

149154
return $this;

src/Traits/Helpers/PaginationHelpers.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ public function getComputedPageName(): string
6969

7070
public function getPerPage(): int
7171
{
72-
return $this->perPage;
72+
return $this->perPage ?? $this->getDefaultPerPage();
73+
}
74+
75+
public function getDefaultPerPage(): int
76+
{
77+
return in_array((int) $this->defaultPerPage, $this->getPerPageAccepted()) ? $this->defaultPerPage : ($this->getPerPageAccepted()[0] ?? 10);
7378
}
7479

7580
/**
@@ -128,7 +133,7 @@ public function setupPagination(): void
128133
if (in_array(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()), $this->getPerPageAccepted(), true)) {
129134
$this->setPerPage(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()));
130135
} else {
131-
$this->setPerPage($this->getPerPageAccepted()[0] ?? 10);
136+
$this->setPerPage($this->getDefaultPerPage());
132137
}
133138
}
134139

src/Traits/WithPagination.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ trait WithPagination
1515

1616
public ?string $pageName = null;
1717

18-
public int $perPage = 10;
18+
public ?int $perPage;
19+
20+
#[Locked]
21+
public int $defaultPerPage = 10;
1922

2023
#[Locked]
2124
public array $perPageAccepted = [10, 25, 50];
@@ -57,9 +60,9 @@ trait WithPagination
5760

5861
public function mountWithPagination(): void
5962
{
60-
$sessionPerPage = session()->get($this->getPerPagePaginationSessionKey(), $this->getPerPageAccepted()[0] ?? 10);
63+
$sessionPerPage = session()->get($this->getPerPagePaginationSessionKey(), $this->getPerPage());
6164
if (! in_array((int) $sessionPerPage, $this->getPerPageAccepted(), false)) {
62-
$sessionPerPage = $this->getPerPageAccepted()[0] ?? 10;
65+
$sessionPerPage = $this->getDefaultPerPage();
6366
}
6467
$this->setPerPage($sessionPerPage);
6568
}
@@ -68,7 +71,7 @@ public function mountWithPagination(): void
6871
public function updatedPerPage(int|string $value): void
6972
{
7073
if (! in_array((int) $value, $this->getPerPageAccepted(), false)) {
71-
$value = $this->getPerPageAccepted()[0] ?? 10;
74+
$value = $this->getDefaultPerPage();
7275
}
7376

7477
if (in_array(session($this->getPerPagePaginationSessionKey(), (int) $value), $this->getPerPageAccepted(), true)) {

tests/Unit/Traits/Configuration/PaginationConfigurationTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,18 @@ public function test_can_set_per_page_manually(): void
113113

114114
public function test_can_set_default_per_page(): void
115115
{
116-
$this->assertSame(10, $this->unpaginatedTable->getPerPage());
117-
$this->unpaginatedTable->setDefaultPerPage(50);
118-
$this->assertSame(50, $this->unpaginatedTable->getPerPage());
119-
$this->unpaginatedTable->perPage = 25;
120-
$this->assertSame(25, $this->unpaginatedTable->getPerPage());
116+
$this->assertSame(10, $this->basicTable->getPerPage());
117+
$this->basicTable->unsetPerPage();
118+
$this->basicTable->setDefaultPerPage(50);
119+
$this->assertSame(50, $this->basicTable->getDefaultPerPage());
120+
$this->assertSame(50, $this->basicTable->getPerPage());
121+
$this->basicTable->perPage = 25;
122+
$this->assertSame(25, $this->basicTable->getPerPage());
123+
$this->basicTable->setPerPage(10);
124+
$this->assertSame(10, $this->basicTable->getPerPage());
125+
$this->assertSame(50, $this->basicTable->getDefaultPerPage());
126+
$this->basicTable->unsetPerPage();
127+
$this->assertSame(50, $this->basicTable->getPerPage());
128+
121129
}
122130
}

0 commit comments

Comments
 (0)