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

POC: selected state in list items #10345

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions packages/main/src/ListItemBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class ListItemBase extends UI5Element implements ITabbable {

onBeforeRendering(): void {
this.actionable = true;

this._toggleSelectedState();
}

_onfocusin(e: FocusEvent) {
Expand All @@ -133,6 +135,14 @@ class ListItemBase extends UI5Element implements ITabbable {
this.fireDecoratorEvent("_focused", e);
}

_toggleSelectedState() {
if (this.selected) {
this._internals.states.add("selected");
} else {
this._internals.states.delete("selected");
}
}

_onkeydown(e: KeyboardEvent) {
if (isTabNext(e)) {
return this._handleTabNext(e);
Expand Down
9 changes: 3 additions & 6 deletions packages/main/src/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ class Menu extends UI5Element {
this.fireEvent<MenuBeforeOpenEventDetail>("before-open", {
item,
}, false, false);
item._popover.opener = item;
item._popover.open = true;
item.selected = true;
item.open = true;
}

_closeItemSubMenu(item: MenuItem) {
Expand All @@ -279,8 +277,7 @@ class Menu extends UI5Element {
this._closeItemSubMenu(openedSibling);
}

item._popover.open = false;
item.selected = false;
item.open = false;
}
}

Expand Down Expand Up @@ -356,7 +353,7 @@ class Menu extends UI5Element {
if (shouldOpenMenu) {
this._openItemSubMenu(item);
} else if (shouldCloseMenu && parentElement.hasAttribute("ui5-menu-item") && parentElement._popover) {
parentElement._popover.open = false;
parentElement.open = false;
parentElement.selected = false;
(parentElement._popover.opener as HTMLElement)?.focus();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/MenuItem.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
prevent-initial-focus
prevent-focus-restore
hide-arrow
.opener={{_opener}}
?open={{open}}
allow-target-overlap
sub-menu
placement={{placement}}
Expand Down
33 changes: 26 additions & 7 deletions packages/main/src/MenuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ class MenuItem extends ListItem implements IMenuItem {
@property({ type: Boolean })
disabled = false;

/**
* Indicates if the menu is open
* @public
* @default false
* @since 1.10.0
*/
@property({ type: Boolean })
open = false;

/**
* Defines the delay in milliseconds, after which the loading indicator will be displayed inside the corresponding ui5-menu popover.
*
Expand Down Expand Up @@ -255,13 +264,28 @@ class MenuItem extends ListItem implements IMenuItem {
}

onBeforeRendering() {
// super.onBeforeRendering();
this._toggleSelectedState();

const siblingsWithIcon = this._menuItems.some(menuItem => !!menuItem.icon);

this._menuItems.forEach(item => {
item._siblingsWithIcon = siblingsWithIcon;
});
}

get _opener() {
return this.getDomRef();
}

_toggleSelectedState() {
if (this.open) {
this._internals.states.add("selected");
} else {
this._internals.states.delete("selected");
}
}

async focus(focusOptions?: FocusOptions): Promise<void> {
await renderFinished();

Expand Down Expand Up @@ -296,17 +320,12 @@ class MenuItem extends ListItem implements IMenuItem {
}

_closeAll() {
if (this._popover) {
this._popover.open = false;
}
this.selected = false;
this.open = false;
this.fireEvent("close-menu", {});
}

_close() {
if (this._popover) {
this._popover.open = false;
}
this.open = false;
this.selected = false;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/main/src/themes/ListItemBase.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}

/* selected */
:host([selected]) {
:host(:state(selected)) {
background-color: var(--sapList_SelectionBackgroundColor);
border-bottom: var(--ui5-listitem-selected-border-bottom);

Expand All @@ -34,7 +34,7 @@
}

/* hovered */
:host([actionable]:not([active]):not([selected]):not([ui5-li-group-header]):hover) {
:host([actionable]:not([active]):not(:state(selected)):not([ui5-li-group-header]):hover) {
background-color: var(--sapList_Hover_Background);

.ui5-li-additional-text {
Expand All @@ -43,13 +43,13 @@
}

/* selected and hovered */
:host([actionable][selected]:not([active], [data-moving]):hover) {
:host([actionable]:state(selected):not([active], [data-moving]):hover) {
background-color: var(--sapList_Hover_SelectionBackground);
}

/* selected and active */
:host([active][actionable]:not([data-moving])),
:host([active][actionable][selected]:not([data-moving])) {
:host([active][actionable]:state(selected):not([data-moving])) {
background-color: var(--sapList_Active_Background);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/themes/MenuItem.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}

/* hovered and active */
:host([disabled][actionable]:not([active]):not([selected]):hover),
:host([disabled][actionable]:not([active]):not(:state(selected)):hover),
:host([disabled][active][actionable]) {
background: var(--ui5-listitem-background-color);
}
Expand All @@ -30,7 +30,7 @@
background-color: var(--sapList_Active_Background);
}

:host(:not([active]):not([selected]):not([disabled]):hover) {
:host(:not([active]):not(:state(selected)):not([disabled]):hover) {
background-color: var(--sapList_Hover_Background);
}

Expand Down
Loading