Skip to content

Commit

Permalink
fix(VCombobox): correctly handle duplicate items (#13366)
Browse files Browse the repository at this point in the history
fixes #12351

* fix(VCombobox): correctly handle duplicate items

* fix(VCombobox): copy object beore it is removed

* fix(VCombobox): copy object before it is removed

* chore: resolve merge conflicts

* chore: add deleted changes
  • Loading branch information
vibhash-singh committed Jun 14, 2021
1 parent 25e80a1 commit 5fbea51
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/vuetify/src/components/VCombobox/VCombobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,15 @@ export default VAutocomplete.extend({
return this.updateEditing()
}

const index = this.selectedItems.indexOf(this.internalSearch)
const index = this.selectedItems.findIndex(item =>
this.internalSearch === this.getText(item))

// If the duplicate item is an object,
// copy it, so that it can be added again later
const itemToSelect = index > -1 && typeof this.selectedItems[index] === 'object'
? Object.assign({}, this.selectedItems[index])
: this.internalSearch

// If it already exists, do nothing
// this might need to change to bring
// the duplicated item to the last entered
Expand All @@ -239,7 +247,8 @@ export default VAutocomplete.extend({
// TODO: find out where
if (menuIndex > -1) return (this.internalSearch = null)

this.selectItem(this.internalSearch)
this.selectItem(itemToSelect)

this.internalSearch = null
},
onPaste (event: ClipboardEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,32 @@ describe('VCombobox.ts', () => {

expect(change).toHaveBeenLastCalledWith(['foo'])
})

// https://github.com/vuetifyjs/vuetify/issues/12351
it('should correctly handle duplicate items', async () => {
const { wrapper, change } = createMultipleCombobox({
chips: true,
multiple: true,
items: [
{ text: 'foo', value: 'foo' },
{ text: 'bar', value: 'bar' },
],
value: [
{ text: 'foo', value: 'foo' },
],
})

const input = wrapper.find('input')
const element = input.element as HTMLInputElement

input.trigger('focus')
element.value = 'foo'
input.trigger('input')
await wrapper.vm.$nextTick()

input.trigger('keydown.tab')
await wrapper.vm.$nextTick()

expect(change).toHaveBeenLastCalledWith([{ text: 'foo', value: 'foo' }])
})
})

0 comments on commit 5fbea51

Please sign in to comment.