Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
feat(menu): Add selected property to menu items (#665)
Browse files Browse the repository at this point in the history
Change as per material-components-web v0.30.0
reference: material-components/material-components-web#2084

Adds `mdc-list-item--selected` class to menu items when `rememberSelection` is set to `true`.

#### New Public methods
- [x] Add `setRememberSelection(rememberSelection: boolean)`
- [x] Add `getSelectedIndex(): number`
- [x] Add `setSelectedIndex(index): void`
- [x] Add `setMaxHeight(height: number): void`

Closes #618
  • Loading branch information
trimox authored Feb 14, 2018
1 parent 7bf1c71 commit 25be411
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/lib/menu/menu-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
MdcMenuItems,
} from './menu';

const MENU_COMPONENTS = [
const MENU_DECLARATIONS = [
MdcMenu,
MdcMenuAnchor,
MdcMenuDivider,
Expand All @@ -19,7 +19,7 @@ const MENU_COMPONENTS = [

@NgModule({
imports: [CommonModule],
exports: [MENU_COMPONENTS],
declarations: [MENU_COMPONENTS],
exports: [MENU_DECLARATIONS],
declarations: [MENU_DECLARATIONS],
})
export class MdcMenuModule { }
33 changes: 32 additions & 1 deletion src/lib/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,21 @@ export class MdcMenu implements AfterViewInit, OnChanges, OnDestroy {
position.top ? this._setStyle(position.top, 'top') : this._removeStyle('top');
position.bottom ? this._setStyle(position.bottom, 'bottom') : this._removeStyle('bottom');
},
setMaxHeight: (height) => {
setMaxHeight: (height: string) => {
this._renderer.setStyle(this.elementRef.nativeElement, 'maxHeight', height);
},
setAttrForOptionAtIndex: (index: number, attr: string, value: string) => {
this._renderer.setAttribute(this.options.toArray()[index].elementRef.nativeElement, attr, value);
},
rmAttrForOptionAtIndex: (index: number, attr: string) => {
this._renderer.removeAttribute(this.options.toArray()[index].elementRef.nativeElement, attr);
},
addClassForOptionAtIndex: (index: number, className: string) => {
this._renderer.addClass(this.options.toArray()[index].elementRef.nativeElement, className);
},
rmClassForOptionAtIndex: (index: number, className: string) => {
this._renderer.removeClass(this.options.toArray()[index].elementRef.nativeElement, className);
}
};

private _foundation: {
Expand All @@ -233,6 +245,9 @@ export class MdcMenu implements AfterViewInit, OnChanges, OnDestroy {
setAnchorCorner(corner: Corner): void,
setAnchorMargin(): void,
setQuickOpen(quickOpen: boolean): void,
setRememberSelection(rememberSelection: boolean): void,
setSelectedIndex(index: number): void,
getSelectedIndex(): number
} = new MDCMenuFoundation(this._mdcAdapter);

constructor(
Expand All @@ -256,6 +271,18 @@ export class MdcMenu implements AfterViewInit, OnChanges, OnDestroy {
}
}

setRememberSelection(rememberSelection: boolean): void {
this._foundation.setRememberSelection(rememberSelection);
}

setSelectedIndex(index: number): void {
this._foundation.setSelectedIndex(index);
}

getSelectedIndex(): number {
return this._foundation.getSelectedIndex();
}

private _setStyle(position: string, anchorPoint: string): void {
this._renderer.setStyle(this.elementRef.nativeElement, anchorPoint, position);
}
Expand Down Expand Up @@ -322,4 +349,8 @@ export class MdcMenu implements AfterViewInit, OnChanges, OnDestroy {
setQuickOpen(quickOpen: boolean): void {
this._foundation.setQuickOpen(quickOpen);
}

setMaxHeight(height: string): void {
this._mdcAdapter.setMaxHeight(height);
}
}
3 changes: 2 additions & 1 deletion src/lib/menu/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular-mdc/web/menu",
"skipTemplateCodegen": true
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
}
6 changes: 6 additions & 0 deletions test/unit/menu/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ describe('MdcMenu', () => {
testDebugElement.nativeElement.click();
fixture.detectChanges();
});

it('#should get selected index', () => {
testInstance.setSelectedIndex(2);
fixture.detectChanges();
expect(testInstance.getSelectedIndex()).toBe(2);
});
});
});

Expand Down

0 comments on commit 25be411

Please sign in to comment.