Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:menu): fix dropdown menu item selected className #2434

Merged
merged 1 commit into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
}