Skip to content

Commit adda8e5

Browse files
committed
changes for multi-select
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent f3d5432 commit adda8e5

1 file changed

Lines changed: 40 additions & 19 deletions

File tree

ui/src/components/widgets/InfiniteScrollSelect.vue

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
resourceType="account"
3333
optionValueKey="name"
3434
optionLabelKey="name"
35-
@change-option="handleAccountChange" />
35+
@change-option-value="handleAccountNameChange" />
3636
3737
Props:
3838
- api (String, required): API command name (e.g., 'listAccounts')
@@ -45,7 +45,8 @@
4545
- defaultIcon (String, optional): Icon to be shown when there is no resource icon for the option. Default is 'cloud-outlined'
4646
4747
Events:
48-
- @change-option (Function): Emits the selected option object when value changes. Do not use @change as it will give warnings and may not work
48+
- @change-option-value (Function): Emits the selected option value(s) when value(s) changes. Do not use @change as it will give warnings and may not work
49+
- @change-option (Function): Emits the selected option object when value changes. Works only when mode is not multiple
4950
5051
Features:
5152
- Debounced remote filtering
@@ -168,7 +169,7 @@ export default {
168169
this.onSearch()
169170
}
170171
},
171-
emits: ['change-option'],
172+
emits: ['change-option-value', 'change-option'],
172173
methods: {
173174
async fetchItems () {
174175
if (this.successiveFetches === 0 && this.loading) return
@@ -199,19 +200,7 @@ export default {
199200
}
200201
})
201202
this.page++
202-
203-
if (this.preselectedOptionValue && this.successiveFetches < this.maxSuccessiveFetches) {
204-
const match = this.options.find(entry => entry[this.optionValueKey] === this.preselectedOptionValue)
205-
if (!match) {
206-
this.successiveFetches++
207-
this.fetchItems()
208-
} else {
209-
this.preselectedOptionValue = null
210-
this.successiveFetches = 0
211-
}
212-
} else {
213-
this.successiveFetches = 0
214-
}
203+
this.checkAndFetchPreselectedOption()
215204
}).catch(error => {
216205
this.$notifyError(error)
217206
}).finally(() => {
@@ -220,11 +209,35 @@ export default {
220209
}
221210
})
222211
},
212+
checkAndFetchPreselectedOption () {
213+
if (!this.preselectedOptionValue ||
214+
(Array.isArray(this.preselectedOptionValue) && this.preselectedOptionValue.length === 0) ||
215+
this.successiveFetches >= this.maxSuccessiveFetches) {
216+
this.resetPreselectedOptionValue()
217+
return
218+
}
219+
const matchValue = Array.isArray(this.preselectedOptionValue) ? this.preselectedOptionValue[0] : this.preselectedOptionValue
220+
const match = this.options.find(entry => entry[this.optionValueKey] === matchValue)
221+
if (!match) {
222+
this.successiveFetches++
223+
this.fetchItems()
224+
return
225+
}
226+
if (Array.isArray(this.preselectedOptionValue) && this.preselectedOptionValue.length > 1) {
227+
this.preselectedOptionValue = this.preselectedOptionValue.filter(o => o !== match)
228+
} else {
229+
this.resetPreselectedOptionValue()
230+
}
231+
},
223232
addDefaultOptionIfNeeded () {
224233
if (this.defaultOption) {
225234
this.options.push(this.defaultOption)
226235
}
227236
},
237+
resetPreselectedOptionValue () {
238+
this.preselectedOptionValue = null
239+
this.successiveFetches = 0
240+
},
228241
onSearch (value) {
229242
if (value && value.length > 0 && value.length < 3) {
230243
return
@@ -245,9 +258,17 @@ export default {
245258
this.fetchItems()
246259
}
247260
},
248-
onChange (keyValue) {
249-
this.preselectedOptionValue = null
250-
const match = this.options.find(entry => entry[this.optionValueKey] === keyValue)
261+
onChange (value) {
262+
this.resetPreselectedOptionValue()
263+
this.$emit('change-option-value', value)
264+
if (Array.isArray(value)) {
265+
return
266+
}
267+
if (value === undefined || value == null) {
268+
this.$emit('change-option', undefined)
269+
return
270+
}
271+
const match = this.options.find(entry => entry[this.optionValueKey] === value)
251272
if (match) {
252273
this.$emit('change-option', match)
253274
}

0 commit comments

Comments
 (0)