Skip to content

Commit

Permalink
Add draft page selector
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Nov 8, 2024
1 parent 9401e7f commit b07995a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 17 deletions.
16 changes: 2 additions & 14 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

use function is_array;
use function is_string;
use function PHPUnit\Framework\returnArgument;

/**
* @psalm-type UrlArguments = array<string,scalar|Stringable|null>
Expand Down Expand Up @@ -690,20 +691,6 @@ protected function getDefaultPageSize(): int
return $this->defaultPageSize;
}

/**
* Get page size constraint.
*
* @return array|bool|int Page size constraint.
* - `true` - default only.
* - `false` - no constraint.
* - int - maximum page size.
* - [int, int, ...] - a list of page sizes to choose from.
*/
protected function getPageSizeConstraint(): array|int|bool
{
return $this->pageSizeConstraint;
}

/**
* Get a new instance with a page size constraint set.
*
Expand Down Expand Up @@ -757,6 +744,7 @@ private function renderPagination(): string
}

return $pagination
->pageSizeConstraint($this->pageSizeConstraint)
->defaultPageSize($this->getDefaultPageSize())
->urlConfig($this->urlConfig)
->render();
Expand Down
102 changes: 100 additions & 2 deletions src/BasePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Yiisoft\Yii\DataView;

use InvalidArgumentException;
use LogicException;
use Yiisoft\Data\Paginator\PageToken;
use Yiisoft\Data\Paginator\PaginatorInterface;
use Yiisoft\Html\Html;
use Yiisoft\Http\Method;
use Yiisoft\Widget\Widget;

use function call_user_func_array;
Expand All @@ -17,6 +19,15 @@
*/
abstract class BasePagination extends Widget
{
/**
* @var array|bool|int Page size constraint.
* - `true` - default only.
* - `false` - no constraint.
* - int - maximum page size.
* - [int, int, ...] - a list of page sizes to choose from.
*/
private bool|int|array $pageSizeConstraint = true;

/**
* @psalm-var UrlCreator|null
*/
Expand All @@ -25,6 +36,8 @@ abstract class BasePagination extends Widget

private int $defaultPageSize = PaginatorInterface::DEFAULT_PAGE_SIZE;



/**
* @psalm-var non-empty-string|null
*/
Expand Down Expand Up @@ -172,9 +185,41 @@ final public function render(): string
$result .= "\n" . Html::closeTag($this->containerTag);
}

$result .= $this->renderPageSize();

return $result;
}

private function renderPageSize(): string
{
$out = Html::openTag('nav', ['class' => 'pageSize']);

$form = Html::form($this->getPageSizeItem()->url, Method::GET);
$out .= $form->open();

if (is_int($this->pageSizeConstraint)) {
$out .= Html::textInput(
$this->urlConfig->getPageSizeParameterName(),
$this->getPaginator()->getPageSize(),
['class' => 'form-control']
);
} elseif (is_array($this->pageSizeConstraint)) {
$options = array_combine($this->pageSizeConstraint, $this->pageSizeConstraint);

Check failure on line 207 in src/BasePagination.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgumentTypeCoercion

src/BasePagination.php:207:38: MixedArgumentTypeCoercion: Argument 1 of array_combine expects array<array-key, array-key>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)

Check failure on line 207 in src/BasePagination.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedArgumentTypeCoercion

src/BasePagination.php:207:38: MixedArgumentTypeCoercion: Argument 1 of array_combine expects array<array-key, array-key>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)

Check failure on line 207 in src/BasePagination.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgumentTypeCoercion

src/BasePagination.php:207:38: MixedArgumentTypeCoercion: Argument 1 of array_combine expects array<array-key, array-key>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)
$out .= Html::select($this->urlConfig->getPageSizeParameterName())
->optionsData($options)

Check failure on line 209 in src/BasePagination.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgumentTypeCoercion

src/BasePagination.php:209:31: MixedArgumentTypeCoercion: Argument 1 of Yiisoft\Html\Tag\Select::optionsData expects array<array-key, array<array-key, string>|string>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)

Check failure on line 209 in src/BasePagination.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

MixedArgumentTypeCoercion

src/BasePagination.php:209:31: MixedArgumentTypeCoercion: Argument 1 of Yiisoft\Html\Tag\Select::optionsData expects array<array-key, array<array-key, string>|string>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)

Check failure on line 209 in src/BasePagination.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgumentTypeCoercion

src/BasePagination.php:209:31: MixedArgumentTypeCoercion: Argument 1 of Yiisoft\Html\Tag\Select::optionsData expects array<array-key, array<array-key, string>|string>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)
->value($this->getPaginator()->getPageSize());
} else {
return '';
}

$out .= Html::submitButton();
$out .= $form->close();

$out .= Html::closeTag('nav');

return $out;
}

/**
* @psalm-param UrlCreator|null $urlCreator
*/
Expand Down Expand Up @@ -202,15 +247,19 @@ public function urlConfig(UrlConfig $config): static
/**
* Creates the URL suitable for pagination with the specified page number. This method is mainly called by pagers
* when creating URLs used to perform pagination.
*
* @param PageToken $pageToken Token for the page.
* @param int|null $pageSize Page size to change to.
* @return string Created URL.
*/
protected function createUrl(PageToken $pageToken): string
protected function createUrl(PageToken $pageToken, ?int $pageSize = null): string
{
if ($this->urlCreator === null) {
return '#' . $pageToken->value;
}

$paginator = $this->getPaginator();
$pageSize = $paginator->getPageSize();
$pageSize = $pageSize ?? $paginator->getPageSize();
$sort = $paginator->getSort()?->getOrderAsString();

return call_user_func_array(
Expand All @@ -224,6 +273,55 @@ protected function createUrl(PageToken $pageToken): string
);
}

private function getPageSizeItem(): PaginationItem
{
$first = null;
foreach ($this->getItems() as $item) {
if ($first === null) {
$first = $item;
}
if ($item->isCurrent) {
return $item;
}
}
if ($first !== null) {
return $first;
}

throw new LogicException('Page size item is missing.');
}

/**
* Get page size constraint.
*
* @return array|bool|int Page size constraint.
* - `true` - default only.
* - `false` - no constraint.
* - int - maximum page size.
* - [int, int, ...] - a list of page sizes to choose from.
*/
public function getPageSizeConstraint(): array|int|bool
{
return $this->pageSizeConstraint;
}

/**
* Get a new instance with a page size constraint set.
*
* @param array|bool|int $pageSizeConstraint Page size constraint.
* `true` - default only.
* `false` - no constraint.
* int - maximum page size.
* [int, int, ...] - a list of page sizes to choose from.
* @return static New instance.
*/
public function pageSizeConstraint(array|int|bool $pageSizeConstraint): static
{
$new = clone $this;
$new->pageSizeConstraint = $pageSizeConstraint;
return $new;
}

/**
* @return PaginationItem[]
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/ListView/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public function testClosureForItemViewAttributesWithClosure(): void
public function testDefaultPageSizeConstraint(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Page size can not be changed');
$this->expectExceptionMessage('Page size can not be changed.');

$parameters = ['pagesize' => 10];

Expand Down

0 comments on commit b07995a

Please sign in to comment.