Skip to content

Commit 520ebe0

Browse files
authored
FixSetDefaultPerPage
1 parent ff38951 commit 520ebe0

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/Traits/Configuration/PaginationConfiguration.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ public function setDisplayPaginationDetailsDisabled(): self
138138
/**
139139
* Set a default per-page value (if not set already by session or querystring)
140140
*/
141-
public function setDefaultPerPage(int $perPage): self
141+
public function setDefaultPerPage(int $defaultPerPage): self
142142
{
143-
$defaultPerPage = $perPage;
144-
145-
if ($this->perPage == 10) {
146-
$this->setPerPage($perPage);
143+
if (in_array((int) $defaultPerPage, $this->getPerPageAccepted())) {
144+
$this->defaultPerPage = $defaultPerPage;
147145
}
148146

149147
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 $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)) {

0 commit comments

Comments
 (0)