Skip to content

Commit 5b4f26f

Browse files
authored
Emit focus event when input receives focus (#154)
Related to #18
1 parent a92f0e3 commit 5b4f26f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

docs/guide/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Name | Description
3333
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.
3434
input | The component can be used with `v-model`
3535
keyup | Triggered when any keyup event is fired in the input. Often used for catching `keyup.enter`.
36+
focus | Triggered when the input element receives focus.
3637
blur | Triggered when the input field loses focus, except when pressing the `tab` key to focus the dropdown list.
3738

3839
## Slots

src/components/VueTypeaheadBootstrap.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
:aria-label="(!ariaLabelledBy) ? placeholder : false"
2929
:value="inputValue"
3030
:disabled="disabled"
31-
@focus="isFocused = true"
31+
@focus="handleFocus"
3232
@blur="handleFocusOut"
3333
@input="handleInput($event.target.value)"
3434
@keydown.esc="handleEsc($event.target.value)"
@@ -171,6 +171,8 @@ export default {
171171
highlightClass: String
172172
},
173173
174+
emits: ['hit', 'input', 'keyup', 'focus', 'blur'],
175+
174176
computed: {
175177
id() {
176178
return Math.floor(Math.random() * 100000)
@@ -257,6 +259,11 @@ export default {
257259
}
258260
},
259261
262+
handleFocus() {
263+
this.$emit('focus')
264+
this.isFocused = true
265+
},
266+
260267
handleInput(newValue) {
261268
this.isFocused = true
262269
this.inputValue = newValue

tests/unit/VueTypeaheadBootstrap.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,11 @@ describe('VueTypeaheadBootstrap', () => {
187187

188188
expect(wrapper.emitted().blur).toBeFalsy()
189189
})
190+
191+
it('Emits a focus event when the underlying input field receives focus', async () => {
192+
let input = wrapper.find('input')
193+
await input.trigger('focus')
194+
expect(wrapper.emitted().focus).toBeTruthy()
195+
})
190196
})
191197
})

0 commit comments

Comments
 (0)