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 #14815 - improved logic when modelValue is null and selected option value is null #14821

Merged
merged 3 commits into from
Feb 23, 2024
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
15 changes: 10 additions & 5 deletions src/app/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
// 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));
const selectedOptionIndex = options.findIndex((option) => this.isOptionValueEqualsModelValue(option));

return selectedOptionIndex !== -1 ? this.getOptionLabel(options[selectedOptionIndex]) : this.placeholder() || 'p-emptylabel';
});
Expand All @@ -969,12 +969,13 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV

if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) {
const selectedOptionIndex = this.findSelectedOptionIndex();
if (selectedOptionIndex !== -1 || modelValue === undefined || (typeof modelValue === 'string' && modelValue.length === 0) || modelValue === null || this.editable) {

if (selectedOptionIndex !== -1 || modelValue === undefined || (typeof modelValue === 'string' && modelValue.length === 0) || this.isModelValueNotSet() || this.editable) {
this.selectedOption = visibleOptions[selectedOptionIndex];
}
}

if (ObjectUtils.isEmpty(visibleOptions) && (modelValue === undefined || modelValue === null) && ObjectUtils.isNotEmpty(this.selectedOption)) {
if (ObjectUtils.isEmpty(visibleOptions) && (modelValue === undefined || this.isModelValueNotSet()) && ObjectUtils.isNotEmpty(this.selectedOption)) {
this.selectedOption = null;
}

Expand All @@ -985,6 +986,10 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
});
}

private isModelValueNotSet(): boolean {
return this.modelValue() === null && !this.isOptionValueEqualsModelValue(this.selectedOption);
}

displayPlaceholder() {
return ObjectUtils.isEmpty(this.selectedOption) && this.label() === this.placeholder();
}
Expand Down Expand Up @@ -1150,10 +1155,10 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV
}

isSelected(option) {
return this.isValidOption(option) && this.isOptionEqualsModelValue(option);
return this.isValidOption(option) && this.isOptionValueEqualsModelValue(option);
}

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

Expand Down
Loading