Skip to content

Commit

Permalink
feat: place focus on search when select #163
Browse files Browse the repository at this point in the history
  • Loading branch information
adamberecz committed Dec 16, 2021
1 parent a474227 commit 2983fb9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Multiselect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
pointer: pointer.pointer,
clearPointer: pointer.clearPointer,
blur: multiselect.blur,
focus: multiselect.focus,
deactivate: multiselect.deactivate,
})
Expand Down
7 changes: 6 additions & 1 deletion src/composables/useMultiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ export default function useMultiselect (props, context, dep)
multiselect.value.blur()
}

const handleFocus = () => {
const focus = () => {
if (searchable.value && !disabled.value) {
input.value.focus()
}
}

const handleFocus = () => {
focus()
}

const activate = () => {
if (disabled.value) {
return
Expand Down Expand Up @@ -70,6 +74,7 @@ export default function useMultiselect (props, context, dep)
tabindex,
isActive,
blur,
focus,
handleFocus,
activate,
deactivate,
Expand Down
3 changes: 3 additions & 0 deletions src/composables/useOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function useOptions (props, context, dep)
const pointer = dep.pointer
const clearPointer = dep.clearPointer
const blur = dep.blur
const focus = dep.focus
const deactivate = dep.deactivate

// ================ DATA ================
Expand Down Expand Up @@ -338,6 +339,8 @@ export default function useOptions (props, context, dep)

if (closeOnSelect.value) {
deactivate()
} else {
focus()
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/composables/useOptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,48 @@ describe('useOptions', () => {
jest.advanceTimersByTime(1)
expect(select.vm.isOpen).toBe(true)
})

it('should focus input on select when closeOnSelect=false and has search', async () => {
let select = createSelect({
value: null,
options: [1,2,3],
closeOnSelect: false,
searchable: true,
}, {
attach: true,
})

select.vm.open()
expect(select.vm.isOpen).toBe(true)

select.vm.handleOptionClick(select.vm.getOption(2))

jest.advanceTimersByTime(1)
expect(document.activeElement == select.vm.input).toBe(true)

destroy(select)
})

it('should not focus input on select when closeOnSelect=true and has search', async () => {
let select = createSelect({
value: null,
options: [1,2,3],
closeOnSelect: true,
searchable: true,
}, {
attach: true,
})

select.vm.open()
expect(select.vm.isOpen).toBe(true)

select.vm.handleOptionClick(select.vm.getOption(2))

jest.advanceTimersByTime(1)
expect(document.activeElement == select.vm.input).toBe(false)

destroy(select)
})
})

describe('handleGroupClick', () => {
Expand Down

0 comments on commit 2983fb9

Please sign in to comment.