-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
selectbutton.ts
executable file
·326 lines (284 loc) · 10.9 KB
/
selectbutton.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ElementRef, EventEmitter, Input, NgModule, Output, TemplateRef, ViewChild, ViewEncapsulation, booleanAttribute, forwardRef, numberAttribute } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { PrimeTemplate, SharedModule } from 'primeng/api';
import { RippleModule } from 'primeng/ripple';
import { Nullable } from 'primeng/ts-helpers';
import { ObjectUtils } from 'primeng/utils';
import { AutoFocusModule } from 'primeng/autofocus';
import { SelectButtonChangeEvent, SelectButtonOptionClickEvent } from './selectbutton.interface';
export const SELECTBUTTON_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectButton),
multi: true
};
/**
* SelectButton is used to choose single or multiple items from a list using buttons.
* @group Components
*/
@Component({
selector: 'p-selectButton',
template: `
<div #container [ngClass]="'p-selectbutton p-buttonset p-component'" [ngStyle]="style" [class]="styleClass" role="group" [attr.aria-labelledby]="ariaLabelledBy" [attr.data-pc-name]="'selectbutton'" [attr.data-pc-section]="'root'">
<div
*ngFor="let option of options; let i = index"
pRipple
[attr.tabindex]="i === focusedIndex ? '0' : '-1'"
[attr.aria-label]="option.label"
[role]="multiple ? 'checkbox' : 'radio'"
[attr.aria-checked]="isSelected(option)"
[attr.aria-disabled]="optionDisabled"
class="p-button p-component"
[class]="option.styleClass"
[ngClass]="{ 'p-highlight': isSelected(option), 'p-disabled': disabled || isOptionDisabled(option), 'p-button-icon-only': option.icon && !getOptionLabel(option) }"
(click)="onOptionSelect($event, option, i)"
(keydown)="onKeyDown($event, option, i)"
[attr.title]="option.title"
(focus)="onFocus($event, i)"
(blur)="onBlur()"
[attr.aria-labelledby]="this.getOptionLabel(option)"
[attr.data-pc-section]="'button'"
pAutoFocus
[autofocus]="autofocus"
>
<ng-container *ngIf="!itemTemplate; else customcontent">
<span [ngClass]="'p-button-icon p-button-icon-left'" [class]="option.icon" *ngIf="option.icon" [attr.data-pc-section]="'icon'"></span>
<span class="p-button-label" [attr.data-pc-section]="'label'">{{ getOptionLabel(option) }}</span>
</ng-container>
<ng-template #customcontent>
<ng-container *ngTemplateOutlet="selectButtonTemplate; context: { $implicit: option, index: i }"></ng-container>
</ng-template>
</div>
</div>
`,
providers: [SELECTBUTTON_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styleUrls: ['../button/button.css'],
host: {
class: 'p-element'
}
})
export class SelectButton implements ControlValueAccessor {
/**
* An array of selectitems to display as the available options.
* @group Props
*/
@Input() options: any[] | undefined;
/**
* Name of the label field of an option.
* @group Props
*/
@Input() optionLabel: string | undefined;
/**
* Name of the value field of an option.
* @group Props
*/
@Input() optionValue: string | undefined;
/**
* Name of the disabled field of an option.
* @group Props
*/
@Input() optionDisabled: string | undefined;
/**
* Whether selection can be cleared.
* @group Props
*/
@Input({ transform: booleanAttribute }) unselectable: boolean = false;
/**
* Index of the element in tabbing order.
* @group Props
*/
@Input({ transform: numberAttribute }) tabindex: number = 0;
/**
* When specified, allows selecting multiple values.
* @group Props
*/
@Input({ transform: booleanAttribute }) multiple: boolean | undefined;
/**
* Whether selection can not be cleared.
* @group Props
*/
@Input({ transform: booleanAttribute }) allowEmpty: boolean = true;
/**
* Inline style of the component.
* @group Props
*/
@Input() style: any;
/**
* Style class of the component.
* @group Props
*/
@Input() styleClass: string | undefined;
/**
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
* @group Props
*/
@Input() ariaLabelledBy: string | undefined;
/**
* When present, it specifies that the element should be disabled.
* @group Props
*/
@Input({ transform: booleanAttribute }) disabled: boolean | undefined;
/**
* A property to uniquely identify a value in options.
* @group Props
*/
@Input() dataKey: string | undefined;
/**
* When present, it specifies that the component should automatically get focus on load.
* @group Props
*/
@Input({ transform: booleanAttribute }) autofocus: boolean | undefined;
/**
* Callback to invoke on input click.
* @param {SelectButtonOptionClickEvent} event - Custom click event.
* @group Emits
*/
@Output() onOptionClick: EventEmitter<SelectButtonOptionClickEvent> = new EventEmitter<SelectButtonOptionClickEvent>();
/**
* Callback to invoke on selection change.
* @param {SelectButtonChangeEvent} event - Custom change event.
* @group Emits
*/
@Output() onChange: EventEmitter<SelectButtonChangeEvent> = new EventEmitter<SelectButtonChangeEvent>();
@ViewChild('container') container: Nullable<ElementRef>;
@ContentChild(PrimeTemplate) itemTemplate!: PrimeTemplate;
public get selectButtonTemplate(): TemplateRef<any> {
return this.itemTemplate?.template;
}
get equalityKey() {
return this.optionValue ? null : this.dataKey;
}
value: any;
onModelChange: Function = () => {};
onModelTouched: Function = () => {};
focusedIndex: number = 0;
constructor(public cd: ChangeDetectorRef) {}
getOptionLabel(option: any) {
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : option.label != undefined ? option.label : option;
}
getOptionValue(option: any) {
return this.optionValue ? ObjectUtils.resolveFieldData(option, this.optionValue) : this.optionLabel || option.value === undefined ? option : option.value;
}
isOptionDisabled(option: any) {
return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : option.disabled !== undefined ? option.disabled : false;
}
writeValue(value: any): void {
this.value = value;
this.cd.markForCheck();
}
registerOnChange(fn: Function): void {
this.onModelChange = fn;
}
registerOnTouched(fn: Function): void {
this.onModelTouched = fn;
}
setDisabledState(val: boolean): void {
this.disabled = val;
this.cd.markForCheck();
}
onOptionSelect(event, option, index) {
if (this.disabled || this.isOptionDisabled(option)) {
return;
}
let selected = this.isSelected(option);
if (selected && this.unselectable) {
return;
}
let optionValue = this.getOptionValue(option);
let newValue;
if (this.multiple) {
if (selected) newValue = this.value.filter((val) => !ObjectUtils.equals(val, optionValue, this.equalityKey));
else newValue = this.value ? [...this.value, optionValue] : [optionValue];
} else {
if (selected && !this.allowEmpty) {
return;
}
newValue = selected ? null : optionValue;
}
this.focusedIndex = index;
this.value = newValue;
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
this.onOptionClick.emit({
originalEvent: event,
option: option,
index: index
});
}
onKeyDown(event, option, index) {
switch (event.code) {
case 'Space': {
this.onOptionSelect(event, option, index);
event.preventDefault();
break;
}
case 'ArrowDown':
case 'ArrowRight': {
this.changeTabIndexes(event, 'next');
event.preventDefault();
break;
}
case 'ArrowUp':
case 'ArrowLeft': {
this.changeTabIndexes(event, 'prev');
event.preventDefault();
break;
}
default:
//no op
break;
}
}
changeTabIndexes(event, direction) {
let firstTabableChild, index;
for (let i = 0; i <= this.container.nativeElement.children.length - 1; i++) {
if (this.container.nativeElement.children[i].getAttribute('tabindex') === '0') firstTabableChild = { elem: this.container.nativeElement.children[i], index: i };
}
if (direction === 'prev') {
if (firstTabableChild.index === 0) index = this.container.nativeElement.children.length - 1;
else index = firstTabableChild.index - 1;
} else {
if (firstTabableChild.index === this.container.nativeElement.children.length - 1) index = 0;
else index = firstTabableChild.index + 1;
}
this.focusedIndex = index;
this.container.nativeElement.children[index].focus();
}
onFocus(event: Event, index: number) {
this.focusedIndex = index;
}
onBlur() {
this.onModelTouched();
}
removeOption(option: any): void {
this.value = this.value.filter((val: any) => !ObjectUtils.equals(val, this.getOptionValue(option), this.dataKey));
}
isSelected(option: any) {
let selected = false;
const optionValue = this.getOptionValue(option);
if (this.multiple) {
if (this.value && Array.isArray(this.value)) {
for (let val of this.value) {
if (ObjectUtils.equals(val, optionValue, this.dataKey)) {
selected = true;
break;
}
}
}
} else {
selected = ObjectUtils.equals(this.getOptionValue(option), this.value, this.equalityKey);
}
return selected;
}
}
@NgModule({
imports: [CommonModule, RippleModule, SharedModule, AutoFocusModule],
exports: [SelectButton, SharedModule],
declarations: [SelectButton]
})
export class SelectButtonModule {}