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

chore: use fireDecoratorEvent over fireEvent #10247

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 28 additions & 13 deletions packages/main/src/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ type MenuBeforeCloseEventDetail = { escPressed: boolean };
type: String,
},
},
cancelable: true,
})

/**
* Fired before the menu is opened. This event can be cancelled, which will prevent the menu from opening. **This event does not bubble.**
* Fired before the menu is opened. This event can be cancelled, which will prevent the menu from opening.
*
* **Note:** Since 1.14.0 the event is also fired before a sub-menu opens.
* @public
Expand All @@ -153,17 +154,29 @@ type MenuBeforeCloseEventDetail = { escPressed: boolean };
type: HTMLElement,
},
},
bubbles: true,
cancelable: true,
})

/**
* Fired after the menu is opened. **This event does not bubble.**
* Fired after the menu is opened.
* @public
* @since 1.10.0
*/
@event("open")
@event("open", {
bubbles: true,
})

/**
* Fired when the menu is being closed.
* @private
*/
@event("close-menu", {
bubbles: true,
})

/**
* Fired before the menu is closed. This event can be cancelled, which will prevent the menu from closing. **This event does not bubble.**
* Fired before the menu is closed. This event can be cancelled, which will prevent the menu from closing.
* @public
* @allowPreventDefault
* @param {boolean} escPressed Indicates that `ESC` key has triggered the event.
Expand All @@ -178,10 +191,12 @@ type MenuBeforeCloseEventDetail = { escPressed: boolean };
type: Boolean,
},
},
bubbles: true,
cancelable: true,
})

/**
* Fired after the menu is closed. **This event does not bubble.**
* Fired after the menu is closed.
* @public
* @since 1.10.0
*/
Expand Down Expand Up @@ -290,9 +305,9 @@ class Menu extends UI5Element {
return;
}

this.fireEvent<MenuBeforeOpenEventDetail>("before-open", {
this.fireDecoratorEvent<MenuBeforeOpenEventDetail>("before-open", {
item,
}, false, false);
});
item._popover.opener = item;
item._popover.open = true;
item.selected = true;
Expand Down Expand Up @@ -342,13 +357,13 @@ class Menu extends UI5Element {
const item = e.detail.item as MenuItem;

if (!item._popover) {
const prevented = !this.fireEvent<MenuItemClickEventDetail>("item-click", {
const prevented = !this.fireDecoratorEvent<MenuItemClickEventDetail>("item-click", {
"item": item,
"text": item.text || "",
}, true, false);
});

if (!prevented && this._popover) {
item.fireEvent("close-menu", {});
item.fireDecoratorEvent("close-menu", {});
}
} else {
this._openItemSubMenu(item);
Expand Down Expand Up @@ -378,7 +393,7 @@ class Menu extends UI5Element {
}

_beforePopoverOpen(e: CustomEvent) {
const prevented = !this.fireEvent<MenuBeforeOpenEventDetail>("before-open", {}, true, true);
const prevented = !this.fireDecoratorEvent<MenuBeforeOpenEventDetail>("before-open", {});

if (prevented) {
this.open = false;
Expand All @@ -388,11 +403,11 @@ class Menu extends UI5Element {

_afterPopoverOpen() {
this._menuItems[0]?.focus();
this.fireEvent("open", {}, false, true);
this.fireDecoratorEvent("open", {});
}

_beforePopoverClose(e: CustomEvent<ResponsivePopoverBeforeCloseEventDetail>) {
const prevented = !this.fireEvent<MenuBeforeCloseEventDetail>("before-close", { escPressed: e.detail.escPressed }, true, true);
const prevented = !this.fireDecoratorEvent<MenuBeforeCloseEventDetail>("before-close", { escPressed: e.detail.escPressed });

if (prevented) {
this.open = true;
Expand Down
75 changes: 69 additions & 6 deletions packages/main/src/MenuItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
import event from "@ui5/webcomponents-base/dist/decorators/event.js";
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
import AriaHasPopup from "@ui5/webcomponents-base/dist/types/AriaHasPopup.js";
Expand Down Expand Up @@ -60,6 +61,68 @@ type MenuItemAccessibilityAttributes = Pick<AccessibilityAttributes, "ariaKeySho
styles: [ListItem.styles, menuItemCss],
dependencies: [...ListItem.dependencies, ResponsivePopover, List, BusyIndicator, Icon],
})

/**
* Fired before the menu is opened. This event can be cancelled, which will prevent the menu from opening.
*
* **Note:** Since 1.14.0 the event is also fired before a sub-menu opens.
* @public
* @allowPreventDefault
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
* @since 1.10.0
* @param { HTMLElement } item The `ui5-menu-item` that triggers opening of the sub-menu or undefined when fired upon root menu opening.
*/
@event<MenuBeforeOpenEventDetail>("before-open", {
detail: {
/**
* @public
* @since 1.14.0
*/
item: {
type: HTMLElement,
},
},
cancelable: true,
})

/**
* Fired after the menu is opened.
* @public
*/
@event("open")

/**
* Fired when the menu is being closed.
* @private
*/
@event("close-menu", {
bubbles: true,
})

/**
* Fired before the menu is closed. This event can be cancelled, which will prevent the menu from closing.
* @public
* @allowPreventDefault
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} escPressed Indicates that `ESC` key has triggered the event.
* @since 1.10.0
*/
@event<MenuBeforeCloseEventDetail>("before-close", {
detail: {
/**
* @public
*/
escPressed: {
type: Boolean,
},
},
cancelable: true,
})

/**
* Fired after the menu is closed.
* @public
* @since 1.10.0
*/
@event("close")
class MenuItem extends ListItem implements IMenuItem {
/**
* Defines the text of the tree item.
Expand Down Expand Up @@ -289,7 +352,7 @@ class MenuItem extends ListItem implements IMenuItem {
this._popover.open = false;
}
this.selected = false;
this.fireEvent("close-menu", {});
this.fireDecoratorEvent("close-menu", {});
}

_close() {
Expand All @@ -300,7 +363,7 @@ class MenuItem extends ListItem implements IMenuItem {
}

_beforePopoverOpen(e: CustomEvent) {
const prevented = !this.fireEvent<MenuBeforeOpenEventDetail>("before-open", {}, true, false);
const prevented = !this.fireDecoratorEvent<MenuBeforeOpenEventDetail>("before-open", {});

if (prevented) {
e.preventDefault();
Expand All @@ -309,11 +372,11 @@ class MenuItem extends ListItem implements IMenuItem {

_afterPopoverOpen() {
this.items[0]?.focus();
this.fireEvent("open", {}, false, false);
this.fireDecoratorEvent("open");
}

_beforePopoverClose(e: CustomEvent<ResponsivePopoverBeforeCloseEventDetail>) {
const prevented = !this.fireEvent<MenuBeforeCloseEventDetail>("before-close", { escPressed: e.detail.escPressed }, true, false);
const prevented = !this.fireDecoratorEvent<MenuBeforeCloseEventDetail>("before-close", { escPressed: e.detail.escPressed });

if (prevented) {
e.preventDefault();
Expand All @@ -324,13 +387,13 @@ class MenuItem extends ListItem implements IMenuItem {
if (e.detail.escPressed) {
this.focus();
if (isPhone()) {
this.fireEvent("close-menu", {});
this.fireDecoratorEvent("close-menu", {});
}
}
}

_afterPopoverClose() {
this.fireEvent("close", {}, false, false);
this.fireDecoratorEvent("close", {});
}
}

Expand Down