Skip to content

Commit

Permalink
Fix Grid IPP selector (#2017)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
  • Loading branch information
mkrecek234 and mvorisek committed Jun 19, 2023
1 parent a78b335 commit c093efc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
7 changes: 0 additions & 7 deletions .php-cs-fixer.dist.php
Expand Up @@ -53,13 +53,6 @@
// fn => without curly brackets is less readable,
// also prevent bounding of unwanted variables for GC
'use_arrow_functions' => false,

// TODO remove once 3.17.1 is released
// https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/6979
// https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7011
// fixes were merged
'phpdoc_types' => ['groups' => ['simple', 'meta']],
'phpdoc_var_without_name' => false,
])
->setFinder($finder)
->setCacheFile(sys_get_temp_dir() . '/php-cs-fixer.' . md5(__DIR__) . '.cache');
2 changes: 1 addition & 1 deletion demos/collection/grid.php
Expand Up @@ -79,4 +79,4 @@
));

// Setting ipp with an array will add an ItemPerPageSelector to paginator.
$grid->setIpp([10, 25, 50, 100]);
$grid->setIpp([10, 100, 1000]);
7 changes: 7 additions & 0 deletions src/Behat/Context.php
Expand Up @@ -231,6 +231,13 @@ function ($matches) {
$selector
);

// add NBSP support for normalize-space() xpath function
$xpath = preg_replace(
'~(?<![\w\-])normalize-space\([^()\'"]*\)~',
'normalize-space(translate($0, \'' . "\u{00a0}" . '\', \' \'))',
$xpath
);

return ['xpath', $xpath];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Grid.php
Expand Up @@ -206,7 +206,7 @@ public function setIpp($ipp, $label = 'Items per page:'): void
*
* @return $this
*/
public function addItemsPerPageSelector(array $items = [10, 25, 50, 100], $label = 'Items per page:')
public function addItemsPerPageSelector(array $items = [10, 100, 1000], $label = 'Items per page:')
{
$ipp = (int) $this->container->stickyGet('ipp');
if ($ipp) {
Expand Down
24 changes: 12 additions & 12 deletions src/ItemsPerPageSelector.php
Expand Up @@ -4,6 +4,7 @@

namespace Atk4\Ui;

use Atk4\Data\Field;
use Atk4\Ui\Js\JsExpression;

/**
Expand All @@ -16,14 +17,9 @@ class ItemsPerPageSelector extends View
public $ui = 'selection compact dropdown';

/** @var list<int> Default page length menu items. */
public $pageLengthItems = [10, 25, 50, 100];
public $pageLengthItems = [10, 100, 1000];

/**
* Default button label.
* - [ipp] will be replace by the number of pages selected.
*
* @var string
*/
/** @var string */
public $label = 'Items per page:';

/** @var int The current number of item per page. */
Expand All @@ -32,6 +28,11 @@ class ItemsPerPageSelector extends View
/** @var Callback|null The callback function. */
public $cb;

private function formatInteger(int $value): string
{
return $this->getApp()->uiPersistence->typecastSaveField(new Field(['type' => 'integer']), $value);
}

protected function init(): void
{
parent::init();
Expand All @@ -45,7 +46,7 @@ protected function init(): void
if (!$this->currentIpp) {
$this->currentIpp = $this->pageLengthItems[0];
}
$this->set((string) $this->currentIpp);
$this->set($this->formatInteger($this->currentIpp));
}

/**
Expand All @@ -59,8 +60,7 @@ public function onPageLengthSelect(\Closure $fx): void
{
$this->cb->set(function () use ($fx) {
$ipp = isset($_GET['ipp']) ? (int) $_GET['ipp'] : null;
// $this->pageLength->set(preg_replace("/\[ipp\]/", $ipp, $this->label));
$this->set($ipp); // @phpstan-ignore-line TODO https://github.com/atk4/ui/issues/2016
$this->set($this->formatInteger($ipp));
$reload = $fx($ipp);
if ($reload) {
$this->getApp()->terminateJson($reload);
Expand All @@ -71,8 +71,8 @@ public function onPageLengthSelect(\Closure $fx): void
protected function renderView(): void
{
$menuItems = [];
foreach ($this->pageLengthItems as $key => $item) {
$menuItems[] = ['name' => $item, 'value' => $item];
foreach ($this->pageLengthItems as $item) {
$menuItems[] = ['name' => $this->formatInteger($item), 'value' => $item];
}

$function = new JsExpression('function (value, text, item) {
Expand Down
15 changes: 15 additions & 0 deletions tests-behat/grid.feature
Expand Up @@ -61,3 +61,18 @@ Feature: Grid
When I click using selector "//th.sortable[//div[text()='Name']]"
Then I should see "Andorra"
Then I should not see "Zambia"

Scenario: IPP selector
Then I should see "Andorra"
Then I should not see "China"
Then I should not see "Zambia"
When I click using selector "//div.ui.dropdown.compact"
When I click using selector "//div.ui.dropdown.compact//div.item[text()='100']"
Then I should see "Andorra"
Then I should see "China"
Then I should not see "Zambia"
When I click using selector "//div.ui.dropdown.compact"
When I click using selector "//div.ui.dropdown.compact//div.item[text()[normalize-space()='1 000']]"
Then I should see "Andorra"
Then I should see "China"
Then I should see "Zambia"

0 comments on commit c093efc

Please sign in to comment.