diff --git a/src/components/autocomplete/AutoComplete.vue b/src/components/autocomplete/AutoComplete.vue index 973f2a3ab..23774aab0 100755 --- a/src/components/autocomplete/AutoComplete.vue +++ b/src/components/autocomplete/AutoComplete.vue @@ -783,6 +783,16 @@ export default { this.$emit('update:modelValue', value); this.$emit('change', { originalEvent: event, value }); }, + flatOptions(options = []) { + return options.reduce((result, option, index) => { + result.push({ optionGroup: option, group: true, index }); + + const optionGroupChildren = this.getOptionGroupChildren(option); + optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); + + return result; + }, []); + }, overlayRef(el) { this.overlay = el; }, @@ -824,20 +834,7 @@ export default { return ['p-autocomplete-loader pi-spin', this.loadingIcon]; }, visibleOptions() { - let options = this.suggestions || []; - - if (this.optionGroupLabel) { - options = options.reduce((result, option, index) => { - result.push({ optionGroup: option, group: true, index }); - - let optionGroupChildren = this.getOptionGroupChildren(option); - optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); - - return result; - }, []); - } - - return options; + return this.optionGroupLabel ? this.flatOptions(this.suggestions) : (this.suggestions || []); }, inputValue() { if (this.modelValue) { diff --git a/src/components/dropdown/Dropdown.vue b/src/components/dropdown/Dropdown.vue index 969d1b26c..d7065c695 100755 --- a/src/components/dropdown/Dropdown.vue +++ b/src/components/dropdown/Dropdown.vue @@ -771,6 +771,16 @@ export default { this.$emit('update:modelValue', value); this.$emit('change', { originalEvent: event, value }); }, + flatOptions(options = []) { + return options.reduce((result, option, index) => { + result.push({ optionGroup: option, group: true, index }); + + const optionGroupChildren = this.getOptionGroupChildren(option); + optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); + + return result; + }, []); + }, overlayRef(el) { this.overlay = el; }, @@ -809,18 +819,7 @@ export default { return ['p-dropdown-trigger-icon', (this.loading ? this.loadingIcon : 'pi pi-chevron-down')]; }, visibleOptions() { - let options = this.options || []; - - if (this.optionGroupLabel) { - options = options.reduce((result, option, index) => { - result.push({ optionGroup: option, group: true, index }); - - let optionGroupChildren = this.getOptionGroupChildren(option); - optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); - - return result; - }, []); - } + const options = this.optionGroupLabel ? this.flatOptions(this.options) : (this.options || []); return this.filterValue ? FilterService.filter(options, this.searchFields, this.filterValue, this.filterMatchMode, this.filterLocale) : options; }, diff --git a/src/components/listbox/Listbox.vue b/src/components/listbox/Listbox.vue index 17bb38479..f35066c4e 100755 --- a/src/components/listbox/Listbox.vue +++ b/src/components/listbox/Listbox.vue @@ -621,6 +621,16 @@ export default { this.$emit('update:modelValue', value); this.$emit('change', { originalEvent: event, value }); }, + flatOptions(options = []) { + return options.reduce((result, option, index) => { + result.push({ optionGroup: option, group: true, index }); + + const optionGroupChildren = this.getOptionGroupChildren(option); + optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); + + return result; + }, []); + }, listRef(el, contentRef) { this.list = el; contentRef && contentRef(el); // For VirtualScroller @@ -637,18 +647,7 @@ export default { }]; }, visibleOptions() { - let options = this.options || []; - - if (this.optionGroupLabel) { - options = options.reduce((result, option, index) => { - result.push({ optionGroup: option, group: true, index }); - - let optionGroupChildren = this.getOptionGroupChildren(option); - optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); - - return result; - }, []); - } + const options = this.optionGroupLabel ? this.flatOptions(this.options) : (this.options || []); return this.filterValue ? FilterService.filter(options, this.searchFields, this.filterValue, this.filterMatchMode, this.filterLocale) : options; }, diff --git a/src/components/multiselect/MultiSelect.vue b/src/components/multiselect/MultiSelect.vue index 946ca6a7c..772d48adf 100755 --- a/src/components/multiselect/MultiSelect.vue +++ b/src/components/multiselect/MultiSelect.vue @@ -901,6 +901,16 @@ export default { this.$emit('update:modelValue', value); this.$emit('change', { originalEvent: event, value }); }, + flatOptions(options = []) { + return options.reduce((result, option, index) => { + result.push({ optionGroup: option, group: true, index }); + + const optionGroupChildren = this.getOptionGroupChildren(option); + optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); + + return result; + }, []); + }, overlayRef(el) { this.overlay = el; }, @@ -939,18 +949,7 @@ export default { }]; }, visibleOptions() { - let options = this.options || []; - - if (this.optionGroupLabel) { - options = options.reduce((result, option, index) => { - result.push({ optionGroup: option, group: true, index }); - - let optionGroupChildren = this.getOptionGroupChildren(option); - optionGroupChildren && optionGroupChildren.forEach(o => result.push(o)); - - return result; - }, []); - } + const options = this.optionGroupLabel ? this.flatOptions(this.options) : (this.options || []); return this.filterValue ? FilterService.filter(options, this.searchFields, this.filterValue, this.filterMatchMode, this.filterLocale) : options; },