diff --git a/projects/ng-select2-component/src/lib/select2-utils.ts b/projects/ng-select2-component/src/lib/select2-utils.ts index d52ecc8..f6b4be9 100644 --- a/projects/ng-select2-component/src/lib/select2-utils.ts +++ b/projects/ng-select2-component/src/lib/select2-utils.ts @@ -40,20 +40,20 @@ export class Select2Utils { return Select2Utils.getOptionByValue(data, value as Select2Value); } - static getFirstAvailableOption(data: Select2Data) { + static getFirstAvailableOption(data: Select2Data): Select2Option | null { if (Array.isArray(data)) { for (const groupOrOption of data) { const options = (groupOrOption as Select2Group).options; if (options) { for (const option of options) { if (!option.disabled) { - return option.value; + return option; } } } else { const option = groupOrOption as Select2Option; if (!option.disabled) { - return option.value; + return option; } } } @@ -61,30 +61,23 @@ export class Select2Utils { return null; } - static valueIsNotInFilteredData(filteredData: Select2Data, value: Select2Value) { - if (Select2Utils.isNullOrUndefined(value)) { + static optionIsNotInFilteredData(filteredData: Select2Data, option: Select2Option | null): boolean { + if (Select2Utils.isNullOrUndefined(option)) { return true; } for (const groupOrOption of filteredData) { const options = (groupOrOption as Select2Group).options; - if (options) { - for (const option of options) { - if (option.value === value) { - return false; - } - } - } else if ((groupOrOption as Select2Option).value === value) { + if (options && options.includes(option)) { + return false; + } else if (groupOrOption === option) { return false; } } return true; } - static getPreviousOption( - filteredData: Select2Data , - hoveringValue: Select2Value , - ): Select2Option | null { - let findIt = Select2Utils.isNullOrUndefined(hoveringValue); + static getPreviousOption(filteredData: Select2Data, hoveringOption: Select2Option | null): Select2Option | null { + let findIt = Select2Utils.isNullOrUndefined(hoveringOption); for (let i = filteredData.length - 1; i >= 0; i--) { const groupOrOption = filteredData[i]; const options = (groupOrOption as Select2Group).options; @@ -95,7 +88,7 @@ export class Select2Utils { return option; } if (!findIt) { - findIt = option.value === hoveringValue; + findIt = option === hoveringOption; } } } else { @@ -104,15 +97,15 @@ export class Select2Utils { return option; } if (!findIt) { - findIt = option.value === hoveringValue; + findIt = option === hoveringOption; } } } return null; } - static getNextOption(filteredData: Select2Data | null, hoveringValue: Select2Value) { - let findIt = Select2Utils.isNullOrUndefined(hoveringValue); + static getNextOption(filteredData: Select2Data | null, hoveringOption: Select2Option | null): Select2Option | null { + let findIt = Select2Utils.isNullOrUndefined(hoveringOption); if (filteredData) { for (const groupOrOption of filteredData) { const options = (groupOrOption as Select2Group).options; @@ -123,7 +116,7 @@ export class Select2Utils { return option; } } else if (!findIt) { - findIt = option.value === hoveringValue; + findIt = option === hoveringOption; } } } else { @@ -133,7 +126,7 @@ export class Select2Utils { return option; } } else if (!findIt) { - findIt = option.value === hoveringValue; + findIt = option === hoveringOption; } } } @@ -141,6 +134,35 @@ export class Select2Utils { return null; } + static getFirstOption(filteredData: Select2Data): Select2Option | null { + const firstElement = filteredData[0]; + if (this.isOption(firstElement)) { + return firstElement ?? null; + } else { + return firstElement.options[0] ?? null; + } + } + + static getLastOption(filteredData: Select2Data): Select2Option | null { + const lastElement = filteredData.at(-1); + if (!lastElement) { + return null; + } + if (this.isOption(lastElement)) { + return lastElement; + } else { + return lastElement.options.at(-1) ?? null; + } + } + + static isGroup(element: Select2Group | Select2Option): element is Select2Group { + return !!(element as Select2Group).options; + } + + static isOption(element: Select2Group | Select2Option): element is Select2Option { + return !this.isGroup(element); + } + static getReduceData(data: Select2Data, maxResults = 0): { result: Select2Data; reduce: boolean } { if (maxResults > 0) { let counter = 0; @@ -271,7 +293,7 @@ export class Select2Utils { return count; } - private static isNullOrUndefined(value: any) { + private static isNullOrUndefined(value: any): value is null | undefined { return value === null || value === undefined; } diff --git a/projects/ng-select2-component/src/lib/select2.component.html b/projects/ng-select2-component/src/lib/select2.component.html index 4c416fd..75f0778 100644 --- a/projects/ng-select2-component/src/lib/select2.component.html +++ b/projects/ng-select2-component/src/lib/select2.component.html @@ -1,9 +1,9 @@ -
+
+
@if (selectionOverride()) { - @if (resettable() && resetSelectedValue() !== _value && select2Option && !(disabled() || readonly())) { - × - } @if ( - !multiple() && resettable() && resetSelectedValue() !== _value && select2Option && !(disabled || readonly()) + resettable() && !(disabled() || readonly()) && resetSelectedValue() !== _value && + ((!multiple() && select2Option) || (multiple() && select2Options.length > 0)) ) { - × + } } @else if (!multiple()) { @@ -62,14 +73,14 @@ @if (resettable() && resetSelectedValue() !== _value && select2Option && !(disabled() || readonly())) { - × + } } @else {