mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Convert menuItems from Array to Map
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
502a62a173
commit
106cf685bc
@ -35,10 +35,12 @@ export type CatalogAddButtonProps = {
|
|||||||
category: CatalogCategory
|
category: CatalogCategory
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type categoryId = string;
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
||||||
@observable protected isOpen = false;
|
@observable protected isOpen = false;
|
||||||
@observable menuItems: CatalogEntityAddMenu[] = [];
|
@observable menuItems = new Map<categoryId, CatalogEntityAddMenu[]>();
|
||||||
|
|
||||||
constructor(props: CatalogAddButtonProps) {
|
constructor(props: CatalogAddButtonProps) {
|
||||||
super(props);
|
super(props);
|
||||||
@ -60,7 +62,7 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateMenuItems() {
|
updateMenuItems() {
|
||||||
this.menuItems = [];
|
this.menuItems.clear();
|
||||||
|
|
||||||
if (this.props.category) {
|
if (this.props.category) {
|
||||||
this.updateCategoryItems(this.props.category);
|
this.updateCategoryItems(this.props.category);
|
||||||
@ -72,17 +74,22 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
updateCategoryItems = (category: CatalogCategory) => {
|
updateCategoryItems = (category: CatalogCategory) => {
|
||||||
|
if (category instanceof EventEmitter) {
|
||||||
|
const menuItems: CatalogEntityAddMenu[] = [];
|
||||||
const context: CatalogEntityAddMenuContext = {
|
const context: CatalogEntityAddMenuContext = {
|
||||||
navigate: (url: string) => navigate(url),
|
navigate: (url: string) => navigate(url),
|
||||||
menuItems: this.menuItems
|
menuItems
|
||||||
};
|
};
|
||||||
|
|
||||||
if (category instanceof EventEmitter) {
|
|
||||||
category.emit("catalogAddMenu", context);
|
category.emit("catalogAddMenu", context);
|
||||||
this.menuItems = category.filteredItems(this.menuItems);
|
this.menuItems.set(category.getId(), menuItems);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getCategoryFilteredItems = (category: CatalogCategory) => {
|
||||||
|
return category.filteredItems(this.menuItems.get(category.getId()) || []);
|
||||||
|
};
|
||||||
|
|
||||||
@boundMethod
|
@boundMethod
|
||||||
onOpen() {
|
onOpen() {
|
||||||
this.isOpen = true;
|
this.isOpen = true;
|
||||||
@ -95,14 +102,21 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
|||||||
|
|
||||||
@boundMethod
|
@boundMethod
|
||||||
onButtonClick() {
|
onButtonClick() {
|
||||||
const defaultAction = this.menuItems.find(item => item.defaultAction)?.onClick;
|
const defaultAction = this.items.find(item => item.defaultAction)?.onClick;
|
||||||
const clickAction = defaultAction || (this.menuItems.length === 1 ? this.menuItems[0].onClick : null);
|
const clickAction = defaultAction || (this.items.length === 1 ? this.items[0].onClick : null);
|
||||||
|
|
||||||
clickAction?.();
|
clickAction?.();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get items() {
|
||||||
|
const { category } = this.props;
|
||||||
|
|
||||||
|
return category ? this.getCategoryFilteredItems(category) :
|
||||||
|
this.categories.map(this.getCategoryFilteredItems).flat();
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (this.menuItems.length === 0) {
|
if (this.items.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +131,7 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
|||||||
direction="up"
|
direction="up"
|
||||||
onClick={this.onButtonClick}
|
onClick={this.onButtonClick}
|
||||||
>
|
>
|
||||||
{this.menuItems.map((menuItem, index) => {
|
{this.items.map((menuItem, index) => {
|
||||||
return <SpeedDialAction
|
return <SpeedDialAction
|
||||||
key={index}
|
key={index}
|
||||||
icon={<Icon material={menuItem.icon}/>}
|
icon={<Icon material={menuItem.icon}/>}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user