primefaces / primeng

The Most Complete Angular UI Component Library
https://primeng.org
Other
10.48k stars 4.6k forks source link

Component: Autocomplete #14615

Open rekna1 opened 9 months ago

rekna1 commented 9 months ago

Describe the bug

Autocomplete bound to property that has value null displays [Object object]

Environment

"@angular/core": "^17.0.7", "primeng": "^17.3.3",

Reproducer

No response

Angular version

17.0.7

PrimeNG version

17.3.3

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

v20.10.0

Browser(s)

No response

Steps to reproduce the behavior

` vm.viewData.besluitType= { PrefLabel:null }

<p-autoComplete appendTo="body" [ngModel]="vm.viewData.besluitType" class="flexauto" [suggestions]="vm.besluitTypes" (completeMethod)="this.searchBesluitTypeSubj.next($event.query)" (ngModelChange)="setBesluitTypeId($event)" field='PrefLabel' name="besluittype">

{{besluittype.PrefLabel}}
      </p-autoComplete>`

Expected behavior

I expect it to display an empty string as was the case in version 16

rekna1 commented 9 months ago

still having this issue with version 17.6

rekna1 commented 9 months ago

I investigated the issue further, the autocomplete binds to { Id:null, Description:null }. see comments in the code

 inputValue = computed(() => {
        const modelValue = this.modelValue();
        if (modelValue) {
            if (typeof modelValue === 'object') {
                const label = this.getOptionLabel(modelValue);
                // so label will be null and in this case it returns modelValue which is an object
                // the condition might be wrong, property Description exists on the model, it only contains null
                // so it should return null (or empty string)
                return label != null ? label : modelValue;
            }
            else {
                return modelValue;
            }
        }
        else {
            return '';
        }
    });
shammill commented 4 months ago

Also having this issue. Having a null label will display as [Object object]. Introduced in v16. There is nothing in the change logs regarding this change in behaviour.

Related to https://github.com/primefaces/primeng/issues/14341