From 17dccde534de1d1ba3f67fdb884a0108ac47fb8d Mon Sep 17 00:00:00 2001 From: joseph rosenthal Date: Thu, 15 Feb 2024 09:04:34 -0500 Subject: [PATCH 1/2] added check for special case where modelValue is null and selectedOption value is null Added new method isModelValueNotSet to test for special case where modelValue is set because (modelValue is null and the selected option has a value of null ) I also renamed method isOptionEqualsModelValue to isOptionEqualsModelValue which is a more descriptive name. Note: in a previous PR that was accepted I refactored the code that resulted in the original isOptionEqualsModelValue method --- src/app/components/dropdown/dropdown.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/app/components/dropdown/dropdown.ts b/src/app/components/dropdown/dropdown.ts index 64a525a0cae..99c040603f7 100755 --- a/src/app/components/dropdown/dropdown.ts +++ b/src/app/components/dropdown/dropdown.ts @@ -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'; }); @@ -969,12 +969,12 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) { const selectedOptionIndex = this.findSelectedOptionIndex(); - if (selectedOptionIndex !== -1 || modelValue === undefined || modelValue === null || this.editable) { + if (selectedOptionIndex !== -1 || modelValue === undefined || 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; } @@ -985,6 +985,12 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV }); } + // modelValue equal to null would usually mean that modelValue was not set + // this method includes a test for the special case where modelValue is set because (modelValue is null and the selected option has a value of null) + private isModelValueNotSet(): boolean { + return (this.modelValue() === null) && !this.isOptionValueEqualsModelValue(this.selectedOption); + } + private getAllVisibleAndNonVisibleOptions() { return this.group ? this.flatOptions(this.options) : this.options || []; } @@ -1146,10 +1152,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()); } From b929c2b67979a0c57ef8930e534fcbc6217f1531 Mon Sep 17 00:00:00 2001 From: joseph rosenthal Date: Thu, 15 Feb 2024 09:37:40 -0500 Subject: [PATCH 2/2] npm run format --- src/app/components/dropdown/dropdown.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/components/dropdown/dropdown.ts b/src/app/components/dropdown/dropdown.ts index 99c040603f7..1c7eec03815 100755 --- a/src/app/components/dropdown/dropdown.ts +++ b/src/app/components/dropdown/dropdown.ts @@ -986,9 +986,9 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV } // modelValue equal to null would usually mean that modelValue was not set - // this method includes a test for the special case where modelValue is set because (modelValue is null and the selected option has a value of null) + // this method includes a test for the special case where modelValue is set because (modelValue is null and the selected option has a value of null) private isModelValueNotSet(): boolean { - return (this.modelValue() === null) && !this.isOptionValueEqualsModelValue(this.selectedOption); + return this.modelValue() === null && !this.isOptionValueEqualsModelValue(this.selectedOption); } private getAllVisibleAndNonVisibleOptions() {