From c251e2f1858f049c161965f232170fc68ed5ca16 Mon Sep 17 00:00:00 2001 From: ralf Date: Thu, 2 Jun 2022 13:44:46 +0200 Subject: [PATCH] remove removing of empty description and label widgets also make sure option.value is a string, as sl-select seems to use === to calculate matches so option with value 0 is not selected by a value of '0' --- .../etemplate/Et2Select/Et2WidgetWithSelectMixin.ts | 13 +++++++++++++ api/js/etemplate/Et2Select/FindSelectOptions.ts | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/api/js/etemplate/Et2Select/Et2WidgetWithSelectMixin.ts b/api/js/etemplate/Et2Select/Et2WidgetWithSelectMixin.ts index 36c1c7ed50..b948b09b12 100644 --- a/api/js/etemplate/Et2Select/Et2WidgetWithSelectMixin.ts +++ b/api/js/etemplate/Et2Select/Et2WidgetWithSelectMixin.ts @@ -151,6 +151,19 @@ export const Et2widgetWithSelectMixin = dedupeMixin((superclass) => this.__select_options = cleanSelectOptions(new_options); this.requestUpdate("select_options", old_options); + + // if single selection and value does not match an option, use the first option + let first_option; + if (!this.multiple && !this.empty_label && !this.__select_options.filter(option => { + if (typeof first_option === 'undefined') + { + first_option = option; + } + return option.value === this.value; + }).length && typeof first_option !== 'undefined') + { + this.set_value(first_option.value); + } } /** diff --git a/api/js/etemplate/Et2Select/FindSelectOptions.ts b/api/js/etemplate/Et2Select/FindSelectOptions.ts index 361aa3cc3a..79e41b168d 100644 --- a/api/js/etemplate/Et2Select/FindSelectOptions.ts +++ b/api/js/etemplate/Et2Select/FindSelectOptions.ts @@ -240,6 +240,14 @@ export function cleanSelectOptions(options : SelectOption[] | string[] | object) } else { + // make sure value is a string + options.forEach(option => + { + if (typeof option.value !== 'string') + { + option.value = option.value.toString(); + } + }) fixed_options = options; }