-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathdropdown-group.tsx
170 lines (145 loc) · 4.46 KB
/
dropdown-group.tsx
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 {
Component,
Element,
Event,
EventEmitter,
h,
Host,
Listen,
Prop,
VNode,
Watch,
} from "@stencil/core";
import { Scale, SelectionMode } from "../interfaces";
import { createObserver } from "../../utils/observers";
import { CSS } from "../dropdown-item/resources";
import { RequestedItem } from "./interfaces";
/**
* @slot - A slot for adding `calcite-dropdown-item`s.
*/
@Component({
tag: "calcite-dropdown-group",
styleUrl: "dropdown-group.scss",
shadow: {
delegatesFocus: true,
},
})
export class DropdownGroup {
//--------------------------------------------------------------------------
//
// Public Properties
//
//--------------------------------------------------------------------------
/** Specifies and displays a group title. */
@Prop({ reflect: true }) groupTitle: string;
/**
* Specifies the size of the component inherited from the parent `calcite-dropdown`, defaults to `m`.
*
* @internal
*/
@Prop({ reflect: true }) scale: Scale = "m";
/**
* Specifies the selection mode of the component, where:
*
* `"multiple"` allows any number of selections,
*
* `"single"` allows only one selection, and
*
* `"none"` does not allow any selections.
*/
@Prop({ reflect: true }) selectionMode: Extract<"none" | "single" | "multiple", SelectionMode> =
"single";
@Watch("selectionMode")
handlePropsChange(): void {
this.updateItems();
}
//--------------------------------------------------------------------------
//
// Events
//
//--------------------------------------------------------------------------
/**
* @internal
*/
@Event({ cancelable: false }) calciteInternalDropdownItemChange: EventEmitter<RequestedItem>;
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback(): void {
this.updateItems();
this.mutationObserver?.observe(this.el, { childList: true });
}
componentWillLoad(): void {
this.groupPosition = this.getGroupPosition();
}
disconnectedCallback(): void {
this.mutationObserver?.disconnect();
}
render(): VNode {
const groupTitle = this.groupTitle ? (
<span aria-hidden="true" class="dropdown-title">
{this.groupTitle}
</span>
) : null;
const dropdownSeparator =
this.groupPosition > 0 ? <div class="dropdown-separator" role="separator" /> : null;
return (
<Host aria-label={this.groupTitle} role="group">
<div
class={{
[CSS.container]: true,
}}
>
{dropdownSeparator}
{groupTitle}
<slot />
</div>
</Host>
);
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
@Listen("calciteInternalDropdownItemSelect")
updateActiveItemOnChange(event: CustomEvent): void {
this.requestedDropdownGroup = event.detail.requestedDropdownGroup;
this.requestedDropdownItem = event.detail.requestedDropdownItem;
this.calciteInternalDropdownItemChange.emit({
requestedDropdownGroup: this.requestedDropdownGroup,
requestedDropdownItem: this.requestedDropdownItem,
});
}
//--------------------------------------------------------------------------
//
// Private State/Props
//
//--------------------------------------------------------------------------
@Element() el: HTMLCalciteDropdownGroupElement;
/** position of group within a dropdown */
private groupPosition: number;
/** the requested group */
private requestedDropdownGroup: HTMLCalciteDropdownGroupElement;
/** the requested item */
private requestedDropdownItem: HTMLCalciteDropdownItemElement;
private updateItems = (): void => {
Array.from(this.el.querySelectorAll("calcite-dropdown-item")).forEach(
(item) => (item.selectionMode = this.selectionMode),
);
};
mutationObserver = createObserver("mutation", () => this.updateItems());
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
private getGroupPosition(): number {
return Array.prototype.indexOf.call(
this.el.parentElement.querySelectorAll("calcite-dropdown-group"),
this.el,
);
}
}