Skip to content

Commit

Permalink
Merge pull request #14821 from rosenthalj/DropdownFixForIssue14815
Browse files Browse the repository at this point in the history
Fixed #14815 - improved logic when modelValue is null and selected option value  is null
  • Loading branch information
cetincakiroglu authored Feb 23, 2024
2 parents f6e53aa + 8f4e5ef commit b18cb41
Showing 1 changed file with 10 additions and 5 deletions.
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

0 comments on commit b18cb41

Please sign in to comment.