Skip to content

Commit dca428a

Browse files
committed
fix: async option can't selected [Select Component]
1 parent da581bd commit dca428a

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/components/select/src/option.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
selected ? 'at-select__option--selected' : '',
66
isFocus ? 'at-select__option--focus' : ''
77
]"
8-
@click.stop="select"
8+
@click.stop="doSelect"
99
@mouseout.stop="blur"
1010
v-show="!hidden"
1111
ref="option"
@@ -20,6 +20,7 @@ import Emitter from 'src/mixins/emitter'
2020
export default {
2121
name: 'AtOption',
2222
mixins: [Emitter],
23+
inject: ['select'],
2324
props: {
2425
value: {
2526
type: [String, Number],
@@ -48,7 +49,7 @@ export default {
4849
}
4950
},
5051
methods: {
51-
select () {
52+
doSelect () {
5253
if (this.disabled) return false
5354
this.dispatch('AtSelect', 'on-select-selected', this.value)
5455
},
@@ -61,13 +62,26 @@ export default {
6162
}
6263
},
6364
mounted () {
65+
this.select.optionInstances.push(this)
66+
this.select.options.push({
67+
_instance: this,
68+
value: this.value,
69+
label: (typeof this.label === 'undefined') ? this.$el.innerHTML : this.label
70+
})
6471
this.searchLabel = this.$el.innerHTML
6572
this.$on('on-select-close', () => {
6673
this.isFocus = false
6774
})
6875
this.$on('on-query-change', val => {
6976
this.queryChange(val)
7077
})
78+
},
79+
beforeDestroy () {
80+
this.select.options.forEach((option, idx) => {
81+
if (option._instance === this) {
82+
this.select.onOptionDestroy(idx)
83+
}
84+
})
7185
}
7286
}
7387
</script>

src/components/select/src/select.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ export default {
126126
model: this.value
127127
}
128128
},
129+
provide () {
130+
return {
131+
'select': this
132+
}
133+
},
129134
computed: {
130135
showPlaceholder () {
131136
let status = false
@@ -236,7 +241,7 @@ export default {
236241
options.forEach(option => {
237242
if (option.isFocus) {
238243
hasFocus = true
239-
option.select()
244+
option.doSelect()
240245
}
241246
})
242247
@@ -253,21 +258,19 @@ export default {
253258
options.forEach(option => {
254259
if (!firstOption && !option.hidden) {
255260
firstOption = option
256-
option.select()
261+
option.doSelect()
257262
}
258263
})
259264
},
260265
updateOptions () {
261266
const options = []
262-
let index = 1
263267
264268
const optionsEle = findComponentsDownward(this, 'AtOption')
265269
optionsEle.forEach(option => {
266270
options.push({
267271
value: option.value,
268272
label: (typeof option.label === 'undefined') ? option.$el.innerHTML : option.label
269273
})
270-
option.index = index++
271274
272275
this.optionInstances.push(option)
273276
})
@@ -277,6 +280,10 @@ export default {
277280
this.updateSingleSelected(true)
278281
this.updateMultipleSelected(true)
279282
},
283+
onOptionDestroy (index) {
284+
this.options.splice(index, 1)
285+
this.optionInstances.splice(index, 1)
286+
},
280287
updateSingleSelected (init = false) {
281288
const type = typeof this.model
282289
@@ -413,8 +420,8 @@ export default {
413420
414421
const options = findComponentsDownward(this, 'AtOption')
415422
416-
options.forEach(option => {
417-
if (option.index === this.focusIndex) {
423+
options.forEach((option, idx) => {
424+
if ((idx + 1) === this.focusIndex) {
418425
isValid = !option.disabled && !option.hidden
419426
420427
if (isValid) {

0 commit comments

Comments
 (0)