components icon indicating copy to clipboard operation
components copied to clipboard

[Tabs] Allow users to assign IDs to tabs

Open jefersonestevo opened this issue 7 years ago • 28 comments

Bug, feature request, or proposal:

proposal

What is the expected behavior?

Let the user define some "id" for the tab, to identify, outside the tab container, which tab is currently selected. Please note that this id may not necessarily be a substitute to the "selectedIndex" property, but some aditional content to identify which tab is currently selected.

What is the current behavior?

Today we can only know which tab is selected using the selectedIndex of md-tab-group, which is not really safe as the content of the tab may be changed and the index changes within it

What are the steps to reproduce?

Plunker

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

This is linked to #4135. The "workaround" that I found to use a "fixed" content relative to the current selected tab is to render the fixed element outside the tab-group and use the selectedIndex to show it.

Which versions of Angular, Material, OS, browsers are affected?

2.0.0-beta.3

jefersonestevo avatar Apr 18 '17 13:04 jefersonestevo

This would be great to have that because if you protect your tab with ngIf the index is not always the Same.

The id property should be provided in the current available events.

elvirdolic avatar Apr 18 '17 17:04 elvirdolic

This would definitely be useful when programmatically setting the tab.

wulfsberg avatar Apr 18 '17 20:04 wulfsberg

i also want set the tab programmatically +1

finalxcode avatar Aug 14 '17 09:08 finalxcode

@finalxcode I am not saying it is a great solution but I have been programmatically selecting based on index.

tmburnell avatar Nov 01 '17 20:11 tmburnell

I need this for e2e Tests

FoxelFox avatar Jul 02 '18 08:07 FoxelFox

We need this because we have cases where some tabs are hidden with *ngIf, so the index is not reliable.

jackalvrus avatar Jan 23 '19 13:01 jackalvrus

Very valid request! Its really needed!

mohyeid avatar Apr 17 '19 02:04 mohyeid

any update guys?. I need this feature. it helps us to fix the e2e pipeline

dhrn avatar Oct 21 '19 10:10 dhrn

Is there any progress on this feature request?

estoel81 avatar Jan 02 '20 12:01 estoel81

e2e testing is a nightmare because of this. I feel like I'm better off not using material components.

donders avatar Apr 02 '20 08:04 donders

This issue is already 3 years old. Common guys, just make it happen already...

androdel avatar Apr 20 '20 10:04 androdel

Would be nice indeed to be able to add some, for example, data-testid attributes to the DOM

hansregeer avatar Nov 17 '20 16:11 hansregeer

Yeah, we need to be able to add some sort of attributes here, just ran into this now...

tidusjar avatar Mar 17 '21 21:03 tidusjar

Letting developers set an id for tab would be great. IMHO, it should be an id, not some data-tabid or other custom attribute, as it might not fit all use cases. And "rewriting the default AM id" practice exists already for other components (like select). In our case, for Tag Manager triggers, having an id on a mat-tab would be ideal.

sovushka-utrom avatar Apr 23 '21 09:04 sovushka-utrom

While a bit hacky, the label property of mat tab can be used as ID that can be accessed in the MatTabChangeEvent as event.tab.textLabel

The actual visible label can be set using ng-template with mat-tab-label directive.

RuneSP avatar May 27 '21 09:05 RuneSP

While a bit hacky, the label property of mat tab can be used as ID that can be accessed in the MatTabChangeEvent as event.tab.textLabel

The actual visible label can be set using ng-template with mat-tab-label directive.

Thanks for the tip, this works fine Just adding the sample:

component.html

    <!-- label are used to identify tab in component.ts, actual text is overwritten by mat-tab-label -->
    <mat-tab class="tab-content" [label]="'search'">
      <ng-template mat-tab-label>
        My Tab Label
      </ng-template>
    </mat-tab>

