-
Notifications
You must be signed in to change notification settings - Fork 105
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
Feature request - ability to add sub-menu from addItem's callable #277
Comments
I guess you can already achieve this, maybe not so simple, let me take a look |
Is this what you mean? <?php
declare(strict_types=1);
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
require_once(__DIR__ . '/../vendor/autoload.php');
$itemCallable = function (CliMenu $menu) {
echo $menu->getSelectedItem()->getText();
};
$menu = (new CliMenuBuilder)
->setTitle('CLI Menu')
->addSubMenu('Options', function (CliMenuBuilder $b) {
$b->setTitle('CLI Menu > Options');
})
->addItem('Load Available Options', function (CliMenu $menu) {
$items = $menu->getItems();
//find options menu
/** @var MenuMenuItem $optionsMenu */
$optionsMenu = current(array_values(array_filter($items, function (MenuItemInterface $item) {
return $item instanceof MenuMenuItem && $item->getText() === 'Options';
})));
if ($optionsMenu === false) {
//menu not found
}
$optionsMenu->getSubMenu()->setItems([
new \PhpSchool\CliMenu\MenuItem\SelectableItem('Option 1', function (CliMenu $menu) {
echo $menu->getSelectedItem()->getText();
}),
new \PhpSchool\CliMenu\MenuItem\SelectableItem('Option 2', function (CliMenu $menu) {
echo $menu->getSelectedItem()->getText();
})
]);
echo "Loaded";
})
->setBackgroundColour('yellow')
->build();
$menu->open(); |
First of all, thank you for your time and the example. The solution above kind of works, but I wonder if it can be improved to the following flow. Step 1.
Step 2. Step 3.
Selecting any of the options above would take user to the root, which would look as: Step 4.
That's it. In other words, I'd like to generate sub-menu of a root menu item dynamically. I hope this explains it :-) Cheers! |
Ah yeah, I thought that's what you really meant, you can do that by extending the submenu item: <?php
declare(strict_types=1);
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
require_once(__DIR__ . '/../vendor/autoload.php');
$itemCallable = function (CliMenu $menu) {
echo $menu->getSelectedItem()->getText();
};
class DynamicSubMenu extends MenuMenuItem
{
public function __construct(string $text, string $title)
{
parent::__construct($text, new CliMenu($title, []), false);
}
public function getSelectAction() : ?callable
{
return function (CliMenu $menu) {
$this->getSubMenu()->setItems([
new \PhpSchool\CliMenu\MenuItem\SelectableItem('Option 1', function (CliMenu $menu) {
echo $menu->getSelectedItem()->getText();
}),
new \PhpSchool\CliMenu\MenuItem\SelectableItem('Option 2', function (CliMenu $menu) {
echo $menu->getSelectedItem()->getText();
})
]);
$this->showSubMenu($menu);
};
}
}
$menu = (new CliMenuBuilder)
->setTitle('CLI Menu')
->addMenuItem(new DynamicSubMenu('Options', 'CLI Menu > Options'))
->setBackgroundColour('yellow')
->build();
$menu->open(); It would be cool to support this natively though. I guess we could accept a callable parameter to So public function getSelectAction() : ?callable
{
$show = function (CliMenu $menu) {
$this->showSubMenu($menu);
};
if ($this->customAction === null) {
return $open;
}
($this->customAction)($open);
} |
Yes, that's what I had in mind. I'd have to dig deep into the source code to prepare a PR. I'll try to spend some time on it. Cheers! |
Got a question regarding your latest solution. The "GoBack" action does not return from the
I suppose that's because there's no proper support for dynamic submenus? |
Yeah, that seems to be the case:
|
Yes probably a few things don't work properly, I'm sure you could hack them in, but would be nice to have baked in support, feel free to ping if you have any questions ! eg something hacky like |
Maybe the key is to make the following work:
Started digging in the source now, hopefully will be able to figure out proper solution. |
Something along these lines:
In other words, a support for dynamically adding submenus when a menu item is selected would be an amazing feature.
Keep up the great work!
Thank you :)
The text was updated successfully, but these errors were encountered: