Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-toggle CheckableItem #193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,7 @@ use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;

$callable = function (CliMenu $menu) {
$item = $menu->getSelectedItem();
$item->toggle();
$menu->redraw();
echo $menu->getSelectedItem()->getText();
};

$menu = (new CliMenuBuilder)
Expand Down
8 changes: 1 addition & 7 deletions examples/checkable-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\CheckableItem;

require_once(__DIR__ . '/../vendor/autoload.php');

$itemCallable = function (CliMenu $menu) {
/** @var CheckableItem $item */
$item = $menu->getSelectedItem();

$item->toggle();

$menu->redraw();
echo $menu->getSelectedItem()->getText();
};

$menu = (new CliMenuBuilder)
Expand Down
8 changes: 7 additions & 1 deletion src/MenuItem/CheckableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpSchool\CliMenu\MenuItem;

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Util\StringUtil;

Expand Down Expand Up @@ -49,7 +50,12 @@ public function __construct(
*/
public function getSelectAction() : ?callable
{
return $this->selectAction;
return function (CliMenu $cliMenu) {
$this->toggle();
$cliMenu->redraw();

return ($this->selectAction)($cliMenu);
};
}

/**
Expand Down
9 changes: 8 additions & 1 deletion test/MenuItem/CheckableItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpSchool\CliMenuTest\MenuItem;

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\CheckableItem;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\Terminal\Terminal;
Expand All @@ -19,9 +20,15 @@ public function testCanSelectIsTrue() : void
public function testGetSelectAction() : void
{
$callable = function () {
return 'callable is called';
};
$item = new CheckableItem('Item', $callable);
$this->assertSame($callable, $item->getSelectAction());

$cliMenu = $this->getMockBuilder(CLiMenu::class)
->disableOriginalConstructor()
->getMock();

$this->assertSame($callable(), $item->getSelectAction()($cliMenu));
}

public function testShowsItemExtra() : void
Expand Down