Skip to content

Commit

Permalink
fix(module:menu): fix dropdown menu item selected className (#2434)
Browse files Browse the repository at this point in the history
close #2433
  • Loading branch information
LiadIdan authored and vthinkxie committed Nov 30, 2018
1 parent 1c7f983 commit acbe5da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 10 additions & 1 deletion components/dropdown/nz-dropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ describe('dropdown', () => {
expect(items[ 0 ].classList.contains('ant-dropdown-menu-item')).toBe(true);
expect(items[ 0 ].classList.contains('ant-dropdown-menu-item-selected')).toBe(false);
});
it('should append the correct className', () => {
testComponent.visible = true;
testComponent.itemSelected = true;
fixture.detectChanges();
const items = overlayContainerElement.querySelectorAll('.ant-dropdown-menu-item') as NodeListOf<HTMLElement>;
expect(items[ 0 ].classList.contains('.ant-menu-item-selected')).toBe(false);
expect(items[ 0 ].classList.contains('ant-dropdown-menu-item-selected')).toBe(true);
});
it('should backdrop work with click', () => {
testComponent.trigger = 'click';
fixture.detectChanges();
Expand Down Expand Up @@ -390,7 +398,7 @@ describe('dropdown', () => {
Hover me <i nz-icon type="down"></i>
</a>
<ul nz-menu [nzSelectable]="selectable">
<li nz-menu-item>
<li nz-menu-item [nzSelected]="itemSelected">
<a>1st menu item</a>
</li>
<li nz-menu-item>
Expand All @@ -413,6 +421,7 @@ export class NzTestDropdownComponent {
@ViewChild(NzSubMenuComponent) nzSubMenuComponent: NzSubMenuComponent;
visible = false;
selectable = true;
itemSelected = false;
trigger = 'hover';
placement = 'bottomLeft';
disabled = false;
Expand Down
17 changes: 13 additions & 4 deletions components/menu/nz-menu-item.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { NzSubMenuComponent } from './nz-submenu.component';
export class NzMenuItemDirective implements OnInit {
private _disabled = false;
private _selected = false;
private _initialized = false;
level = 0;
padding = null;
isInDropDown = false;
Expand All @@ -37,10 +38,8 @@ export class NzMenuItemDirective implements OnInit {
@Input()
set nzSelected(value: boolean) {
this._selected = toBoolean(value);
if (this._selected) {
this.renderer.addClass(this.hostElement.nativeElement, this.isInDropDown ? 'ant-dropdown-menu-item-selected' : 'ant-menu-item-selected');
} else {
this.renderer.removeClass(this.hostElement.nativeElement, this.isInDropDown ? 'ant-dropdown-menu-item-selected' : 'ant-menu-item-selected');
if (this._initialized) {
this.setClass();
}
}

Expand Down Expand Up @@ -112,5 +111,15 @@ export class NzMenuItemDirective implements OnInit {
this.padding = parseInt(this.hostElement.nativeElement.style[ 'padding-left' ], 10);
}
this.isInDropDown = this.nzMenuDirective.nzInDropDown;
this.setClass();
this._initialized = true;
}

setClass(): void {
if (this._selected) {
this.renderer.addClass(this.hostElement.nativeElement, this.isInDropDown ? 'ant-dropdown-menu-item-selected' : 'ant-menu-item-selected');
} else {
this.renderer.removeClass(this.hostElement.nativeElement, this.isInDropDown ? 'ant-dropdown-menu-item-selected' : 'ant-menu-item-selected');
}
}
}

0 comments on commit acbe5da

Please sign in to comment.