component.ts

    tabChange(tabChange: MatTabChangeEvent): void {
        /* IF tab is search
         * We are forced to filter on textLabel because index is not reliable
         * (it changes if user does not have access to other tabs for example) */
        if (tabChange?.tab?.textLabel === 'search') {

bmtheo avatar Apr 04 '22 13:04 bmtheo

    <!-- label are used to identify tab in component.ts, actual text is overwritten by mat-tab-label -->
    <mat-tab class="tab-content" [label]="'search'">
      <ng-template mat-tab-label>
        My Tab Label
      </ng-template>
    </mat-tab>

@bmtheo I used this for a while thank you, but it seems to be broken with ng15. I tinkered for an hour, but didn't really get anywhere.

burner avatar Dec 08 '22 11:12 burner

It's 2023 and there is still no answer for this that won't break on version updates?

ptletski avatar Jan 09 '23 13:01 ptletski

I was able to get the attribute 'aria-label' to give to mat-tab. I had to use this attr like id. [aria-label]="childSection.id" Then i got element from document with query selector.

bsogulcan avatar Apr 09 '23 23:04 bsogulcan

Hello, this issue just landed on my radar and before I go ahead and rush into implementing this I wanted to get a better idea of what the desired use case is. For example, if the id is only were only for testing purposes (I'm aware it's not) I'd recommend using component harnesses which would provide a much better experience

Stackblitz examples of what is trying to be accomplished would also be very much appreciated! It would give me something tangible to really test against

wagnermaciel avatar Apr 12 '23 16:04 wagnermaciel

The most obvious use case is probably to be able to link to a specific tab, say, using a URI #fragment. (Or various similar cases, e.g. storing the tab ID in localstorage and being able to recreate the UI when the user returns).

wulfsberg avatar Apr 13 '23 08:04 wulfsberg

Back when i commented in 2017 (when i commented). Here was my use-case:

I had data ... that on selection based on an attribute in the data i wanted to default to different tabs. For example record one might go to the first tab, record two might go to the third tab, and the third to the second.
As i stated I needed up using indexes to control this. This logic fell short when i started using dynamic tabs and could not guarantee the indexes.
I did make functions that could look at the current tabs and translated it to the correct index (but seemed like a lot of overhead).

Since a lot of time has passed i have started tying my tabs to routes, so i can use active route. This does allow for a more dynamic loading.

But still would be nice if no routing was used to be able to dynamically select the tab based on a key and not an index.

tmburnell avatar Apr 13 '23 12:04 tmburnell

Literally need this at my job. I am working on a change for this and pushing a PR soon enough.

Edit (6/6/2023):

After looking at the source code:

  • https://github.com/angular/components/blob/main/src/material/tabs/tab-group.ts#L441
  • https://github.com/angular/components/blob/main/src/material/tabs/tab-group.html#L14

it seems like setting the id of the tab was not exposed on purpose in order to ensure uniqueness when multiple tab group instances are rendered on any given page. Although that makes complete sense, i do think there should be override options at the user's/developer's risk.

I'm working on a PR in case there is still a need for it.

ryanwaite28 avatar Jun 07 '23 17:06 ryanwaite28

How do i get permissions to push a PR?

ryanwaite28 avatar Jun 07 '23 20:06 ryanwaite28

I've got a solution on how to add the IDs programmatically, use inspection mode to see the IDs set

Hope someone finds this useful

  • https://stackblitz.com/edit/kingsbury-angular-w-material-c8jswn?file=src%2Fapp%2Fcustomers%2Fcustomers.component.ts

DaveWillie avatar Jun 27 '23 10:06 DaveWillie

any progress on this one

burner avatar Dec 11 '23 12:12 burner

Dear Angular team, please provide a solution for this. It'a huge issue for using dynamic tabs - requiring strange workarounds or making it even impossible to use material tabs. Please fix!

tvollstaedt avatar Dec 20 '23 10:12 tvollstaedt

useful for e2e tests

sainture avatar May 01 '24 04:05 sainture