Et2Select / SearchMixin: fix some more allowFreeEntry issues

- existing values were not being properly matched
- " freeEntry " option.class (with spaces) was causing nasty error in LitElement
This commit is contained in:
nathan 2024-12-19 11:35:10 -07:00
parent e469627ab7
commit c0ac4cafae
2 changed files with 16 additions and 6 deletions

View File

@ -311,6 +311,9 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
private __value : string | string[] = "";
// Flag to avoid issues with free entries & fix_bad_value
private __inInitialSetup : boolean = true;
protected tagOverflowObserver : IntersectionObserver = null;
protected get dropdown() { return this.select; }
@ -429,6 +432,10 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
}
// If no value is set, choose the first option
// Only do this on once during initial setup, or it can be impossible to clear the value
if(!this.__inInitialSetup)
{
return;
}
// value not in options --> use emptyLabel, if exists, or first option otherwise
if(this.select_options.filter((option) => valueArray.find(val => val == option.value) ||
@ -562,7 +569,11 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
super.loadFromXML(_node);
// 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());
this.updateComplete.then(() =>
{
this.fix_bad_value();
this.__inInitialSetup = false;
});
}
/** @param changedProperties */
@ -935,7 +946,7 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
// Tag used must match this.optionTag, but you can't use the variable directly.
// Pass option along so SearchMixin can grab it if needed
const value = (<string>option.value).replaceAll(" ", "___");
const classes = option.class ? Object.fromEntries((option.class).split(" ").map(k => [k, true])) : {};
const classes = option.class ? Object.fromEntries((option.class).trim().split(" ").map(k => [k, true])) : {};
return html`
<sl-option
part="option"

View File

@ -328,7 +328,7 @@ export const Et2WithSearchMixin = dedupeMixin(<T extends Constructor<LitElement>
async getUpdateComplete()
{
const result = super.getUpdateComplete();
const result = await super.getUpdateComplete();
if(this._searchInputNode)
{
await this._searchInputNode.updateComplete;
@ -358,8 +358,7 @@ export const Et2WithSearchMixin = dedupeMixin(<T extends Constructor<LitElement>
if(changedProperties.has("value") && this.value)
{
// Overridden to add options if allowFreeEntries=true
if(this.allowFreeEntries && typeof this.value == "string" && !this.select_options.find(o => o.value == this.value &&
(!o.class || o.class && !o.class.includes('remote'))))
if(this.allowFreeEntries && typeof this.value == "string" && !this.select_options.find(o => o.value == this.value))
{
this.createFreeEntry(this.value);
}
@ -1564,7 +1563,7 @@ export const Et2WithSearchMixin = dedupeMixin(<T extends Constructor<LitElement>
this.value = value;
}
this.select_options = this.select_options.filter(v => v.value !== original);
this.dropdown.querySelector(".freeEntry[value='" + original.replace(/'/g, "\\\'") + "']").remove();
this.dropdown.querySelector(".freeEntry[value='" + original.replace(/'/g, "\\\'") + "']")?.remove();
}
if(value && value != original)