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

fixed #14716 and fixed #14757 - updated computed label logic to also handle disabled selected option #14768

Merged
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
20 changes: 16 additions & 4 deletions src/app/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
}

visibleOptions = computed(() => {
const options = this.group ? this.flatOptions(this.options) : this.options || [];
const options = this.getAllVisibleAndNonVisibleOptions();

if (this._filterValue()) {
const _filterBy = this.filterBy || this.optionLabel;
Expand Down Expand Up @@ -938,9 +938,13 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
});

label = computed(() => {
const selectedOptionIndex = this.findSelectedOptionIndex();
// use getAllVisibleAndNonVisibleOptions verses just visible options
// this will find the selected option whether or not the user is currently filtering because the filtered (i.e. visible) options, are a subset of all the options
const options = this.getAllVisibleAndNonVisibleOptions();
// use isOptionEqualsModelValue for the use case where the dropdown is initalized with a disabled option
const selectedOptionIndex = options.findIndex((option) => this.isOptionEqualsModelValue(option));

return selectedOptionIndex !== -1 ? this.getOptionLabel(this.visibleOptions()[selectedOptionIndex]) : this.placeholder() || 'p-emptylabel';
return selectedOptionIndex !== -1 ? this.getOptionLabel(options[selectedOptionIndex]) : this.placeholder() || 'p-emptylabel';
});

filled = computed(() => {
Expand Down Expand Up @@ -971,6 +975,10 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
});
}

private getAllVisibleAndNonVisibleOptions() {
return this.group ? this.flatOptions(this.options) : this.options || [];
}

ngOnInit() {
this.id = this.id || UniqueComponentId();
this.autoUpdateModel();
Expand Down Expand Up @@ -1126,7 +1134,11 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
}

isSelected(option) {
return this.isValidOption(option) && ObjectUtils.equals(this.modelValue(), this.getOptionValue(option), this.equalityKey());
return this.isValidOption(option) && this.isOptionEqualsModelValue(option);
}

private isOptionEqualsModelValue(option: any) {
return ObjectUtils.equals(this.modelValue(), this.getOptionValue(option), this.equalityKey());
}

ngAfterViewInit() {
Expand Down
Loading