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

V3 Proposal: Options are always objects – value is always stored as array #767

Closed
sagalbot opened this issue Feb 19, 2019 · 0 comments
Closed
Labels
Milestone

Comments

@sagalbot
Copy link
Owner

There's a lot of checks within the component to see if multiple is true, or if we're working with an option that is an object, not a string. Here's the select method:

/**
 * Select a given option.
 * @param  {Object|String} option
 * @return {void}
 */
select(option) {
  if (!this.isOptionSelected(option)) {
    if (this.taggable && !this.optionExists(option)) {
      option = this.createOption(option)
    }
    if(this.index) {
      if (!option.hasOwnProperty(this.index)) {
        return console.warn(
            `[vue-select warn]: Index key "option.${this.index}" does not` +
            ` exist in options object ${JSON.stringify(option)}.`
        )
      }
      option = option[this.index]
    }
    if (this.multiple && !this.mutableValue) {
      this.mutableValue = [option]
    } else if (this.multiple) {
      this.mutableValue.push(option)
    } else {
      this.mutableValue = option
    }
    this.onInput(this.mutableValue);
  }

  this.onAfterSelect(option)
},

If all options are objects and mutableValue is always an array, we can cut out a whole bunch of conditionals. Additionally, if each option object always has a label and an index, we don't have to check for that either. The code becomes much more concise now:

/**
 * Select a given option.
 * @param  {Object} option
 * @return {void}
 */
select (option) {
  if (!this.isOptionSelected(option)) {
    if (this.taggable && !this.optionExists(option)) {
      option = this.createOption(option);
    }
    
    if (!option.hasOwnProperty(this.index)) {
      return console.warn(
        `[vue-select warn]: Index key "option.${this.index}" does not` +
        ` exist in options object ${JSON.stringify(option)}.`,
      );
    }

    this.mutableValue.push(option);
    this.onInput(this.mutableValue);
  }

  this.onAfterSelect(option);
},
@sagalbot sagalbot added the RFC label Feb 19, 2019
@sagalbot sagalbot added this to the v3.0 milestone Feb 19, 2019
@sagalbot sagalbot changed the title Proposal: Options are always objects – value is always stored as array V3 Proposal: Options are always objects – value is always stored as array Feb 20, 2019
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

1 participant