Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ export function hasKeyCodeByCode(arr, keyCode) {
return has(arr)
}
}

export function setIntervalImmediately(func, interval) {
func()
return setInterval(func, interval)
}
63 changes: 45 additions & 18 deletions lib/vue-simple-suggest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
:aria-labelledby="listId"
:class="styles.suggestions"
>
<li v-if="!!this.$scopedSlots['misc-item-above']" :class="styles.miscItemAbove">
<li v-if="!!$scopedSlots['misc-item-above']" :class="styles.miscItemAbove">
<slot name="misc-item-above"
:suggestions="suggestions"
:query="text"
Expand Down Expand Up @@ -48,7 +48,7 @@
</slot>
</li>

<li v-if="!!this.$scopedSlots['misc-item-below']" :class="styles.miscItemBelow">
<li v-if="!!$scopedSlots['misc-item-below']" :class="styles.miscItemBelow">
<slot name="misc-item-below"
:suggestions="suggestions"
:query="text"
Expand All @@ -65,7 +65,8 @@ import {
modes,
fromPath,
hasKeyCodeByCode,
hasKeyCode
hasKeyCode,
setIntervalImmediately
} from './misc'

export default {
Expand Down Expand Up @@ -144,7 +145,7 @@ export default {
// Handle run-time mode changes (now working):
watch: {
mode: {
handler(current, old) {
handler(current) {
this.constructor.options.model.event = current

// Can be null if the component is root
Expand Down Expand Up @@ -225,22 +226,46 @@ export default {
created () {
this.controlScheme = Object.assign({}, defaultControls, this.controls)
},
async mounted () {
await this.$slots.default;

this.$nextTick(() => {
this.inputElement = this.$refs['inputSlot'].querySelector('input')

async mounted() {
await this.$slots.default
await this.$nextTick()
// https://jefrydco.id/en/blog/safe-access-vue-refs-undefined
var nbRetries = 0
// Do not use "const interval = setIntervalImmediately(...)" to avoid
// ReferenceError: can't access lexical declaration 'interval' before initialization.
var interval = undefined
interval = setIntervalImmediately(() => {
// The immediate call succeeded.
if (this.inputElement) {
this.setInputAriaAttributes()
this.prepareEventHandlers(true)
} else {
console.error('No input element found')
if (interval !== undefined) {
clearInterval(interval)
}
return
}
const slot = this.$refs['inputSlot']
if (slot) {
if (interval !== undefined) {
clearInterval(interval)
}
this.inputElement = slot.querySelector('input')
if (this.inputElement) {
this.setInputAriaAttributes()
this.prepareEventHandlers(true)
} else {
console.error('No input element found')
}
} else if (++nbRetries == 4) {
if (interval !== undefined) {
clearInterval(interval)
}
console.error('No input slot found')
}
})
}, 50)
},
beforeDestroy () {
this.prepareEventHandlers(false)
if (this.inputElement) {
this.prepareEventHandlers(false)
}
},
methods: {
isEqual(suggestion, item) {
Expand Down Expand Up @@ -300,7 +325,7 @@ export default {
return this.isScopedSlotEmpty.call(this, slot)
},
getPropertyByAttribute (obj, attr) {
return this.isPlainSuggestion ? obj : typeof obj !== undefined ? fromPath(obj, attr) : obj
return this.isPlainSuggestion ? obj : typeof obj !== 'undefined' ? fromPath(obj, attr) : obj
},
displayProperty (obj) {
if (this.isPlainSuggestion) {
Expand Down Expand Up @@ -359,7 +384,7 @@ export default {
this.hover(null)
},
hover (item, elem) {
const elemId = !!item ? this.getId(item, this.hoveredIndex) : ''
const elemId = item ? this.getId(item, this.hoveredIndex) : ''

this.inputElement.setAttribute('aria-activedescendant', elemId)

Expand Down Expand Up @@ -574,6 +599,7 @@ export default {
this.showList()
}

// eslint-disable-next-line no-unsafe-finally
return this.suggestions
}
},
Expand Down Expand Up @@ -628,6 +654,7 @@ export default {
}

this.isPlainSuggestion = nextIsPlainSuggestion
// eslint-disable-next-line no-unsafe-finally
return result
}
},
Expand Down