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

Cannot remove options from submenu #443

Open
Shawful opened this issue Nov 7, 2023 · 0 comments
Open

Cannot remove options from submenu #443

Shawful opened this issue Nov 7, 2023 · 0 comments

Comments

@Shawful
Copy link

Shawful commented Nov 7, 2023

Hello, I can add, but cannot remove submenu items. Please help. Maybe I'm just missing something, but I believe this should work.

const { Select, Input } = require('enquirer');

let submenuChoices = ['Option A', 'Option B', 'Option C', 'Back to Main Menu'];

const mainMenu = () => {
    const mainMenuPrompt = new Select({
        name: 'option',
        message: 'Select an option:',
        choices: [
            'Actions Menu',
            'Add Action',
            'Remove Action',
            'Exit',
        ],
    });

    mainMenuPrompt.run().then((choice) => {
        if (choice === 'Add Action') {
            addOption();
        } else if (choice === 'Remove Action') {
            removeOption();
        } else if (choice === 'Actions Menu') {
            submenu(mainMenu);
        } else if (choice === 'Exit') {
            console.log('Goodbye!');
        }
    });
};

const addOption = () => {
    const prompt = new Input({
        message: 'Enter a new option:',
        initial: 'New Option',
    });

    prompt.run().then((newOption) => {
        submenuChoices.push(newOption); // Add to submenu
        console.log(`Added new option: ${newOption}`);
        mainMenu();
    });
};

const removeOption = () => {
     const removePrompt = new Select({
         name: 'remove',
         message: 'Select an option to remove:',
         choices: submenuChoices, // Use submenu choices for removal
     });

     removePrompt.run().then((optionToRemove) => {
         // Remove from submenu choices
         submenuChoices = submenuChoices.filter((submenuOption) => submenuOption !== optionToRemove);
         console.log(`Removed option: ${optionToRemove}`);
         mainMenu();
     });
 };

const submenu = (backFunction) => {
    const submenuPrompt = new Select({
        name: 'submenu',
        message: 'Submenu options:',
        choices: submenuChoices,
    });

    submenuPrompt.run().then((selectedOption) => {
        if (selectedOption === 'Back to Main Menu') {
            backFunction();
        } else {
            console.log(`Selected submenu option: ${selectedOption}`);
            submenu(backFunction);
        }
    });
};

mainMenu();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant