vertical-nav.component.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { Component, Input, Output, EventEmitter, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. export interface NavItem {
  4. id: 'progress' | 'members' | 'files';
  5. name: string;
  6. icon?: string;
  7. }
  8. @Component({
  9. selector: 'app-vertical-nav',
  10. standalone: true,
  11. imports: [CommonModule],
  12. templateUrl: './vertical-nav.component.html',
  13. styleUrls: ['./vertical-nav.component.scss']
  14. })
  15. export class VerticalNavComponent implements AfterViewInit {
  16. @Input() activeTab: 'progress' | 'members' | 'files' = 'progress';
  17. @Input() navItems: NavItem[] = [
  18. { id: 'progress', name: '项目进度' },
  19. { id: 'members', name: '项目人员' },
  20. { id: 'files', name: '项目文件' }
  21. ];
  22. @Output() tabChange = new EventEmitter<'progress' | 'members' | 'files'>();
  23. @ViewChild('tabContainer', { static: false }) tabContainer!: ElementRef;
  24. private tabWidth = 120; // 默认选项卡宽度
  25. ngAfterViewInit(): void {
  26. // 动态计算选项卡宽度
  27. this.calculateTabWidth();
  28. }
  29. onTabClick(tabId: 'progress' | 'members' | 'files'): void {
  30. this.tabChange.emit(tabId);
  31. }
  32. isActive(tabId: 'progress' | 'members' | 'files'): boolean {
  33. return this.activeTab === tabId;
  34. }
  35. // 计算滑动指示器的位置
  36. getIndicatorPosition(): number {
  37. const activeIndex = this.navItems.findIndex(item => item.id === this.activeTab);
  38. return activeIndex * this.tabWidth;
  39. }
  40. // 动态计算选项卡宽度
  41. private calculateTabWidth(): void {
  42. if (this.tabContainer) {
  43. const containerWidth = this.tabContainer.nativeElement.offsetWidth;
  44. this.tabWidth = (containerWidth - 8) / this.navItems.length; // 减去padding
  45. }
  46. }
  47. }