diff --git a/components/dropdown/dropdown-menu.component.ts b/components/dropdown/dropdown-menu.component.ts index 312d8fb4413..e6393acb00f 100644 --- a/components/dropdown/dropdown-menu.component.ts +++ b/components/dropdown/dropdown-menu.component.ts @@ -46,6 +46,7 @@ export type NzPlacementType = 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 't [@slideMotion]="'enter'" [@.disabled]="noAnimation?.nzNoAnimation" [nzNoAnimation]="noAnimation?.nzNoAnimation" + [attr.id]="id" (mouseenter)="setMouseState(true)" (mouseleave)="setMouseState(false)" > @@ -63,6 +64,7 @@ export class NzDropdownMenuComponent implements AfterContentInit { descendantMenuItemClick$ = this.nzMenuService.descendantMenuItemClick$; nzOverlayClassName: string = ''; nzOverlayStyle: IndexableObject = {}; + id: string | null = null; @ViewChild(TemplateRef, { static: true }) templateRef!: TemplateRef; setMouseState(visible: boolean): void { diff --git a/components/dropdown/dropdown.directive.spec.ts b/components/dropdown/dropdown.directive.spec.ts index addce04db67..e3f72d56015 100644 --- a/components/dropdown/dropdown.directive.spec.ts +++ b/components/dropdown/dropdown.directive.spec.ts @@ -197,6 +197,27 @@ describe('dropdown', () => { fixture.detectChanges(); expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledTimes(1); })); + it('should aria states work', fakeAsync(() => { + const fixture = createComponent(NzTestDropdownComponent, [], []); + fixture.componentInstance.trigger = 'click'; + + fixture.detectChanges(); + const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement; + + expect(dropdownElement.getAttribute('aria-haspopup')).toBe('menu'); + expect(dropdownElement.getAttribute('aria-expanded')).toBe('false'); + expect(dropdownElement.getAttribute('aria-owns')).toBe(null); + + dispatchFakeEvent(dropdownElement, 'click'); + tick(1000); + fixture.detectChanges(); + + const overlayMenu = overlayContainerElement.querySelector('.ant-dropdown')!; + expect(dropdownElement.getAttribute('aria-expanded')).toBe('true'); + const id = dropdownElement.getAttribute('aria-owns'); + expect(id).not.toBe(null); + expect(overlayMenu.getAttribute('id')).toBe(id); + })); }); @Component({ @@ -210,7 +231,8 @@ describe('dropdown', () => { [nzBackdrop]="backdrop" [nzOverlayClassName]="className" [nzOverlayStyle]="overlayStyle" - >Trigger + > + Trigger