Skip to content

Commit

Permalink
Refactor tab ids
Browse files Browse the repository at this point in the history
  • Loading branch information
jantoun-scottlogic committed Oct 3, 2024
1 parent ef084dc commit 4aa19b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/app/tab/tab-item/tab-item.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
role="tabpanel"
aria-live="off"
[hidden]="!active()"
[id]="title().replaceAll(' ', '-') + '-tabpanel'"
[attr.aria-labelledby]="title().replaceAll(' ', '-') + '-tab'">
[id]="tabIdPrefix + 'Tabpanel'"
[attr.aria-labelledby]="tabIdPrefix + 'Tab'">
<ng-content></ng-content>
</div>
10 changes: 8 additions & 2 deletions src/app/tab/tab-item/tab-item.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { CommonModule } from '@angular/common';
import { Component, effect, EventEmitter, input, model, Output } from '@angular/core';
import { Component, effect, EventEmitter, input, model, OnInit, Output } from '@angular/core';
import { camelCase } from 'lodash-es';

@Component({
selector: 'tab-item',
standalone: true,
imports: [CommonModule],
templateUrl: './tab-item.component.html',
})
export class TabItemComponent {
export class TabItemComponent implements OnInit {
public active = model(false);
public title = input.required<string>();
public tabIdPrefix!: string;
@Output() public tabSelected = new EventEmitter<void>();

constructor() {
Expand All @@ -19,4 +21,8 @@ export class TabItemComponent {
}
});
}

ngOnInit(): void {
this.tabIdPrefix = camelCase(this.title());
}
}
4 changes: 2 additions & 2 deletions src/app/tab/tabs/tabs.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<button
#tabButton
role="tab"
[id]="tab.title().replaceAll(' ', '-') + '-tab'"
[attr.aria-controls]="tab.title().replaceAll(' ', '-') + '-tabpanel'"
[id]="tab.tabIdPrefix + 'Tab'"
[attr.aria-controls]="tab.tabIdPrefix + 'Tabpanel'"
[class.tce-active-tab]="tab.active()"
[tabindex]="tab.active() ? 0 : -1"
[attr.aria-selected]="tab.active() ? true : false"
Expand Down

0 comments on commit 4aa19b6

Please sign in to comment.