Et2Select: Fix missing options filter to handle option groups too

This commit is contained in:
nathan 2023-05-16 11:02:20 -06:00 committed by ralf
parent c5ec9ff8d8
commit da2413a07f
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) 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) if(missing.length > 0)
{ {
console.warn("Invalid option '" + missing.join(", ") + " ' removed"); 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 export interface SelectOption
{ {
value : string; value : string | SelectOption[];
label : string; label : string;
// Hover help text // Hover help text
title? : string; title? : string;