-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbutton-group.ts
More file actions
246 lines (211 loc) · 6.69 KB
/
button-group.ts
File metadata and controls
246 lines (211 loc) · 6.69 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { LitElement, html } from 'lit';
import { property, queryAssignedElements } from 'lit/decorators.js';
import { themes } from '../../theming/theming-decorator.js';
import {
type MutationControllerParams,
createMutationController,
} from '../common/controllers/mutation-observer.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { findElementFromEventPath, last } from '../common/util.js';
import type { ButtonGroupSelection, ContentOrientation } from '../types.js';
import { styles } from './themes/group.base.css.js';
import { all } from './themes/group.js';
import { styles as shared } from './themes/shared/group/group.common.css.js';
import IgcToggleButtonComponent from './toggle-button.js';
export interface IgcButtonGroupComponentEventMap {
igcSelect: CustomEvent<string | undefined>;
igcDeselect: CustomEvent<string | undefined>;
}
/* blazorAdditionalDependency: IgcToggleButtonComponent */
/**
* The `igc-button-group` groups a series of `igc-toggle-button`s together, exposing features such as layout and selection.
*
* @element igc-button-group
*
* @slot - Renders `igc-toggle-button` component.
*
* @fires igcSelect - Emitted when a button is selected through user interaction.
* @fires igcDeselect - Emitted when a button is deselected through user interaction.
*
* @csspart group - The button group container.
*/
@themes(all)
export default class IgcButtonGroupComponent extends EventEmitterMixin<
IgcButtonGroupComponentEventMap,
Constructor<LitElement>
>(LitElement) {
public static readonly tagName = 'igc-button-group';
public static styles = [styles, shared];
/* blazorSuppress */
public static register() {
registerComponent(IgcButtonGroupComponent, IgcToggleButtonComponent);
}
private get isMultiple() {
return this.selection === 'multiple';
}
private _selectedItems: Set<string> = new Set();
private _observerCallback({
changes: { added, attributes },
}: MutationControllerParams<IgcToggleButtonComponent>) {
if (this.isMultiple || this._selectedButtons.length <= 1) {
return;
}
const buttons = this.toggleButtons;
const idx = buttons.indexOf(
added.length ? last(added).node : last(attributes)
);
for (const [i, button] of buttons.entries()) {
if (button.selected && i !== idx) {
button.selected = false;
}
}
}
private get _selectedButtons(): Array<IgcToggleButtonComponent> {
return this.toggleButtons.filter((b) => b.selected);
}
@queryAssignedElements({ selector: IgcToggleButtonComponent.tagName })
private toggleButtons!: Array<IgcToggleButtonComponent>;
/**
* Disables all buttons inside the group.
* @attr
*/
@property({ type: Boolean, reflect: true })
public disabled = false;
/**
* Sets the orientation of the buttons in the group.
* @attr
*/
@property({ reflect: true })
public alignment: ContentOrientation = 'horizontal';
/**
* Controls the mode of selection for the button group.
* @attr
*/
@property({ reflect: false })
public selection: ButtonGroupSelection = 'single';
/**
* Gets/Sets the currently selected buttons (their values).
* @attr
*/
@property({ attribute: 'selected-items', type: Array, reflect: false })
public get selectedItems(): string[] {
return this._selectedButtons.map((b) => b.value).filter((v) => v);
}
public set selectedItems(values: string[]) {
this._selectedItems = new Set(Array.isArray(values) ? values : []);
this.setSelection(this._selectedItems);
}
@watch('disabled', { waitUntilFirstUpdate: true })
protected updateDisabledState() {
this.toggleButtons.forEach((b) => {
b.disabled = this.disabled;
});
}
@watch('selection', { waitUntilFirstUpdate: true })
protected updateSelectionState() {
if (this._selectedButtons.length) {
this.toggleButtons.forEach((b) => {
b.selected = false;
});
}
}
constructor() {
super();
createMutationController(this, {
callback: this._observerCallback,
filter: [IgcToggleButtonComponent.tagName],
config: {
attributeFilter: ['selected'],
childList: true,
subtree: true,
},
});
}
protected override firstUpdated() {
if (this.disabled) {
this.updateDisabledState();
}
const buttons = this._selectedButtons;
if (buttons.length) {
if (!this.isMultiple) {
const index = buttons.indexOf(buttons.at(-1)!);
for (let i = 0; i < index; i++) {
buttons[i].selected = false;
}
}
} else {
this.setSelection(this._selectedItems);
}
}
private handleClick(event: MouseEvent) {
const button = findElementFromEventPath<IgcToggleButtonComponent>(
IgcToggleButtonComponent.tagName,
event
);
if (button) {
this.isMultiple
? this.handleMultipleSelection(button)
: this.handleSingleSelection(button);
}
}
private handleSingleSelection(button: IgcToggleButtonComponent) {
const singleRequired = this.selection === 'single-required';
const selectedButton = this._selectedButtons.at(0);
const isSame = selectedButton && selectedButton.value === button.value;
if (selectedButton) {
if (singleRequired && isSame) return;
this.emitDeselectEvent(selectedButton);
}
if (isSame) return;
this.emitSelectEvent(button);
}
private handleMultipleSelection(button: IgcToggleButtonComponent) {
button.selected
? this.emitDeselectEvent(button)
: this.emitSelectEvent(button);
}
private emitSelectEvent(button: IgcToggleButtonComponent) {
button.selected = true;
this.emitEvent('igcSelect', { detail: button.value });
}
private emitDeselectEvent(button: IgcToggleButtonComponent) {
button.selected = false;
this.emitEvent('igcDeselect', { detail: button.value });
}
private setSelection(values: Set<string>) {
if (!values.size) {
this.toggleButtons.forEach((b) => {
b.selected = false;
});
return;
}
for (const button of this.toggleButtons) {
if (values.has(button.value)) {
button.selected = true;
if (!this.isMultiple) {
break;
}
}
}
}
protected override render() {
return html`
<div
part="group"
role="group"
aria-disabled=${this.disabled}
@click=${this.handleClick}
>
<slot></slot>
</div>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-button-group': IgcButtonGroupComponent;
}
}