From 016afe8ea870b27c591c32b34fc28b656364937a Mon Sep 17 00:00:00 2001 From: Josh Loya Date: Tue, 21 Sep 2021 18:20:06 -0400 Subject: [PATCH 1/2] Emit blur when underlying input blurs unless the focus changed to a list item. --- src/components/VueTypeaheadBootstrap.vue | 9 +++++---- tests/unit/VueTypeaheadBootstrap.spec.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/components/VueTypeaheadBootstrap.vue b/src/components/VueTypeaheadBootstrap.vue index 0851ffc..fa76ab0 100644 --- a/src/components/VueTypeaheadBootstrap.vue +++ b/src/components/VueTypeaheadBootstrap.vue @@ -240,19 +240,20 @@ export default { this.isFocused = false }, - runFocusOut(tgt) { + runFocusOut(evt) { + const tgt = evt.relatedTarget if (tgt && tgt.classList.contains('vbst-item')) { return } + this.$emit('blur', evt) this.isFocused = false }, handleFocusOut(evt) { - const tgt = evt.relatedTarget if (!!navigator.userAgent.match(/Trident.*rv:11\./) && this.ieCloseFix) { - setTimeout(() => { this.runFocusOut(tgt) }, 300) + setTimeout(() => { this.runFocusOut(evt) }, 300) } else { - this.runFocusOut(tgt) + this.runFocusOut(evt) } }, diff --git a/tests/unit/VueTypeaheadBootstrap.spec.js b/tests/unit/VueTypeaheadBootstrap.spec.js index 8709507..0c8adaf 100644 --- a/tests/unit/VueTypeaheadBootstrap.spec.js +++ b/tests/unit/VueTypeaheadBootstrap.spec.js @@ -172,5 +172,22 @@ describe('VueTypeaheadBootstrap', () => { expect(selectPreviousListItem).toHaveBeenCalledWith() }) + + it('Emits a blur event when the underlying input field blurs', async () => { + let input = wrapper.find('input') + await input.trigger('blur') + expect(wrapper.emitted().blur).toBeTruthy() + }) + + it('Does not emit a blur event if the focus shifted to the dropdown list', async () => { + let input = wrapper.find('input') + wrapper.setData({ inputValue: 'Can' }) + await input.trigger('focus') + + let listItem = wrapper.get('.vbst-item').element + await input.trigger('blur', {relatedTarget: listItem}) + + expect(wrapper.emitted().blur).toBeFalsy() + }) }) }) From 04cd89ed5a7f58ca824692e2594b6349a767d93c Mon Sep 17 00:00:00 2001 From: Josh Loya Date: Tue, 21 Sep 2021 18:51:45 -0400 Subject: [PATCH 2/2] Update docs: add blur event to Reference page. --- docs/guide/reference.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guide/reference.md b/docs/guide/reference.md index c947a98..77d1b92 100644 --- a/docs/guide/reference.md +++ b/docs/guide/reference.md @@ -33,6 +33,7 @@ Name | Description hit | Triggered when an autocomplete item is selected. The entry in the input data array that was selected is returned. If no autocomplete item is selected, the first entry matching the query is selected and returned. input | The component can be used with `v-model` keyup | Triggered when any keyup event is fired in the input. Often used for catching `keyup.enter`. +blur | Triggered when the input field loses focus, except when pressing the `tab` key to focus the dropdown list. ## Slots