Skip to content

Commit eaddedd

Browse files
pdanpdanrstoenescu
authored andcommitted
QAutocomplete: Fix focus after tab, fix loop in normalizeInterval, fix reposition, select options on keyboard navigation (quasarframework#2159)
close quasarframework#2149 close quasarframework#2155 close quasarframework#2157
1 parent 8e3244d commit eaddedd

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/components/autocomplete/QAutocomplete.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export default {
9999
const popover = this.$refs.popover
100100
if (this.results.length) {
101101
this.__keyboardShow(-1)
102-
if (popover.showing) {
103-
popover.reposition()
102+
if (popover && popover.showing) {
103+
this.$nextTick(() => popover && popover.reposition())
104104
}
105105
else {
106106
popover.show()
@@ -146,8 +146,15 @@ export default {
146146
this.searchId = ''
147147
},
148148
__keyboardCustomKeyHandle (key) {
149-
if (key === 27) { // ESCAPE
150-
this.__clearSearch()
149+
switch (key) {
150+
case 27: // ESCAPE
151+
this.__clearSearch()
152+
break
153+
case 38: // UP key
154+
case 40: // DOWN key
155+
case 9: // TAB key
156+
this.__keyboardSetCurrentSelection(true)
157+
break
151158
}
152159
},
153160
__keyboardShowTrigger () {
@@ -156,7 +163,7 @@ export default {
156163
__keyboardIsSelectableIndex (index) {
157164
return index > -1 && index < this.computedResults.length && !this.computedResults[index].disable
158165
},
159-
setValue (result) {
166+
setValue (result, noClose) {
160167
const value = this.staticData ? result[this.staticData.field] : result.value
161168
const suffix = this.__inputDebounce ? 'Debounce' : ''
162169

@@ -168,11 +175,13 @@ export default {
168175
this[`__input${suffix}`].set(value)
169176

170177
this.$emit('selected', result)
171-
this.__clearSearch()
172-
this.hide()
178+
if (!noClose) {
179+
this.__clearSearch()
180+
this.hide()
181+
}
173182
},
174-
__keyboardSetSelection (index) {
175-
this.setValue(this.results[index])
183+
__keyboardSetSelection (index, navigation) {
184+
this.setValue(this.results[index], navigation)
176185
},
177186
__delayTrigger () {
178187
this.__clearSearch()
@@ -219,7 +228,8 @@ export default {
219228
props: {
220229
fit: true,
221230
anchorClick: false,
222-
noFocus: true
231+
noFocus: true,
232+
noRefocus: true
223233
},
224234
on: {
225235
show: () => {

src/mixins/keyboard-selection.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export default {
1313
this.keyboardIndex = index
1414
}
1515
},
16-
__keyboardSetCurrentSelection () {
16+
__keyboardSetCurrentSelection (navigation) {
1717
if (this.keyboardIndex >= 0 && this.keyboardIndex <= this.keyboardMaxIndex) {
18-
this.__keyboardSetSelection(this.keyboardIndex)
18+
this.__keyboardSetSelection(this.keyboardIndex, navigation)
1919
}
2020
},
2121
__keyboardHandleKey (e) {
@@ -54,7 +54,7 @@ export default {
5454
do {
5555
index = normalizeToInterval(
5656
index + offset,
57-
0,
57+
-1,
5858
this.keyboardMaxIndex
5959
)
6060
}

src/utils/format.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export function normalizeToInterval (v, min, max) {
2929

3030
const size = (max - min + 1)
3131

32-
let index = v % size
32+
let index = min + (v - min) % size
3333
if (index < min) {
3434
index = size + index
3535
}
3636

37-
return index
37+
return index === 0 ? 0 : index // fix for (-a % a) => -0
3838
}
3939

4040
export function pad (v, length = 2, char = '0') {

0 commit comments

Comments
 (0)