Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #14596 - fixed label logic #14597

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/app/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV

_disabled: boolean | undefined;

itemsWrapper: Nullable<HTMLDivElement>;
itemsWrapper = signal<Nullable<HTMLDivElement>>(null);

itemTemplate: Nullable<TemplateRef<any>>;

Expand Down Expand Up @@ -923,9 +923,20 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
return options;
});

private currentLabel: string = null;

label = computed(() => {
if (this.itemsWrapper()) {
return this.currentLabel;
}
const selectedOptionIndex = this.findSelectedOptionIndex();
return selectedOptionIndex !== -1 ? this.getOptionLabel(this.visibleOptions()[selectedOptionIndex]) : this.placeholder || 'p-emptylabel';
// tricky logic
// this if condition is only true when the selected option was filtered from the options list
if (selectedOptionIndex === -1 && this.currentLabel !== null) {
return this.currentLabel;
}
this.currentLabel = selectedOptionIndex !== -1 ? this.getOptionLabel(this.visibleOptions()[selectedOptionIndex]) : this.placeholder || 'p-emptylabel';
return this.currentLabel;
});

selectedOption: any;
Expand Down Expand Up @@ -970,10 +981,10 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
});
}

if (this.selectedOptionUpdated && this.itemsWrapper) {
if (this.selectedOptionUpdated && this.itemsWrapper()) {
let selectedItem = DomHandler.findSingle(this.overlayViewChild?.overlayViewChild?.nativeElement, 'li.p-highlight');
if (selectedItem) {
DomHandler.scrollInView(this.itemsWrapper, selectedItem);
DomHandler.scrollInView(this.itemsWrapper(), selectedItem);
}
this.selectedOptionUpdated = false;
}
Expand Down Expand Up @@ -1233,7 +1244,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV

onOverlayAnimationStart(event: AnimationEvent) {
if (event.toState === 'visible') {
this.itemsWrapper = DomHandler.findSingle(this.overlayViewChild?.overlayViewChild?.nativeElement, this.virtualScroll ? '.p-scroller' : '.p-dropdown-items-wrapper');
this.itemsWrapper.set(DomHandler.findSingle(this.overlayViewChild?.overlayViewChild?.nativeElement, this.virtualScroll ? '.p-scroller' : '.p-dropdown-items-wrapper'));
this.virtualScroll && this.scroller?.setContentEl(this.itemsViewChild?.nativeElement);

if (this.options && this.options.length) {
Expand All @@ -1243,7 +1254,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
this.scroller?.scrollToIndex(selectedIndex);
}
} else {
let selectedListItem = DomHandler.findSingle(this.itemsWrapper, '.p-dropdown-item.p-highlight');
let selectedListItem = DomHandler.findSingle(this.itemsWrapper(), '.p-dropdown-item.p-highlight');

if (selectedListItem) {
selectedListItem.scrollIntoView({ block: 'nearest', inline: 'nearest' });
Expand All @@ -1262,7 +1273,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
this.onShow.emit(event);
}
if (event.toState === 'void') {
this.itemsWrapper = null;
this.itemsWrapper.set(null);
this.onModelTouched();
this.onHide.emit(event);
}
Expand Down Expand Up @@ -1745,6 +1756,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
}

clear(event: Event) {
this.currentLabel = null;
this.updateModel(null, event);
this.clearEditableLabel();
this.onChange.emit({ originalEvent: event, value: this.value });
Expand Down
Loading