Skip to content

Commit

Permalink
pressing enter after invalid search should be noop. Fixes #442
Browse files Browse the repository at this point in the history
  • Loading branch information
wildhart committed Feb 24, 2021
1 parent 9ae16b6 commit bf4906e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
evt.preventDefault()
if (instance.menu.current === null) return
const current = instance.getNode(instance.menu.current)
if (current.isBranch && instance.disableBranchNodes) return
if ((current.isBranch && instance.disableBranchNodes) || !current.isMatched) return
instance.select(current)
break
}
Expand Down
29 changes: 29 additions & 0 deletions test/unit/specs/KeyboardSupport.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ describe('Keyboard Support', () => {

wrapper.setProps({ multiple: false })

await typeSearchText(wrapper, 'a')
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([ 'a' ])
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([ 'a' ])

await typeSearchText(wrapper, 'b')
vm.setCurrentHighlightedOption(vm.forest.nodeMap.b)
expect(vm.menu.current).toBe('b')
pressEnterKey(wrapper)
Expand All @@ -139,17 +141,44 @@ describe('Keyboard Support', () => {

wrapper.setProps({ multiple: true })

await typeSearchText(wrapper, 'a')
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([ 'a' ])
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([])

await typeSearchText(wrapper, 'b')
vm.setCurrentHighlightedOption(vm.forest.nodeMap.b)
expect(vm.menu.current).toBe('b')
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([ 'b' ])
})

// https://github.com/riophae/vue-treeselect/issues/442
it('pressing enter after invalid search should be a no-op (single-select)', async () => {
const { wrapper, vm } = await createInstance()

wrapper.setProps({ multiple: false })

await typeSearchText(wrapper, 'a')
await typeSearchText(wrapper, 'ab')
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([])
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([])
})

it('pressing enter after invalid search should be a no-op (multi-select)', async () => {
const { wrapper, vm } = await createInstance()

wrapper.setProps({ multiple: true })

await typeSearchText(wrapper, 'a')
await typeSearchText(wrapper, 'ab')
pressEnterKey(wrapper)
expect(vm.internalValue).toEqual([])
})

it('pressing enter key on a disabled option should be no-op', async () => {
const { wrapper, vm } = await createInstance()

Expand Down

0 comments on commit bf4906e

Please sign in to comment.