Skip to content

Commit 7fac117

Browse files
fix(VCombobox): handle duplicates on chip edit (#13794)
Fixes #6364
1 parent 31d610f commit 7fac117

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Diff for: packages/vuetify/src/components/VCombobox/VCombobox.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,25 @@ export default VAutocomplete.extend({
188188
},
189189
updateEditing () {
190190
const value = this.internalValue.slice()
191-
value[this.editingIndex] = this.internalSearch
191+
const index = this.selectedItems.findIndex(item =>
192+
this.getText(item) === this.internalSearch)
192193

193-
this.setValue(value)
194+
// If user enters a duplicate text on chip edit,
195+
// don't add it, move it to the end of the list
196+
if (index > -1) {
197+
const item = typeof value[index] === 'object'
198+
? Object.assign({}, value[index])
199+
: value[index]
200+
201+
value.splice(index, 1)
202+
value.push(item)
203+
} else {
204+
value[this.editingIndex] = this.internalSearch
205+
}
194206

207+
this.setValue(value)
195208
this.editingIndex = -1
209+
this.internalSearch = null
196210
},
197211
updateCombobox () {
198212
// If search is not dirty, do nothing

Diff for: packages/vuetify/src/components/VCombobox/__tests__/VCombobox-multiple.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,34 @@ describe('VCombobox.ts', () => {
548548

549549
expect(change).toHaveBeenLastCalledWith([{ text: 'foo', value: 'foo' }])
550550
})
551+
552+
// https://github.com/vuetifyjs/vuetify/issues/6364
553+
it('should not add duplicate chip after edit', async () => {
554+
const { wrapper, change } = createMultipleCombobox({
555+
chips: true,
556+
multiple: true,
557+
clearable: true,
558+
items: ['foo', 'bar'],
559+
value: ['foo', 'bar'],
560+
})
561+
562+
const input = wrapper.find('input')
563+
const element = input.element as HTMLInputElement
564+
565+
// Dbl click chip at index 1
566+
const chip = wrapper.findAll('.v-chip').at(1)
567+
chip.trigger('dblclick')
568+
expect(wrapper.vm.editingIndex).toBe(1)
569+
expect(wrapper.vm.internalSearch).toBe('bar')
570+
571+
// Add a duplicate value - 'foo'
572+
input.trigger('focus')
573+
element.value = 'foo'
574+
input.trigger('input')
575+
await wrapper.vm.$nextTick()
576+
input.trigger('keydown.enter')
577+
await wrapper.vm.$nextTick()
578+
579+
expect(change).toHaveBeenLastCalledWith(['bar', 'foo'])
580+
})
551581
})

0 commit comments

Comments
 (0)