diff --git a/src/lib/menu/menu-adapter.ts b/src/lib/menu/menu-adapter.ts index 0497adb78..59eb89c95 100644 --- a/src/lib/menu/menu-adapter.ts +++ b/src/lib/menu/menu-adapter.ts @@ -1,7 +1,7 @@ import { BaseAdapter, IBaseAdapter } from '../core/base/base-adapter'; import { IMenuOption } from './index'; import { IMenuComponent } from './menu'; -import { MENU_CONSTANTS, IMenuSelectEventData } from './menu-constants'; +import { MENU_CONSTANTS, IMenuSelectEventData, IMenuOptionGroup } from './menu-constants'; import { IListDropdownConfig, IListDropdown, ListDropdown } from '../list-dropdown'; import { IListComponent, IListItemComponent, LIST_CONSTANTS, LIST_ITEM_CONSTANTS } from '../list'; @@ -14,7 +14,7 @@ export interface IMenuAdapter extends IBaseAdapter { removeTargetListener(event: string, callback: (event: Event) => void): void; attachMenu(config: IListDropdownConfig): void; detachMenu(): void; - setOptions(options: IMenuOption[]): void; + setOptions(options: Array): void; getActiveOptionIndex(): number; setActiveOption(index: number): void; activateFirstOption(): void; @@ -93,7 +93,7 @@ export class MenuAdapter extends BaseAdapter implements IMenuAda this._targetElement.setAttribute('aria-controls', `list-dropdown-popup-${config.id}`); } - public setOptions(options: IMenuOption[]): void { + public setOptions(options: Array): void { if (this._listDropdown) { this._listDropdown.setOptions(options); } diff --git a/src/lib/menu/menu-constants.ts b/src/lib/menu/menu-constants.ts index 320f32a74..6b4244ef0 100644 --- a/src/lib/menu/menu-constants.ts +++ b/src/lib/menu/menu-constants.ts @@ -68,5 +68,5 @@ export interface IMenuOption extends IListDropdownOption { export interface IMenuOptionGroup extends IListDropdownOptionGroup {} export type MenuOptionBuilder = (option: IMenuOption, parentElement: HTMLElement) => HTMLElement | string | void; -export type MenuOptionFactory = (() => IMenuOption[]) | (() => Promise); +export type MenuOptionFactory = (() => Array) | (() => Promise>); export type MenuMode = 'click' | 'cascade'; diff --git a/src/lib/menu/menu-foundation.ts b/src/lib/menu/menu-foundation.ts index ae5ad4cd4..74c4c139a 100644 --- a/src/lib/menu/menu-foundation.ts +++ b/src/lib/menu/menu-foundation.ts @@ -126,10 +126,17 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation previousValue.concat(currentValue.options), [] as IMenuOption[]) as IMenuOption[]; + return this._flattenOptions(this._options); + } + + private _flattenOptions(options: Array): IMenuOption[] { + if (isListDropdownOptionType(options, ListDropdownOptionType.Group)) { + return (options as IMenuOptionGroup[]) + .reduce((previousValue, currentValue) => { + return currentValue.options ? previousValue.concat(currentValue.options) : previousValue; + }, [] as IMenuOption[]); } - return this._options as IMenuOption[]; + return options as IMenuOption[]; } private _onTargetClick(evt: MouseEvent): void { @@ -348,7 +355,8 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation { if (!this._persistSelection) { - results.forEach(o => o.selected = false); + const flatResults = this._flattenOptions(results); + flatResults.filter(o => o.selected).forEach(o => o.selected = false); } if (this._open) {