p-dropdown unable to auto-select first item if group used #12637
Labels
Type: Bug
Issue contains a bug related to a specific component. Something about the component is not working
Milestone
Describe the bug
When using p-dropdown with a grouped data set, inside of a reactive form, where
autoDisplayFirst
is true / omit, andoptionGroupChildren
is not the default value of'items'
, the following error is logged in the browser console and no default item is selected:core.mjs:8506 ERROR TypeError: Cannot read properties of undefined (reading '0') at Dropdown.updateSelectedOption (primeng-dropdown.mjs:355:69) at Dropdown.ngOnInit (primeng-dropdown.mjs:215:14) at callHook (core.mjs:2488:22) at callHooks (core.mjs:2457:17) at executeInitAndCheckHooks (core.mjs:2408:9) at refreshView (core.mjs:10434:21) at refreshEmbeddedViews (core.mjs:11434:17) at refreshView (core.mjs:10443:9) at refreshEmbeddedViews (core.mjs:11434:17) at refreshView (core.mjs:10443:9) core.mjs:8506 ERROR TypeError: Cannot read properties of undefined (reading '0') at Dropdown.updateSelectedOption (primeng-dropdown.mjs:355:69) at Dropdown.writeValue (primeng-dropdown.mjs:340:14) at setUpControl (forms.mjs:2941:23) at FormGroupDirective.addControl (forms.mjs:4745:9) at FormControlName._setUpControl (forms.mjs:5295:43) at FormControlName.ngOnChanges (forms.mjs:5240:18) at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.mjs:1574:14) at callHook (core.mjs:2498:18) at callHooks (core.mjs:2457:17) at executeInitAndCheckHooks (core.mjs:2408:9)
it looks like the reason is the wrong property is accessed in this one spot in
primeng/src/app/components/dropdown/dropdown.ts
Line 776 in 085a4e6
all areas appear to use
getOptionGroupChildren()
to get the defined field except this one area is still hard coded to 'items'I'm happy to provide a PR sometime later today to address if the issue can be confirmed. Stack blitz attached, changing the data from children -> items works but should not be required
Environment
MacOS 12.0.1
Chrome 109
Reproducer
https://stackblitz.com/edit/github-ixgaz2?file=src%2Fapp%2Fapp.component.html
Angular version
15.1.0
PrimeNG version
15.1.0
Build / Runtime
Angular CLI App
Language
TypeScript
Node version (for AoT issues node --version)
18.10.0
Browser(s)
Chrome 109
Steps to reproduce the behavior
Expected behavior
No error should be logged when the component is using a default value for autoDisplayFirst and group=true and data does not use field
items
as the child propertyThe first selectable option within the first group should be selected probably, as works when it is called
items
The text was updated successfully, but these errors were encountered: