Select account: sort options

This commit is contained in:
nathan 2023-11-30 14:57:31 -07:00
parent 1bcaf545bb
commit 8f5099a85d
2 changed files with 40 additions and 2 deletions

View File

@ -455,7 +455,7 @@ export const Et2WithSearchMixin = dedupeMixin(<T extends Constructor<LitElement>
{ {
await this.updateComplete; await this.updateComplete;
const moreCount = this._total_result_count - this.select?.querySelectorAll("sl-option.match").length; const moreCount = this._total_result_count - this.select?.querySelectorAll("sl-option.match").length;
if(this._total_result_count == 0 || moreCount == 0 || !this.select) if(this._total_result_count <= 0 || moreCount == 0 || !this.select)
{ {
return nothing; return nothing;
} }

View File

@ -74,6 +74,14 @@ export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass
} }
} }
/**
* OVerridden to do nothing, we handle it differently in _find_options()
* @param {string} newValueElement
* @protected
*/
protected _missingOption(newValueElement : string)
{}
_find_options(val) _find_options(val)
{ {
for(let id of val) for(let id of val)
@ -100,6 +108,7 @@ export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass
option.label = title || ''; option.label = title || '';
this.requestUpdate(); this.requestUpdate();
this.account_options.sort(this.optionSort);
// Directly update if it's already there // Directly update if it's already there
const slOption = this.select?.querySelector('[value="' + id + '"]'); const slOption = this.select?.querySelector('[value="' + id + '"]');
if(slOption) if(slOption)
@ -111,6 +120,7 @@ export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass
}); });
} }
} }
this.account_options.sort(this.optionSort);
} }
get value() get value()
@ -121,13 +131,41 @@ export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass
get select_options() get select_options()
{ {
return [...new Map([...this.account_options, ...(super.select_options || [])].map(item => return [...new Map([...this.account_options, ...(super.select_options || [])].map(item =>
[item.value, item])).values()]; [item.value, item])).values()].sort(this.optionSort);
} }
set select_options(value : SelectOption[]) set select_options(value : SelectOption[])
{ {
super.select_options = value; super.select_options = value;
} }
/**
* Sort options
* @param a
* @param b
* @returns {number}
* @protected
*/
protected optionSort(a : SelectOption, b : SelectOption)
{
// Sort accounts before groups, then by label
let int_a = 0;
let int_b = 0;
if(typeof a.value === "string")
{
int_a = parseInt(a.value) ?? 0;
}
if(typeof b.value === "string")
{
int_b = parseInt(b.value) ?? 0;
}
if(int_a < 0 && int_b < 0 || int_a > 0 && int_b > 0)
{
return ('' + a.label).localeCompare(b.label);
}
// Accounts before groups
return int_b - int_a;
}
} }
return SelectAccount as unknown as Constructor<SelectAccountInterface> & T; return SelectAccount as unknown as Constructor<SelectAccountInterface> & T;