Skip to content

Commit cde0ad8

Browse files
KasinhouMatus Kasak
andcommitted
VSB-TUO/Highlight actual option in dropdown menu (#1012)
Co-authored-by: Matus Kasak <matus.kasak@dataquest.sk>
1 parent 744ad22 commit cde0ad8

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<i>{{ 'dropdown.clear' | translate }}</i>
4949
</button>
5050
<button class="dropdown-item collection-item text-truncate" *ngFor="let listEntry of optionsList; let i = index"
51-
[class.active]="i === selectedIndex"
51+
[class.active]="inputFormatter(listEntry) === (currentValue | async) || i === selectedIndex"
5252
(keydown.enter)="onSelect(listEntry); sdRef.close()" (mousedown)="onSelect(listEntry); sdRef.close()"
5353
title="{{ inputFormatter(listEntry) }}" role="option"
5454
[attr.id]="inputFormatter(listEntry) === (currentValue|async) ? ('combobox_' + id + '_selected') : null">

src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
9595
});
9696
}
9797

98+
/**
99+
* Compute the index of the current form value within optionsList.
100+
* Returns -1 when there is no current value or it cannot be matched.
101+
*/
102+
private getIndexForCurrentValue(): number {
103+
try {
104+
if (!this.optionsList || this.optionsList.length === 0) { return -1; }
105+
const rawVal = this.group?.get(this.model.id)?.value;
106+
if (isEmpty(rawVal)) { return -1; }
107+
108+
let currentStr: string;
109+
if (typeof rawVal === 'string') {
110+
currentStr = rawVal;
111+
} else if (this.useFindAllService) {
112+
currentStr = rawVal?.[this.model.displayKey];
113+
} else {
114+
currentStr = rawVal?.display;
115+
}
116+
if (isEmpty(currentStr)) { return -1; }
117+
118+
return this.optionsList.findIndex((le: any) => this.inputFormatter(le) === currentStr);
119+
} catch {
120+
return -1;
121+
}
122+
}
123+
98124
/**
99125
* Get service and method to use to retrieve dropdown options
100126
*/
@@ -114,17 +140,21 @@ export class DsDynamicScrollableDropdownComponent extends DsDynamicVocabularyCom
114140
tap(() => this.loading = false),
115141
).subscribe((list: PaginatedList<CacheableObject>) => {
116142
this.optionsList = list.page;
117-
if (fromInit && this.model.value) {
118-
this.setCurrentValue(this.model.value, true);
119-
}
143+
if (fromInit && this.model.value) {
144+
this.setCurrentValue(this.model.value, true);
145+
}
120146

121147
this.updatePageInfo(
122148
list.pageInfo.elementsPerPage,
123149
list.pageInfo.currentPage,
124150
list.pageInfo.totalElements,
125151
list.pageInfo.totalPages
126152
);
127-
this.selectedIndex = 0;
153+
154+
// Determine which option should be highlighted by default based on current value
155+
const idx = this.getIndexForCurrentValue();
156+
this.selectedIndex = (idx >= 0) ? idx : -1;
157+
128158
this.cdr.detectChanges();
129159
});
130160
}

0 commit comments

Comments
 (0)