Skip to content

Commit 43a18b6

Browse files
KaelWDjohnleider
authored andcommitted
fix(VSelect): allow null values to be selected (#4649)
* fix(VSelect): allow null values to be selected fixes #4431 * refactor: remove redundant code * spelign * fix(v-select): update clearable to handle null values wrote tests as well
1 parent 8d1724a commit 43a18b6

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

src/components/VAutocomplete/VAutocomplete.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ export default {
5757

5858
data: vm => ({
5959
attrsInput: null,
60-
lazySearch: vm.searchInput,
61-
lazyValue: vm.value != null
62-
? vm.value
63-
: vm.multiple ? [] : undefined
60+
lazySearch: vm.searchInput
6461
}),
6562

6663
computed: {
@@ -273,7 +270,7 @@ export default {
273270
}
274271
},
275272
clearableCallback () {
276-
this.internalSearch = null
273+
this.internalSearch = undefined
277274

278275
VSelect.methods.clearableCallback.call(this)
279276
},
@@ -338,17 +335,11 @@ export default {
338335
this.setSearch()
339336
},
340337
setSelectedItems () {
341-
if (this.internalValue == null ||
342-
this.internalValue === ''
343-
) {
344-
this.selectedItems = []
345-
} else {
346-
VSelect.methods.setSelectedItems.call(this)
338+
VSelect.methods.setSelectedItems.call(this)
347339

348-
// #4273 Don't replace if searching
349-
// #4403 Don't replace is focused
350-
if (!this.isFocused) this.setSearch()
351-
}
340+
// #4273 Don't replace if searching
341+
// #4403 Don't replace if focused
342+
if (!this.isFocused) this.setSearch()
352343
},
353344
setSearch () {
354345
// Wait for nextTick so selectedItem

src/components/VSelect/VSelect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export default {
121121
// As long as a value is defined, show it
122122
// Otherwise, check if multiple
123123
// to determine which default to provide
124-
lazyValue: vm.value != null
124+
lazyValue: vm.value !== undefined
125125
? vm.value
126126
: vm.multiple ? [] : undefined,
127127
selectedIndex: -1,
@@ -267,7 +267,7 @@ export default {
267267
this.isMenuActive = true
268268
},
269269
clearableCallback () {
270-
this.internalValue = this.multiple ? [] : null
270+
this.internalValue = this.multiple ? [] : undefined
271271
this.$emit('change', this.internalValue)
272272
this.$nextTick(() => this.$refs.input.focus())
273273

test/unit/components/VAutocomplete/VAutocomplete.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ test('VAutocomplete.js', ({ mount, shallow, compileToFunctions }) => {
690690

691691
icon.trigger('click')
692692

693-
expect(wrapper.vm.internalSearch).toBe(null)
693+
expect(wrapper.vm.internalSearch).toBe(undefined)
694694
})
695695

696696
it('should propagate content class', () => {

test/unit/components/VSelect/VSelect2.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ test('VSelect', ({ mount, compileToFunctions }) => {
5151

5252
await wrapper.vm.$nextTick()
5353

54-
expect(wrapper.vm.internalValue).toBe(null)
55-
expect(input).toHaveBeenCalledWith(null)
54+
expect(wrapper.vm.internalValue).toBe(undefined)
55+
expect(input).toHaveBeenCalledWith(undefined)
5656
})
5757

5858
it('should be clearable with prop, dirty and single select', async () => {
@@ -73,7 +73,7 @@ test('VSelect', ({ mount, compileToFunctions }) => {
7373

7474
clear.trigger('click')
7575
await wrapper.vm.$nextTick()
76-
expect(wrapper.vm.internalValue).toBe(null)
76+
expect(wrapper.vm.internalValue).toBe(undefined)
7777
expect(wrapper.vm.isMenuActive).toBe(false)
7878
})
7979

test/unit/components/VSelect/VSelect3.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,31 @@ test('VSelect', ({ mount, compileToFunctions }) => {
105105
// Will be undefined if fails
106106
expect(wrapper.vm.listData.on).toBeTruthy()
107107
})
108+
109+
// https://github.com/vuetifyjs/vuetify/issues/4431
110+
it('should accept null and "" as values', async () => {
111+
const wrapper = mount(VSelect, {
112+
propsData: {
113+
clearable: true,
114+
items: [
115+
{ text: 'Foo', value: null },
116+
{ text: 'Bar', value: 'bar' }
117+
],
118+
value: null
119+
},
120+
})
121+
122+
const icon = wrapper.first('.v-input__append-inner .v-icon')
123+
124+
expect(wrapper.vm.selectedItems.length).toBe(1)
125+
expect(wrapper.vm.isDirty).toBe(true)
126+
127+
icon.trigger('click')
128+
129+
await wrapper.vm.$nextTick()
130+
131+
expect(wrapper.vm.selectedItems.length).toBe(0)
132+
expect(wrapper.vm.isDirty).toBe(false)
133+
expect(wrapper.vm.internalValue).toBe(undefined)
134+
})
108135
})

0 commit comments

Comments
 (0)