Dynamic TabView: Auto-select next tab on closing
Describe the bug
Referring to issue #11905
Created a dynamic, closable TabView.
According to the official documentation the selected property of the TabPanels must be set manually. From my understanding, there is no need to set the activeIndex property of the TabView:
Dynamic Tabs
p-tabPanel components can be generated dynamically using ngFor, note that in this case due to an Angular issue selected property must be enabled manually.
<p-tabView>
<p-tabPanel [header]="item.header" *ngFor="let item of items; let i = index" [selected]="i == 0">
{{item.content}}
</p-tabPanel>
</p-tabView>
Properties of TabView
activeIndex | number | null | Index of the active tab to change selected tab programmatically.
Properties of TabPanel
selected | boolean | false | Defines if tab is active.
So, to handle the selected/active tab I've created a variable in the code-behind.
Selecting a tab programmatically works just fine.
Closing a tab seems to produce strange behaviour. During the OnTabClose-Event seemingly the very first tab is selected automatically in addition to the selected tab.
Tried to implement the closing functionality according to the selected tab:
- On closing prior tab to slected tab => index reduced by 1 // WORKS
- On closing following tab to the selected tab => index stays the same, no change // WORKS
- On closing the selected tab => index stays the same, no change // FAILS => automatically selects the very fist tab in addition to the selected tab
I've inspected the source-code of the browser for the failing example (selected and closed Tab#5 => First Tab and Tab#6 selected at the same time) and noticed following:
<div>class="p-tabview-panels"</div> includes all TabPanels
First Tab:
<p-tabpanel class="p-element" ng-reflect-closable="true" ng-reflect-header="Tab 1" ng-reflect-selected="false"><div role="tabpanel" class="p-tabview-panel" id="p-tabpanel-1151" aria-hidden="false" aria-labelledby="p-tabpanel-1151-label"> Content 1 <!--bindings={}--></div><!--bindings={
"ng-reflect-ng-if": "true"
}--></p-tabpanel>
Selected Tab (Tab#6):
<p-tabpanel class="p-element" ng-reflect-closable="true" ng-reflect-header="Tab 6" ng-reflect-selected="true"><div role="tabpanel" class="p-tabview-panel" id="p-tabpanel-5" aria-hidden="false" aria-labelledby="p-tabpanel-5-label"> Content 6 <!--bindings={}--></div><!--bindings={
"ng-reflect-ng-if": "true"
}--></p-tabpanel>
As expected the property ng-reflect-selected is only set for the actually selected tab. So why does the TabView visually selects also the first tab and even the content?!
Environment
Refer to stackblitz example
Reproducer
https://stackblitz.com/edit/github-kfg65h-u7z1qy?file=src/app/app.component.ts
Angular version
11.0.4
PrimeNG version
14.0.0
Build / Runtime
Angular CLI App
Language
TypeScript
Node version (for AoT issues node --version)
12.11.1
Browser(s)
Tested on Chrome, Mozilla, Edge
Steps to reproduce the behavior
- Open Stackblitz example
- Close the active Tab (Tab#5)
- The TabView will display the content of the panel with the activeIndex (Tab#6) plus additionally the first panel
- Inspecting the source code of the browser
=> navigate to
<div>class="p-tabview-panels"</div>=> p-tabPanel for Tab#1 hasng-reflect-selected=false=> p-tabPanel for Tab#6 hasng-reflect-selected=true
Expected behavior
When closing the active tab and setting the corresponding index only the TabPanel with selected-property = true should be displayed
OnTabClose-event should not automatically select the very first tab
This issue has been automatically marked as stale. If this issue is still affecting you with the latest version, please leave any comment, and we will keep it open. We are sorry that we have not been able to prioritize it yet. If you have any new additional information, please include it with your comment!
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you for your understanding!