components icon indicating copy to clipboard operation
components copied to clipboard

Feature Request : Mat-Tab select by name.

Open AngularAnt opened this issue 6 years ago • 8 comments

Bug, feature request, or proposal:

Feature Request for material tabs

What is the expected behavior?

The ability to select a tab by its name, like this;

<mat-tab-group [selectedTab]="profile" >

<mat-tab label="Profile" name="profile">

What is the current behavior?

<mat-tab-group [selectedIndex]="4" >

What is the use-case or motivation for changing an existing behavior?

I am using the material tabs in a responsive app. When the app is in mobile size I display 5 tabs, when the app is in desktop size I display 3 tabs. The reason is since there is more room on the desktop I combine the content and show less tabs.

The issue is when I try to link to a specific tab via a URL. I would use a query parameter like ?tabindex=2. However since my tab index changes depending on the application size, the link will work in one case and not the other. So it would be useful to link to the tab by name like this, ?tab=profile. This way I can pass the name to the component and it can figure out the active tab regardless of the index number.

AngularAnt avatar Jun 20 '18 13:06 AngularAnt

Is there any updates to this feature request?

AngularAnt avatar Mar 29 '19 20:03 AngularAnt

Use case in an online game: Certain tab contents appear in different tab orders, i.e. same content = same tab but different indexes. In this case identifying of a certain tab would only work by name. That is not a nice to have feature, that's an essential feature.

webia1 avatar May 12 '19 19:05 webia1

This is an important feature. The index of dynamically added tabs by other users means nothing, the name is the appropriate way of selecting a specific tag.

FiringBlanks avatar Aug 17 '19 15:08 FiringBlanks

I'd have said that the current solution is to just determine the tab index based on the label, but it looks like that doesn't work very well as:

  1. We don't expose _tabs as public API (the internal variable that determines the tab indices)
  2. We don't have a construct of assigning a unique id to a mat-tab. A label is not always computable as it can be an arbitrary template rendered (i.e. @Input() label can be empty)

I think this feature is reasonable, but it seems like it would require a proper design as it would either be a breaking change and we'd require tabs to have a unique name, or we'd support both indices and an optional name (with better backwards compatibility)

devversion avatar May 28 '20 19:05 devversion

For info: My workaround for this is:

@ViewChildren(MatTab) tabs: QueryList<MatTab>;

  let tab = this.tabs.toArray().find((tabItem: MatTab) => tabItem.textLabel === tabName);
  if (tab && !tab.disabled) this.selectedTabIndex = this.tabs.toArray().indexOf(tab);

this.tabGroup.selectedIndexChange.subscribe((index: number) => {
  this.currentTabName = this.tabs.toArray()[index].textLabel;

LanderBeeuwsaert avatar May 28 '20 19:05 LanderBeeuwsaert

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

angular-robot[bot] avatar Feb 01 '22 18:02 angular-robot[bot]

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

angular-robot[bot] avatar Feb 22 '22 15:02 angular-robot[bot]

with the help of @LanderBeeuwsaert suggestion , As a workaround, I did as below. @ViewChild('tabGroup') tabGroup! : MatTabGroup;

ngAfterViewInit() { let tab = this.tabGroup._allTabs.toArray().find((tabItem: MatTab) => tabItem.textLabel === tabName); if (tab && !tab.disabled) { this.tabGroup.selectedIndex = this.tabGroup._allTabs.toArray().indexOf(tab); } }

ssparasuram avatar Dec 20 '23 09:12 ssparasuram