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

fix(VCombobox): correctly handle duplicate items #13366

Conversation

vibhash-singh
Copy link
Contributor

@vibhash-singh vibhash-singh commented Apr 2, 2021

Description

Correctly handle duplicate item addition in combobox. Fixes #12351

Motivation and Context

Combobox removes the saved value on blur when an input string matches an existing item. This only happens with the list of objects and works fine with the list of strings. When we input a string in the combobox it is checked for the duplicate in the existing selected item list. This is done in updateTags() in VCombobox.ts

const index = this.selectedItems.indexOf(this.internalSearch)

This check will always fail for objects, as it is compared with a string. Then we try to add the search value to the list of selected items in selectItem() in VSelect.ts . There this value is found in the list of items and hence deselected, which removes it from the selected items list.

(this.internalValue || []).findIndex((i: object) => this.valueComparator(this.getValue(i), itemValue))

i !== -1 ? internalValue.splice(i, 1) : internalValue.push(item)

The fix is to correctly search and add object in the selected items list.

How Has This Been Tested?

Unit testing and Visually

Markup:

// Paste your FULL Playground.vue here
<template>
  <v-app id="inspire">
    <v-container fluid>
      <v-combobox
        v-model="model"
        :items="items"
        label="Search for an option"
        multiple
        small-chips
        @change = change
      >
      </v-combobox>
    </v-container>
  </v-app>      
</template>

<script>
export default {
 data: () => ({
    items: [
      {
        text: 'Foo',
        value: 'Foo'
      },
      {
        text: 'Bar',
        value: 'Bar'
      },
    ],
    model: [
      {
        text: 'Foo',
        value: 'Foo'
      },
    ]
  }),

  methods: {
    change() {
      console.log(this.model)
    }
  }
}
</script>


Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Improvement/refactoring (non-breaking change that doesn't add any features but makes things better)

Checklist:

  • The PR title is no longer than 64 characters.
  • The PR is submitted to the correct branch (master for bug fixes and documentation updates, dev for new features and backwards compatible changes and next for non-backwards compatible changes).
  • My code follows the code style of this project.
  • I've added relevant changes to the documentation (applies to new features and breaking changes in core library)

@johnleider
Copy link
Member

I wanted some more time to test this, will most likely go out with v2.4.12.

@johnleider johnleider self-requested a review April 21, 2021 01:26
@johnleider johnleider added C: VCombobox VCombobox T: bug Functionality that does not work as intended/expected labels Jun 11, 2021
@johnleider johnleider added this to the v2.5.x milestone Jun 11, 2021
@johnleider johnleider added the S: has merge conflicts The pending Pull Request has merge conflicts label Jun 11, 2021
@johnleider
Copy link
Member

Looks good, please resolve conflicts.

@vibhash-singh
Copy link
Contributor Author

@johnleider Done. Please check.

@johnleider johnleider merged commit 5fbea51 into vuetifyjs:master Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: VCombobox VCombobox S: has merge conflicts The pending Pull Request has merge conflicts T: bug Functionality that does not work as intended/expected
Projects
None yet
2 participants