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

Nested choice name collision in MultiSelect #451

Open
jeylost opened this issue Feb 16, 2024 · 1 comment
Open

Nested choice name collision in MultiSelect #451

jeylost opened this issue Feb 16, 2024 · 1 comment
Labels

Comments

@jeylost
Copy link

jeylost commented Feb 16, 2024

Issue Description

Multisearch functionality does not operate correctly when dealing with nested choices that have repeated names. This issue arises when using the library to create a multiselect prompt with a nested structure where some choices share the same name. Below is a code snippet that replicates the problem.

OS: macOS 14.2.1

Node Version: v20.10.0

Enquirer Version: 2.4.1

Error Messages: No specific error messages are generated, but the selection behavior does not work as expected with nested choices having identical names.

Extensions/Plugins/Helpers: N/A

Code to Reproduce

import Enquirer from 'enquirer';

new Enquirer.prompts.MultiSelect({
  name: 'test-multiselect-bug',
  hint: '(Use <space> to select, <return> to submit)',
  message: 'Please select from list file:',
  choices: [
    {
      "name": "baz/",
      choices: [
        {
          "name": "foo/",
          choices: [
            {
              "name": "bar/",
              "choices": [
                {
                  "name": "bar",
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "foo/",
      "choices": [
        {
          "name": "bar/",
          choices: [ {
            "name": "bar",
          },
        ]
        }
      ]
    },
  ],
  result(choices) {
    return this.map(choices);
  },
  validate(value) {
    if (value.length) {
      return true;
    }

    return 'Failed.';
  },
}).run();

test repo

Workaround

Inserting ZWSPs in the names makes them unique to JavaScript's string comparison while still appearing visually similar in the terminal.
Example

function uniqueName(name) {
  const randomNumberOfSpaces = Math.floor(Math.random() * 6);
  return name + '\u200B'.repeat(randomNumberOfSpaces);
}

Visual representation

test.mov
@jeylost jeylost changed the title multiselect: Nested choice name collision in MultiSelect Feb 16, 2024
@jonschlinkert
Copy link
Member

@jeylost thanks for creating the issue and for the detailed explanation of what's happening. I'm working on a new release and will make sure this gets addressed.

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

No branches or pull requests

2 participants