From 6ecb2d8cf92d187b52d0277cfb80041ab8526e9e Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 16 May 2023 11:02:20 -0600 Subject: [PATCH] Et2Select: Fix missing options filter to handle option groups too --- api/js/etemplate/Et2Select/Et2Select.ts | 26 ++++++++++++++++++- .../etemplate/Et2Select/FindSelectOptions.ts | 9 ++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/api/js/etemplate/Et2Select/Et2Select.ts b/api/js/etemplate/Et2Select/Et2Select.ts index 86164dd7df..578130da42 100644 --- a/api/js/etemplate/Et2Select/Et2Select.ts +++ b/api/js/etemplate/Et2Select/Et2Select.ts @@ -527,7 +527,31 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect) { if(!this.readonly && value && value.length > 0 && !this.allowFreeEntries && this.select_options.length > 0) { - const missing = value.filter(v => !this.select_options.some(option => option.value == v)); + function filterBySelectOptions(arrayToFilter, options : SelectOption[]) + { + const filteredArray = arrayToFilter.filter(item => + { + // Check if item is found in options + return !options.some(option => + { + if(typeof option.value === 'string') + { + // Regular option + return option.value === item; + } + else if(Array.isArray(option.value)) + { + // Recursively check if item is found in nested array (option groups) + return filterBySelectOptions([item], option.value).length > 0; + } + return false; + }); + }); + + return filteredArray; + } + + const missing = filterBySelectOptions(value, this.select_options); if(missing.length > 0) { console.warn("Invalid option '" + missing.join(", ") + " ' removed"); diff --git a/api/js/etemplate/Et2Select/FindSelectOptions.ts b/api/js/etemplate/Et2Select/FindSelectOptions.ts index 0efa6f81d1..0d1d69e8a1 100644 --- a/api/js/etemplate/Et2Select/FindSelectOptions.ts +++ b/api/js/etemplate/Et2Select/FindSelectOptions.ts @@ -1,6 +1,13 @@ +/** + * Interface for select options + * + * While we can (mostly) handle key => value maps, this is the preferred way to specify selectable options. + * For option groups, value is the list of sub-options. + * + */ export interface SelectOption { - value : string; + value : string | SelectOption[]; label : string; // Hover help text title? : string;