Skip to content

Commit

Permalink
Fix #203: Add ability to set default widget config for offset/keyset …
Browse files Browse the repository at this point in the history
…pagination (#206)

Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
samdark and vjik authored Sep 4, 2024
1 parent ae2112c commit b35f810
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ abstract class BaseListView extends Widget
private string $header = '';
private array $headerAttributes = [];
private string $layout = "{header}\n{toolbar}\n{items}\n{summary}\n{pager}";

private array $offsetPaginationConfig = [];
private array $keysetPaginationConfig = [];
private string|OffsetPagination|KeysetPagination|null $pagination = null;
protected ?ReadableDataInterface $dataReader = null;
private string $toolbar = '';
Expand Down Expand Up @@ -470,6 +473,32 @@ public function pagination(string|KeysetPagination|OffsetPagination|null $pagina
return $new;
}

/**
* Set configuration for offset pagination widget.
*
* @param array $config Widget config.
* @return $this
*/
public function offsetPaginationConfig(array $config): static
{
$new = clone $this;

Check warning on line 484 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function offsetPaginationConfig(array $config) : static { - $new = clone $this; + $new = $this; $new->offsetPaginationConfig = $config; return $new; }
$new->offsetPaginationConfig = $config;
return $new;
}

/**
* Set configuration for keyset pagination widget.
*
* @param array $config Widget config.
* @return $this
*/
public function keysetPaginationConfig(array $config): static
{
$new = clone $this;

Check warning on line 497 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function keysetPaginationConfig(array $config) : static { - $new = clone $this; + $new = $this; $new->keysetPaginationConfig = $config; return $new; }
$new->keysetPaginationConfig = $config;
return $new;
}

/**
* Returns a new instance with the paginator interface of the grid view, detail view, or list view.
*
Expand Down Expand Up @@ -630,9 +659,9 @@ private function renderPagination(): string

if ($this->pagination === null) {
if ($preparedDataReader instanceof OffsetPaginator) {
$pagination = OffsetPagination::widget();
$pagination = OffsetPagination::widget(config: $this->offsetPaginationConfig);
} elseif ($preparedDataReader instanceof KeysetPaginator) {
$pagination = KeysetPagination::widget();
$pagination = KeysetPagination::widget(config: $this->keysetPaginationConfig);
} else {
return '';
}
Expand Down
74 changes: 74 additions & 0 deletions tests/ListView/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,78 @@ public function testViewParams(): void
->render(),
);
}

public function testOffsetPaginationConfig(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<div>
<div class=text-success>1</div>
</div>
<div>Page <b>1</b> of <b>2</b></div>
<nav>
<ul class="pagination">
<li class="page-item disabled"><a class="page-link" href="#1">⟪</a></li>
<li class="page-item disabled"><a class="page-link" href="#1">⟨</a></li>
<li class="page-item active"><a class="page-link" href="#1">1</a></li>
<li class="page-item"><a class="page-link" href="#2">2</a></li>
<li class="page-item"><a class="page-link" href="#2">⟩</a></li>
<li class="page-item"><a class="page-link" href="#2">⟫</a></li>
</ul>
</nav>
</div>
HTML,
ListView::widget()
->itemView(dirname(__DIR__) . '/Support/view/_listviewparams.php')
->dataReader($this->createOffsetPaginator($this->data, 1))
->separator(PHP_EOL)
->viewParams(['itemClass' => 'text-success'])
->offsetPaginationConfig([
'listTag()' => ['ul'],
'listAttributes()' => [['class' => 'pagination']],
'itemTag()' => ['li'],
'itemAttributes()' => [['class' => 'page-item']],
'linkAttributes()' => [['class' => 'page-link']],
'currentItemClass()' => ['active'],
'disabledItemClass()' => ['disabled'],
])
->render(),
);
}

public function testKeysetPaginationConfig(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<div>
<div class=text-success>1</div>
</div>
<nav>
<ul class="pagination">
<li class="page-item disabled"><a class="page-link">⟨</a></li>
<li class="page-item"><a class="page-link" href="#1">⟩</a></li>
</ul>
</nav>
</div>
HTML,
ListView::widget()
->itemView(dirname(__DIR__) . '/Support/view/_listviewparams.php')
->dataReader($this->createKeysetPaginator($this->data, 1))
->separator(PHP_EOL)
->viewParams(['itemClass' => 'text-success'])
->keysetPaginationConfig([
'listTag()' => ['ul'],
'listAttributes()' => [['class' => 'pagination']],
'itemTag()' => ['li'],
'itemAttributes()' => [['class' => 'page-item']],
'linkAttributes()' => [['class' => 'page-link']],
'currentItemClass()' => ['active'],
'disabledItemClass()' => ['disabled'],
])
->render(),
);
}
}

0 comments on commit b35f810

Please sign in to comment.