angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.37k stars 6.75k forks source link

Feature Request : Mat-Tab select by name. #11858

Open AngularAnt opened 6 years ago

AngularAnt commented 6 years ago

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 commented 5 years ago

Is there any updates to this feature request?

webia1 commented 5 years ago

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.

FiringBlanks commented 5 years ago

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.

devversion commented 4 years ago

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)

LanderBeeuwsaert commented 4 years ago

For info: My workaround for this is:

@ViewChildren(MatTab) tabs: QueryList;

  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;
angular-robot[bot] commented 2 years ago

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] commented 2 years ago

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.

ssparasuram commented 10 months ago

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); } }