-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathListItemBase.ts
170 lines (145 loc) · 3.9 KB
/
ListItemBase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import event from "@ui5/webcomponents-base/dist/decorators/event.js";
import type { ITabbable } from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import type { ClassMap } from "@ui5/webcomponents-base/dist/types.js";
import { getTabbableElements } from "@ui5/webcomponents-base/dist/util/TabbableElements.js";
import { isTabNext, isTabPrevious } from "@ui5/webcomponents-base/dist/Keys.js";
import getActiveElement from "@ui5/webcomponents-base/dist/util/getActiveElement.js";
// Styles
import styles from "./generated/themes/ListItemBase.css.js";
import draggableElementStyles from "./generated/themes/DraggableElement.css.js";
/**
* @class
* A class to serve as a foundation
* for the `ListItem` and `ListItemGroupHeader` classes.
* @constructor
* @abstract
* @extends UI5Element
* @public
*/
@customElement({
renderer: litRender,
styles: [styles, draggableElementStyles],
})
@event("_request-tabindex-change")
@event("_focused")
@event("_forward-after")
@event("_forward-before")
class ListItemBase extends UI5Element implements ITabbable {
/**
* Defines the selected state of the component.
* @default false
* @public
*/
@property({ type: Boolean })
selected!: boolean;
/**
* Defines whether the item is movable.
* @default false
* @private
* @since 2.0.0
*/
@property({ type: Boolean })
movable!: boolean;
/**
* Defines if the list item should display its bottom border.
* @private
*/
@property({ type: Boolean })
hasBorder!: boolean;
@property({ defaultValue: "-1", noAttribute: true })
forcedTabIndex!: string;
/**
* Defines whether `ui5-li` is in disabled state.
*
* **Note:** A disabled `ui5-li` is noninteractive.
* @default false
* @protected
* @since 1.0.0-rc.12
*/
@property({ type: Boolean })
disabled!: boolean;
/**
* Indicates if the element is on focus
* @private
*/
@property({ type: Boolean })
focused!: boolean;
_onfocusin(e: FocusEvent) {
this.fireEvent("_request-tabindex-change", e);
if (e.target !== this.getFocusDomRef()) {
return;
}
this.focused = true;
this.fireEvent("_focused", e);
}
_onfocusout() {
this.focused = false;
}
_onkeydown(e: KeyboardEvent) {
if (isTabNext(e)) {
return this._handleTabNext(e);
}
if (isTabPrevious(e)) {
return this._handleTabPrevious(e);
}
}
_onkeyup(e: KeyboardEvent) {} // eslint-disable-line
_handleTabNext(e: KeyboardEvent) {
if (this.shouldForwardTabAfter()) {
if (!this.fireEvent("_forward-after", {}, true)) {
e.preventDefault();
}
}
}
_handleTabPrevious(e: KeyboardEvent) {
const target = e.target as HTMLElement;
if (this.shouldForwardTabBefore(target)) {
this.fireEvent("_forward-before");
}
}
/*
* Determines if th current list item either has no tabbable content or
* [Tab] is performed onto the last tabbale content item.
*/
shouldForwardTabAfter() {
const aContent = getTabbableElements(this.getFocusDomRef()!);
return aContent.length === 0 || (aContent[aContent.length - 1] === getActiveElement());
}
/*
* Determines if the current list item is target of [SHIFT+TAB].
*/
shouldForwardTabBefore(target: HTMLElement) {
return this.getFocusDomRef() === target;
}
get classes(): ClassMap {
return {
main: {
"ui5-li-root": true,
"ui5-li--focusable": this._focusable,
},
};
}
get _ariaDisabled() {
return this.disabled ? true : undefined;
}
get _focusable() {
return !this.disabled;
}
get hasConfigurableMode() {
return false;
}
get _effectiveTabIndex() {
if (!this._focusable) {
return -1;
}
if (this.selected) {
return 0;
}
return this.forcedTabIndex;
}
}
export default ListItemBase;