Skip to content

Commit b710b8e

Browse files
committed
fix: set the pointer to the selected option for the computed options
1 parent 1943696 commit b710b8e

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

dev/app.vue

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</ul>
1414
<v-select
1515
v-model="selected"
16-
:options="options"
16+
:options="computedOptions"
1717
:clearable="false"
1818
:searchable="false"
1919
/>
@@ -34,13 +34,19 @@ import BaseSelect from './BaseSelect.vue'
3434
const selected = ref(null)
3535
const options = ref(countries)
3636
37-
const selectedOption = ref(null)
37+
const computedOptions = computed(() => countries)
38+
3839
const baseSelectOptions = ref(
3940
countries.map((country) => ({
4041
name: country.label,
4142
value: country.value,
4243
}))
4344
)
45+
const selectedOption = ref()
46+
47+
setTimeout(() => {
48+
selectedOption.value = baseSelectOptions.value[0]
49+
}, 3000)
4450
</script>
4551

4652
<style lang="scss">

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue3-select",
3-
"version": "0.0.5-beta.3",
3+
"version": "0.0.5",
44
"description": "A maintained fork of vue-select for Vue 3",
55
"author": "Jeff Sagal <[email protected]>, Howard Chen <[email protected]>",
66
"homepage": "https://vue-select.org",

src/components/Select.vue

+6-4
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@
112112
'vs__dropdown-option--disabled': !selectable(option),
113113
}"
114114
:aria-selected="index === typeAheadPointer ? true : null"
115-
@mouseover="selectable(option) ? (typeAheadPointer = index) : null"
115+
@mouseover="selectable(option) ? updateTypeAheadPointer(index) : null"
116116
@click.prevent.stop="selectable(option) ? select(option) : null"
117-
@touchstart="selectable(option) ? (typeAheadPointer = index) : null"
117+
@touchstart="
118+
selectable(option) ? updateTypeAheadPointer(index) : null
119+
"
118120
>
119121
<slot name="option" v-bind="normalizeOptionForSlot(option)">
120122
{{ getOptionLabel(option) }}
@@ -731,7 +733,7 @@ export default {
731733
}
732734
733735
if (value !== undefined && value !== null && value !== '') {
734-
return [].concat(value)
736+
return Array.prototype.concat(value)
735737
}
736738
737739
return []
@@ -892,7 +894,7 @@ export default {
892894
* @return {array}
893895
*/
894896
filteredOptions() {
895-
const optionList = [].concat(this.optionList)
897+
const optionList = Array.prototype.concat(this.optionList)
896898
897899
if (!this.filterable && !this.taggable) {
898900
return optionList

src/mixins/pointerScroll.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default {
3838
return (this.$refs.dropdownMenu.scrollTop = optionEl.offsetTop)
3939
} else if (bottom > bounds.bottom) {
4040
return (this.$refs.dropdownMenu.scrollTop =
41-
optionEl.offsetTop - (bounds.height - height))
41+
optionEl.offsetTop - (bounds.height - height) + 200)
4242
}
4343
}
4444
},

src/mixins/typeAheadPointer.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export default {
2727
},
2828

2929
methods: {
30+
updateTypeAheadPointer(index) {
31+
this.typeAheadPointer = index
32+
},
3033
/**
3134
* Move the typeAheadPointer visually up the list by
3235
* setting it to the previous selectable option.
@@ -35,7 +38,7 @@ export default {
3538
typeAheadUp() {
3639
for (let i = this.typeAheadPointer - 1; i >= 0; i--) {
3740
if (this.selectable(this.filteredOptions[i])) {
38-
this.typeAheadPointer = i
41+
this.updateTypeAheadPointer(i)
3942
break
4043
}
4144
}
@@ -53,7 +56,7 @@ export default {
5356
i++
5457
) {
5558
if (this.selectable(this.filteredOptions[i])) {
56-
this.typeAheadPointer = i
59+
this.updateTypeAheadPointer(i)
5760
break
5861
}
5962
}
@@ -76,12 +79,32 @@ export default {
7679
* Moves the pointer to the last selected option.
7780
*/
7881
typeAheadToLastSelected() {
79-
this.typeAheadPointer =
82+
/**
83+
* @feat If the option is an object, the "label" will be used as the previous selection record,
84+
* enabling the "computed" options to function properly.
85+
*/
86+
if (
87+
this.filteredOptions[0] &&
88+
typeof this.filteredOptions[0] === 'object'
89+
) {
90+
const label = this.label
91+
const index = this.filteredOptions
92+
.map((option) => option[label])
93+
.indexOf(this.selectedValue[this.selectedValue.length - 1]?.[label])
94+
95+
this.updateTypeAheadPointer(
96+
this.selectedValue.length !== 0 ? index : -1
97+
)
98+
return
99+
}
100+
101+
this.updateTypeAheadPointer(
80102
this.selectedValue.length !== 0
81103
? this.filteredOptions.indexOf(
82104
this.selectedValue[this.selectedValue.length - 1]
83105
)
84106
: -1
107+
)
85108
},
86109
},
87110
}

0 commit comments

Comments
 (0)