diff --git a/api/js/etemplate/Et2Select/Et2Select.ts b/api/js/etemplate/Et2Select/Et2Select.ts index a954b96355..d6d4bf1f75 100644 --- a/api/js/etemplate/Et2Select/Et2Select.ts +++ b/api/js/etemplate/Et2Select/Et2Select.ts @@ -241,9 +241,8 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect) * Careful when this is called. We change the value here, so an infinite loop is possible if the widget has * onchange. * - * @private */ - private fix_bad_value() + protected fix_bad_value() { if(this.multiple || (!this.emptyLabel && (!Array.isArray(this.select_options) || this.select_options.length == 0))) { @@ -322,7 +321,9 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect) loadFromXML(_node : Element) { super.loadFromXML(_node); - this.fix_bad_value(); + + // Wait for update to be complete before we check for bad value so extending selects can have a chance + this.updateComplete.then(() => this.fix_bad_value()); } /** @param {import('@lion/core').PropertyValues } changedProperties */ @@ -332,7 +333,7 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect) if(changedProperties.has('select_options') || changedProperties.has("value") || changedProperties.has('emptyLabel')) { - this.fix_bad_value(); + this.updateComplete.then(() => this.fix_bad_value()); } if(changedProperties.has("select_options") && changedProperties.has("value")) { diff --git a/api/js/etemplate/Et2Select/Et2SelectCategory.ts b/api/js/etemplate/Et2Select/Et2SelectCategory.ts index e71d101223..46ac39b1e2 100644 --- a/api/js/etemplate/Et2Select/Et2SelectCategory.ts +++ b/api/js/etemplate/Et2Select/Et2SelectCategory.ts @@ -87,7 +87,7 @@ export class Et2SelectCategory extends Et2StaticSelectMixin(Et2Select) if(changedProperties.has("global_categories") || changedProperties.has("application") || changedProperties.has("parentCat")) { - so.cat(this).then(options => + this.fetchComplete = so.cat(this).then(options => { this.static_options = cleanSelectOptions(options); this.requestUpdate("select_options"); diff --git a/api/js/etemplate/Et2Select/StaticOptions.ts b/api/js/etemplate/Et2Select/StaticOptions.ts index 788a098dc7..513eebc17e 100644 --- a/api/js/etemplate/Et2Select/StaticOptions.ts +++ b/api/js/etemplate/Et2Select/StaticOptions.ts @@ -29,13 +29,19 @@ export const Et2StaticSelectMixin = > class Et2StaticSelectOptions extends (superclass) { + // Hold the static widget options separately so other options (like sent from server in sel_options) won't + // conflict or be wiped out protected static_options : SelectOption[]; + // If widget needs to fetch options from server, we might want to wait for them + protected fetchComplete : Promise; + constructor(...args) { super(...args); this.static_options = []; + this.fetchComplete = Promise.resolve(); // Trigger the options to get rendered into the DOM this.requestUpdate("select_options"); @@ -63,6 +69,23 @@ export const Et2StaticSelectMixin = > this.static_options = new_static_options; this.requestUpdate("select_options"); } + + /** + * Override the parent fix_bad_value() to wait for server-side options + * to come back before we check to see if the value is not there. + */ + fix_bad_value() + { + this.fetchComplete.then(() => + { + // @ts-ignore Doesn't know it's an Et2Select + if(typeof super.fix_bad_value == "function") + { + // @ts-ignore Doesn't know it's an Et2Select + super.fix_bad_value(); + } + }) + } } return Et2StaticSelectOptions;