primefaces / primeng

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

Dropdown: when using `selectedItem` template if the selected option is `0` then no template is rendered #15366

Open frabonomi opened 2 weeks ago

frabonomi commented 2 weeks ago

Describe the bug

When using the p-dropdown component with a custom selectedItem template to customize the selected label display as per documentation, the template isn't rendered if the selected option equals to 0.

PrimeNG Dropdown issue when the selected option is 0

From a quick look at the code it seems that this is happening because of the condition selectedItemTemplate && selectedOption. In my case the selectedOption is 0 and is therefore considered falsy, but it should be a treated as a possible valid value.

In a way it seems to be related to issue https://github.com/primefaces/primeng/issues/14954

Environment

MacOS 14.2.1

Reproducer

https://stackblitz.com/edit/github-41pgth?file=src%2Fapp%2Fapp.component.html

Angular version

17.3.5

PrimeNG version

17.14.1

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

LTS

Browser(s)

No response

Steps to reproduce the behavior

  1. Add a p-dropdown component
  2. Set its options to an array containing a value that is 0
  3. Define a custom selectedItem template
  4. Select the option with the value 0

Expected behavior

The custom template should be rendered even if the selected option is 0.

frabonomi commented 2 weeks ago

Happy to open a PR for this, if needed ☝️