Et2Select: Fix missing options filter to handle option groups too

This commit is contained in:
nathan 2023-05-16 11:02:20 -06:00
parent eddcc97163
commit 6ecb2d8cf9
2 changed files with 33 additions and 2 deletions

View File

@ -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");

View File

@ -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;