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(menu): fixed MenuOptionFactory typings to support IMenuOptionGroup #428

Merged
merged 2 commits into from
Nov 27, 2023
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
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
2 changes: 1 addition & 1 deletion src/stories/src/components/menu/menu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ type MenuOptionBuilder = (option: IMenuOption, parentElement: HTMLElement) => HT
### MenuOptionFactory

```typescript
type MenuOptionFactory = (() => IMenuOption[]) | (() => Promise<IMenuOption[]>);
type MenuOptionFactory = (() => Array<IMenuOption | IMenuOptionGroup>) | (() => Promise<Array<IMenuOption | IMenuOptionGroup>>);
```

### MenuMode
Expand Down