Skip to content

Commit

Permalink
fix(menu): fixed MenuOptionFactory typings to support `IMenuOptionG…
Browse files Browse the repository at this point in the history
…roup`
  • Loading branch information
DRiFTy17 committed Nov 9, 2023
1 parent ab5c977 commit cf98bab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/lib/menu/menu-adapter.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<IMenuOption | IMenuOptionGroup>): void;
getActiveOptionIndex(): number;
setActiveOption(index: number): void;
activateFirstOption(): void;
Expand Down Expand Up @@ -93,7 +93,7 @@ export class MenuAdapter extends BaseAdapter<IMenuComponent> implements IMenuAda
this._targetElement.setAttribute('aria-controls', `list-dropdown-popup-${config.id}`);
}

public setOptions(options: IMenuOption[]): void {
public setOptions(options: Array<IMenuOption | IMenuOptionGroup>): void {
if (this._listDropdown) {
this._listDropdown.setOptions(options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/menu/menu-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ export interface IMenuOption<T = any> extends IListDropdownOption<T> {
export interface IMenuOptionGroup extends IListDropdownOptionGroup {}

export type MenuOptionBuilder = (option: IMenuOption, parentElement: HTMLElement) => HTMLElement | string | void;
export type MenuOptionFactory = (() => IMenuOption[]) | (() => Promise<IMenuOption[]>);
export type MenuOptionFactory = (() => Array<IMenuOption | IMenuOptionGroup>) | (() => Promise<Array<IMenuOption | IMenuOptionGroup>>);
export type MenuMode = 'click' | 'cascade';
16 changes: 12 additions & 4 deletions src/lib/menu/menu-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,17 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation<IMenuOp
}

private get _flatOptions(): IMenuOption[] {
if (isListDropdownOptionType(this._options, ListDropdownOptionType.Group)) {
return (this._options as IMenuOptionGroup[]).reduce((previousValue, currentValue) => previousValue.concat(currentValue.options), [] as IMenuOption[]) as IMenuOption[];
return this._flattenOptions(this._options);
}

private _flattenOptions(options: Array<IMenuOption | IMenuOptionGroup>): 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 {
Expand Down Expand Up @@ -348,7 +355,8 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation<IMenuOp
return Promise.resolve(this._optionsFactory())
.then(results => {
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) {
Expand Down

0 comments on commit cf98bab

Please sign in to comment.