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

'enabled' property on 'choice' object does not enable choice by default #270

Open
LukeBalizet opened this issue Mar 27, 2020 · 8 comments · May be fixed by #447
Open

'enabled' property on 'choice' object does not enable choice by default #270

LukeBalizet opened this issue Mar 27, 2020 · 8 comments · May be fixed by #447
Labels

Comments

@LukeBalizet
Copy link

LukeBalizet commented Mar 27, 2020

I apologize if I am doing something incorrectly, but when I run the code below, the 'hello' choice is not enabled by default.

const { MultiSelect } = require('enquirer');

const response = new MultiSelect({
  name: 'test',
  choices: [{name: 'hello', enabled: true}]
});
response.run();

The 'hello' choice appears as normal, but the checkmark next to it is grey, not green as expected. Am I doing something wrong, or is there a bug here? Thank you for any help you can give me! (BTW, awesome package!)

@jonschlinkert
Copy link
Member

This is a bug. Thanks for reporting.

@iamtabrezkhan
Copy link

Hey @doowb,

Is this still open? If yes, I would like to try and fix it.

@fabiomcosta
Copy link

Just had this issue as well

@the0neWhoKnocks
Copy link

Any traction on this?

Spent some time trying to figure out what I was doing wrong while following the docs, and had to come here to find out that it's been broken for over a year. I'm using v2.3.6 btw.

@mrspartak
Copy link

Any luck for the basic functionality to be fixed? This is still a thing

@brownieboy
Copy link

brownieboy commented Dec 11, 2023

The issue appears to be in the ArrayPrompt component in the lib/types/array.js file. When the component first loads, it calls its reset method, and that method explicitly sets all the enabled properties to false:

this.choices.forEach(ch => (ch.enabled = false));

If I comment out the line above then my default values are properly shown as already selected when my MultiSelect loads. Although that will mess up any subsequent reset method calls, of course.

The reset method is called by the initialize method. It actually passes a parameter, set to true, to the reset method, but the latter doesn't actually receive any parameters:

  async initialize() {
    if (typeof this.options.initial === 'function') {
      this.initial = await this.options.initial.call(this);
    }
    await this.reset(true);
    await super.initialize();
  }

  async reset() {
    let { choices, initial, autofocus, suggest } = this.options;

I wonder if that parameter was originally supposed to tell the reset method not to wipe all the enabled values on this call?

Maybe something like this to fix the issue?:

  async initialize() {
    if (typeof this.options.initial === "function") {
      this.initial = await this.options.initial.call(this);
    }
    await this.reset({ doNotResetEnabled: true });
    await super.initialize();
  }

  async reset({ doNotResetEnabled = false }) {
    let { choices, initial, autofocus, suggest } = this.options;
    this.state._choices = [];
    this.state.choices = [];

    this.choices = await Promise.all(await this.toChoices(choices));
    if (!doNotResetEnabled) {
      this.choices.forEach((ch) => (ch.enabled = false));
    }

So now a new doNoResetEnabled parameter, passed as an object property, will skip with resetting of all the enabled properties to false.

@brownieboy
Copy link

brownieboy commented Dec 12, 2023

Actually, my change above broke a load of tests. It seems that the first item list was always being enabled by default, and that threw the test counts out.

This is the code that was causing that. It's in the toChoice method of the ArrayPrompt component:

    ele.path = parent ? parent.path + "." + ele.name : ele.name;
    ele.enabled = !!(
      this.multiple &&
      !this.isDisabled(ele) &&
     (ele.enabled || this.isSelected(ele))
    );

The first line is always selected by default. The line (ele.enabled || this.isSelected(ele)) ensures that it's always enabled by default too.

I changed it to remove the this.isSelected() call, like so:

  ele.path = parent ? parent.path + "." + ele.name : ele.name;
    ele.enabled = !!(
      this.multiple &&
      !this.isDisabled(ele) &&
      ele.enabled
    );

Now tests are passing.

@brownieboy brownieboy linked a pull request Dec 12, 2023 that will close this issue
@brownieboy
Copy link

Can I get a review on my PR for this one, please, @jonschlinkert ? It's #447 .

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

Successfully merging a pull request may close this issue.

7 participants