-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
multiselect.ts
executable file
·2276 lines (1943 loc) · 86.3 KB
/
multiselect.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { AnimationEvent } from '@angular/animations';
import { CommonModule } from '@angular/common';
import {
AfterContentInit,
AfterViewChecked,
AfterViewInit,
booleanAttribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
computed,
ContentChild,
ContentChildren,
effect,
ElementRef,
EventEmitter,
forwardRef,
Input,
NgModule,
NgZone,
numberAttribute,
OnInit,
Output,
QueryList,
Renderer2,
Signal,
signal,
TemplateRef,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { FilterService, Footer, Header, OverlayOptions, OverlayService, PrimeNGConfig, PrimeTemplate, SharedModule, TranslationKeys } from 'primeng/api';
import { DomHandler } from 'primeng/dom';
import { Overlay, OverlayModule } from 'primeng/overlay';
import { RippleModule } from 'primeng/ripple';
import { Scroller, ScrollerModule } from 'primeng/scroller';
import { ScrollerOptions } from 'primeng/api';
import { TooltipModule } from 'primeng/tooltip';
import { ObjectUtils, UniqueComponentId } from 'primeng/utils';
import { CheckIcon } from 'primeng/icons/check';
import { SearchIcon } from 'primeng/icons/search';
import { TimesCircleIcon } from 'primeng/icons/timescircle';
import { TimesIcon } from 'primeng/icons/times';
import { ChevronDownIcon } from 'primeng/icons/chevrondown';
import { Nullable } from 'primeng/ts-helpers';
import { AutoFocusModule } from 'primeng/autofocus';
import { MultiSelectRemoveEvent, MultiSelectFilterOptions, MultiSelectFilterEvent, MultiSelectBlurEvent, MultiSelectChangeEvent, MultiSelectFocusEvent, MultiSelectLazyLoadEvent, MultiSelectSelectAllChangeEvent } from './multiselect.interface';
import { MinusIcon } from 'primeng/icons/minus';
export const MULTISELECT_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MultiSelect),
multi: true
};
@Component({
selector: 'p-multiSelectItem',
template: `
<li
pRipple
role="option"
[ngStyle]="{ height: itemSize + 'px' }"
class="p-multiselect-item"
[ngClass]="{ 'p-multiselect-item': true, 'p-disabled': disabled, 'p-focus': focused }"
[id]="id"
[attr.aria-label]="label"
[attr.aria-setsize]="ariaSetSize"
[attr.aria-posinset]="ariaPosInset"
[attr.aria-selected]="selected"
[attr.data-p-focused]="focused"
[attr.data-p-highlight]="selected"
[attr.data-p-disabled]="disabled"
[attr.aria-checked]="selected"
(click)="onOptionClick($event)"
(mouseenter)="onOptionMouseEnter($event)"
>
<div class="p-checkbox p-component" [ngClass]="{ 'p-variant-filled': config.inputStyle() === 'filled' }">
<div class="p-checkbox-box" [ngClass]="{ 'p-highlight': selected }">
<ng-container *ngIf="selected">
<CheckIcon *ngIf="!checkIconTemplate && !itemCheckboxIconTemplate" [styleClass]="'p-checkbox-icon'" [attr.aria-hidden]="true" />
<span *ngIf="checkIconTemplate" class="p-checkbox-icon" [attr.aria-hidden]="true">
<ng-template *ngTemplateOutlet="checkIconTemplate"></ng-template>
</span>
<span *ngIf="itemCheckboxIconTemplate" class="p-checkbox-icon" [attr.aria-hidden]="true">
<ng-template *ngTemplateOutlet="itemCheckboxIconTemplate; context: { $implicit: selected }"></ng-template>
</span>
</ng-container>
</div>
</div>
<span *ngIf="!template">{{ label ?? 'empty' }}</span>
<ng-container *ngTemplateOutlet="template; context: { $implicit: option }"></ng-container>
</li>
`,
encapsulation: ViewEncapsulation.None,
host: {
class: 'p-element'
}
})
export class MultiSelectItem {
@Input() id: string | undefined;
@Input() option: any;
@Input({ transform: booleanAttribute }) selected: boolean | undefined;
@Input() label: string | undefined;
@Input({ transform: booleanAttribute }) disabled: boolean | undefined;
@Input({ transform: numberAttribute }) itemSize: number | undefined;
@Input({ transform: booleanAttribute }) focused: boolean | undefined;
@Input() ariaPosInset: string | undefined;
@Input() ariaSetSize: string | undefined;
@Input() template: TemplateRef<any> | undefined;
@Input() checkIconTemplate: TemplateRef<any> | undefined;
@Input() itemCheckboxIconTemplate: TemplateRef<any> | undefined;
@Output() onClick: EventEmitter<any> = new EventEmitter();
@Output() onMouseEnter: EventEmitter<any> = new EventEmitter();
constructor(public config: PrimeNGConfig) {}
onOptionClick(event: Event) {
this.onClick.emit({
originalEvent: event,
option: this.option,
selected: this.selected
});
event.stopPropagation();
}
onOptionMouseEnter(event: Event) {
this.onMouseEnter.emit({
originalEvent: event,
option: this.option,
selected: this.selected
});
}
}
/**
* MultiSelect is used to select multiple items from a collection.
* @group Components
*/
@Component({
selector: 'p-multiSelect',
template: `
<div #container [attr.id]="id" [ngClass]="containerClass" [ngStyle]="style" [class]="styleClass" (click)="onContainerClick($event)">
<div class="p-hidden-accessible" [attr.data-p-hidden-accessible]="true">
<input
#focusInput
[pTooltip]="tooltip"
[tooltipPosition]="tooltipPosition"
[positionStyle]="tooltipPositionStyle"
[tooltipStyleClass]="tooltipStyleClass"
[attr.aria-disabled]="disabled"
[attr.id]="inputId"
role="combobox"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledBy"
[attr.aria-haspopup]="'listbox'"
[attr.aria-expanded]="overlayVisible ?? false"
[attr.aria-controls]="overlayVisible ? id + '_list' : null"
[attr.tabindex]="!disabled ? tabindex : -1"
[attr.aria-activedescendant]="focused ? focusedOptionId : undefined"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
(keydown)="onKeyDown($event)"
pAutoFocus
[autofocus]="autofocus"
[attr.value]="label() || 'empty'"
/>
</div>
<div
class="p-multiselect-label-container"
[pTooltip]="tooltip"
(mouseleave)="labelContainerMouseLeave()"
[tooltipDisabled]="_disableTooltip"
[tooltipPosition]="tooltipPosition"
[positionStyle]="tooltipPositionStyle"
[tooltipStyleClass]="tooltipStyleClass"
>
<div [ngClass]="labelClass">
<ng-container *ngIf="!selectedItemsTemplate">
<ng-container *ngIf="display === 'comma'">{{ label() || 'empty' }}</ng-container>
<ng-container *ngIf="display === 'chip'">
<div #token *ngFor="let item of chipSelectedItems(); let i = index" class="p-multiselect-token">
<span class="p-multiselect-token-label">{{ getLabelByValue(item) }}</span>
<ng-container *ngIf="!disabled">
<TimesCircleIcon
*ngIf="!removeTokenIconTemplate"
[ngClass]="{ 'p-disabled': isOptionDisabled(item) }"
[styleClass]="'p-multiselect-token-icon'"
(click)="removeOption(item, event)"
[attr.data-pc-section]="'clearicon'"
[attr.aria-hidden]="true"
/>
<span *ngIf="removeTokenIconTemplate" class="p-multiselect-token-icon" (click)="removeOption(item, event)" [attr.data-pc-section]="'clearicon'" [attr.aria-hidden]="true">
<ng-container *ngTemplateOutlet="removeTokenIconTemplate"></ng-container>
</span>
</ng-container>
</div>
<ng-container *ngIf="!modelValue() || modelValue().length === 0">{{ placeholder() || defaultLabel || 'empty' }}</ng-container>
</ng-container>
</ng-container>
<ng-container *ngTemplateOutlet="selectedItemsTemplate; context: { $implicit: selectedOptions, removeChip: removeOption.bind(this) }"></ng-container>
</div>
<ng-container *ngIf="isVisibleClearIcon">
<TimesIcon *ngIf="!clearIconTemplate" [styleClass]="'p-multiselect-clear-icon'" (click)="clear($event)" [attr.data-pc-section]="'clearicon'" [attr.aria-hidden]="true" />
<span *ngIf="clearIconTemplate" class="p-multiselect-clear-icon" (click)="clear($event)" [attr.data-pc-section]="'clearicon'" [attr.aria-hidden]="true">
<ng-template *ngTemplateOutlet="clearIconTemplate"></ng-template>
</span>
</ng-container>
</div>
<div class="p-multiselect-trigger">
<ng-container *ngIf="loading; else elseBlock">
<ng-container *ngIf="loadingIconTemplate">
<ng-container *ngTemplateOutlet="loadingIconTemplate"></ng-container>
</ng-container>
<ng-container *ngIf="!loadingIconTemplate">
<span *ngIf="loadingIcon" [ngClass]="'p-multiselect-trigger-icon pi-spin ' + loadingIcon" aria-hidden="true"></span>
<span *ngIf="!loadingIcon" [class]="'p-multiselect-trigger-icon pi pi-spinner pi-spin'" aria-hidden="true"></span>
</ng-container>
</ng-container>
<ng-template #elseBlock>
<ng-container *ngIf="!dropdownIconTemplate">
<span *ngIf="dropdownIcon" class="p-multiselect-trigger-icon" [ngClass]="dropdownIcon" [attr.data-pc-section]="'triggericon'" [attr.aria-hidden]="true"></span>
<ChevronDownIcon *ngIf="!dropdownIcon" [styleClass]="'p-multiselect-trigger-icon'" [attr.data-pc-section]="'triggericon'" [attr.aria-hidden]="true" />
</ng-container>
<span *ngIf="dropdownIconTemplate" class="p-multiselect-trigger-icon" [attr.data-pc-section]="'triggericon'" [attr.aria-hidden]="true">
<ng-template *ngTemplateOutlet="dropdownIconTemplate"></ng-template>
</span>
</ng-template>
</div>
<p-overlay
#overlay
[(visible)]="overlayVisible"
[options]="overlayOptions"
[target]="'@parent'"
[appendTo]="appendTo"
[autoZIndex]="autoZIndex"
[baseZIndex]="baseZIndex"
[showTransitionOptions]="showTransitionOptions"
[hideTransitionOptions]="hideTransitionOptions"
(onAnimationStart)="onOverlayAnimationStart($event)"
(onHide)="hide()"
>
<ng-template pTemplate="content">
<div [attr.id]="id + '_list'" [ngClass]="'p-multiselect-panel p-component'" [ngStyle]="panelStyle" [class]="panelStyleClass">
<span
#firstHiddenFocusableEl
role="presentation"
class="p-hidden-accessible p-hidden-focusable"
[attr.tabindex]="0"
(focus)="onFirstHiddenFocus($event)"
[attr.data-p-hidden-accessible]="true"
[attr.data-p-hidden-focusable]="true"
>
</span>
<div class="p-multiselect-header" *ngIf="showHeader">
<ng-content select="p-header"></ng-content>
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
<ng-container *ngIf="filterTemplate; else builtInFilterElement">
<ng-container *ngTemplateOutlet="filterTemplate; context: { options: filterOptions }"></ng-container>
</ng-container>
<ng-template #builtInFilterElement>
<div
class="p-checkbox p-component"
*ngIf="isSelectionAllDisabled()"
[ngClass]="{ 'p-variant-filled': variant === 'filled' || config.inputStyle() === 'filled', 'p-checkbox-disabled': disabled || toggleAllDisabled }"
(click)="onToggleAll($event)"
(keydown)="onHeaderCheckboxKeyDown($event)"
>
<div class="p-hidden-accessible" [attr.data-p-hidden-accessible]="true">
<input
#headerCheckbox
type="checkbox"
[readonly]="readonly"
[attr.checked]="allSelected()"
(focus)="onHeaderCheckboxFocus()"
(blur)="onHeaderCheckboxBlur()"
[disabled]="disabled || toggleAllDisabled"
[attr.aria-label]="toggleAllAriaLabel"
/>
</div>
<div
class="p-checkbox-box"
role="checkbox"
[attr.aria-label]="toggleAllAriaLabel"
[attr.aria-checked]="allSelected()"
[ngClass]="{ 'p-highlight': allSelected(), 'p-focus': headerCheckboxFocus, 'p-disabled': disabled || toggleAllDisabled }"
>
<ng-container *ngIf="allSelected() || partialSelected()">
<ng-container *ngIf="!checkIconTemplate && !headerCheckboxIconTemplate">
<CheckIcon [styleClass]="'p-checkbox-icon'" *ngIf="allSelected()" [attr.aria-hidden]="true" />
</ng-container>
<span *ngIf="checkIconTemplate" class="p-checkbox-icon" [attr.aria-hidden]="true">
<ng-template *ngTemplateOutlet="checkIconTemplate; context: { $implicit: allSelected() }"></ng-template>
</span>
<span *ngIf="headerCheckboxIconTemplate" class="p-checkbox-icon" [attr.aria-hidden]="true">
<ng-template *ngTemplateOutlet="headerCheckboxIconTemplate; context: { $implicit: allSelected(), partialSelected: partialSelected() }"></ng-template>
</span>
</ng-container>
</div>
</div>
<div class="p-multiselect-filter-container" *ngIf="filter">
<input
#filterInput
type="text"
role="searchbox"
[attr.autocomplete]="autocomplete"
[attr.placeholder]="filterPlaceHolder"
role="searchbox"
[attr.aria-owns]="id + '_list'"
[attr.aria-activedescendant]="focusedOptionId"
[value]="_filterValue() || ''"
(input)="onFilterInputChange($event)"
(keydown)="onFilterKeyDown($event)"
(click)="onInputClick($event)"
(blur)="onFilterBlur($event)"
class="p-multiselect-filter p-inputtext p-component"
[disabled]="disabled"
[attr.placeholder]="filterPlaceHolder"
[attr.aria-label]="ariaFilterLabel"
/>
<SearchIcon [styleClass]="'p-multiselect-filter-icon'" *ngIf="!filterIconTemplate" />
<span *ngIf="filterIconTemplate" class="p-multiselect-filter-icon">
<ng-template *ngTemplateOutlet="filterIconTemplate"></ng-template>
</span>
</div>
<button class="p-multiselect-close p-link p-button-icon-only" type="button" (click)="close($event)" pRipple [attr.aria-label]="closeAriaLabel">
<TimesIcon [styleClass]="'p-multiselect-close-icon'" *ngIf="!closeIconTemplate" />
<span *ngIf="closeIconTemplate" class="p-multiselect-close-icon">
<ng-template *ngTemplateOutlet="closeIconTemplate"></ng-template>
</span>
</button>
</ng-template>
</div>
<div class="p-multiselect-items-wrapper" [ngStyle]="{ 'max-height': virtualScroll ? 'auto' : scrollHeight || 'auto' }">
<p-scroller
*ngIf="virtualScroll"
#scroller
[items]="visibleOptions()"
[style]="{ height: scrollHeight }"
[itemSize]="virtualScrollItemSize || _itemSize"
[autoSize]="true"
[tabindex]="-1"
[lazy]="lazy"
(onLazyLoad)="onLazyLoad.emit($event)"
[options]="virtualScrollOptions"
>
<ng-template pTemplate="content" let-items let-scrollerOptions="options">
<ng-container *ngTemplateOutlet="buildInItems; context: { $implicit: items, options: scrollerOptions }"></ng-container>
</ng-template>
<ng-container *ngIf="loaderTemplate">
<ng-template pTemplate="loader" let-scrollerOptions="options">
<ng-container *ngTemplateOutlet="loaderTemplate; context: { options: scrollerOptions }"></ng-container>
</ng-template>
</ng-container>
</p-scroller>
<ng-container *ngIf="!virtualScroll">
<ng-container *ngTemplateOutlet="buildInItems; context: { $implicit: visibleOptions(), options: {} }"></ng-container>
</ng-container>
<ng-template #buildInItems let-items let-scrollerOptions="options">
<ul #items class="p-multiselect-items p-component" [ngClass]="scrollerOptions.contentStyleClass" [ngStyle]="scrollerOptions.contentStyle" role="listbox" aria-multiselectable="true" [attr.aria-label]="listLabel">
<ng-template ngFor let-option [ngForOf]="items" let-i="index">
<ng-container *ngIf="isOptionGroup(option)">
<li [attr.id]="id + '_' + getOptionIndex(i, scrollerOptions)" class="p-multiselect-item-group" [ngStyle]="{ height: scrollerOptions.itemSize + 'px' }" role="option">
<span *ngIf="!groupTemplate">{{ getOptionGroupLabel(option.optionGroup) }}</span>
<ng-container *ngTemplateOutlet="groupTemplate; context: { $implicit: option.optionGroup }"></ng-container>
</li>
</ng-container>
<ng-container *ngIf="!isOptionGroup(option)">
<p-multiSelectItem
[id]="id + '_' + getOptionIndex(i, scrollerOptions)"
[option]="option"
[selected]="isSelected(option)"
[label]="getOptionLabel(option)"
[disabled]="isOptionDisabled(option)"
[template]="itemTemplate"
[checkIconTemplate]="checkIconTemplate"
[itemCheckboxIconTemplate]="itemCheckboxIconTemplate"
[itemSize]="scrollerOptions.itemSize"
[focused]="focusedOptionIndex() === getOptionIndex(i, scrollerOptions)"
[ariaPosInset]="getAriaPosInset(getOptionIndex(i, scrollerOptions))"
[ariaSetSize]="ariaSetSize"
(onClick)="onOptionSelect($event, false, getOptionIndex(i, scrollerOptions))"
(onMouseEnter)="onOptionMouseEnter($event, getOptionIndex(i, scrollerOptions))"
></p-multiSelectItem>
</ng-container>
</ng-template>
<li *ngIf="hasFilter() && isEmpty()" class="p-multiselect-empty-message" [ngStyle]="{ height: scrollerOptions.itemSize + 'px' }" role="option">
<ng-container *ngIf="!emptyFilterTemplate && !emptyTemplate; else emptyFilter">
{{ emptyFilterMessageLabel }}
</ng-container>
<ng-container #emptyFilter *ngTemplateOutlet="emptyFilterTemplate || emptyTemplate"></ng-container>
</li>
<li *ngIf="!hasFilter() && isEmpty()" class="p-multiselect-empty-message" [ngStyle]="{ height: scrollerOptions.itemSize + 'px' }" role="option">
<ng-container *ngIf="!emptyTemplate; else empty">
{{ emptyMessageLabel }}
</ng-container>
<ng-container #empty *ngTemplateOutlet="emptyTemplate"></ng-container>
</li>
</ul>
</ng-template>
</div>
<div class="p-multiselect-footer" *ngIf="footerFacet || footerTemplate">
<ng-content select="p-footer"></ng-content>
<ng-container *ngTemplateOutlet="footerTemplate"></ng-container>
</div>
<span
#lastHiddenFocusableEl
role="presentation"
class="p-hidden-accessible p-hidden-focusable"
[attr.tabindex]="0"
(focus)="onLastHiddenFocus($event)"
[attr.data-p-hidden-accessible]="true"
[attr.data-p-hidden-focusable]="true"
></span>
</div>
</ng-template>
</p-overlay>
</div>
`,
host: {
class: 'p-element p-inputwrapper',
'[class.p-inputwrapper-focus]': 'focused || overlayVisible',
'[class.p-inputwrapper-filled]': 'filled'
},
providers: [MULTISELECT_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styleUrls: ['./multiselect.css']
})
export class MultiSelect implements OnInit, AfterViewInit, AfterContentInit, AfterViewChecked, ControlValueAccessor {
/**
* Unique identifier of the component
* @group Props
*/
@Input() id: string | undefined;
/**
* Defines a string that labels the input for accessibility.
* @group Props
*/
@Input() ariaLabel: string | undefined;
/**
* Inline style of the element.
* @group Props
*/
@Input() style: { [klass: string]: any } | null | undefined;
/**
* Style class of the element.
* @group Props
*/
@Input() styleClass: string | undefined;
/**
* Inline style of the overlay panel.
* @group Props
*/
@Input() panelStyle: any;
/**
* Style class of the overlay panel element.
* @group Props
*/
@Input() panelStyleClass: string | undefined;
/**
* Identifier of the focus input to match a label defined for the component.
* @group Props
*/
@Input() inputId: string | undefined;
/**
* When present, it specifies that the element should be disabled.
* @group Props
*/
@Input({ transform: booleanAttribute }) disabled: boolean | undefined;
/**
* When present, it specifies that the component cannot be edited.
* @group Props
*/
@Input({ transform: booleanAttribute }) readonly: boolean | undefined;
/**
* Whether to display options as grouped when nested options are provided.
* @group Props
*/
@Input({ transform: booleanAttribute }) group: boolean | undefined;
/**
* When specified, displays an input field to filter the items on keyup.
* @group Props
*/
@Input({ transform: booleanAttribute }) filter: boolean = true;
/**
* Defines placeholder of the filter input.
* @group Props
*/
@Input() filterPlaceHolder: string | undefined;
/**
* Locale to use in filtering. The default locale is the host environment's current locale.
* @group Props
*/
@Input() filterLocale: string | undefined;
/**
* Specifies the visibility of the options panel.
* @group Props
*/
@Input({ transform: booleanAttribute }) overlayVisible: boolean | undefined;
/**
* Index of the element in tabbing order.
* @group Props
*/
@Input({ transform: numberAttribute }) tabindex: number | undefined = 0;
/**
* Specifies the input variant of the component.
* @group Props
*/
@Input() variant: 'filled' | 'outlined' = 'outlined';
/**
* Target element to attach the overlay, valid values are "body" or a local ng-template variable of another element (note: use binding with brackets for template variables, e.g. [appendTo]="mydiv" for a div element having #mydiv as variable name).
* @group Props
*/
@Input() appendTo: HTMLElement | ElementRef | TemplateRef<any> | string | null | undefined | any;
/**
* A property to uniquely identify a value in options.
* @group Props
*/
@Input() dataKey: string | undefined;
/**
* Name of the input element.
* @group Props
*/
@Input() name: 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;
/**
* Whether to show labels of selected item labels or use default label.
* @group Props
* @defaultValue true
*/
@Input() set displaySelectedLabel(val: boolean) {
this._displaySelectedLabel = val;
}
get displaySelectedLabel(): boolean {
return this._displaySelectedLabel;
}
/**
* Decides how many selected item labels to show at most.
* @group Props
* @defaultValue 3
*/
@Input() set maxSelectedLabels(val: number | null | undefined) {
this._maxSelectedLabels = val;
}
get maxSelectedLabels(): number | null | undefined {
return this._maxSelectedLabels;
}
/**
* Decides how many selected item labels to show at most.
* @group Props
*/
@Input({ transform: numberAttribute }) selectionLimit: number | undefined;
/**
* Label to display after exceeding max selected labels e.g. ({0} items selected), defaults "ellipsis" keyword to indicate a text-overflow.
* @group Props
*/
@Input() selectedItemsLabel: string | undefined;
/**
* Whether to show the checkbox at header to toggle all items at once.
* @group Props
*/
@Input({ transform: booleanAttribute }) showToggleAll: boolean = true;
/**
* Text to display when filtering does not return any results.
* @group Props
*/
@Input() emptyFilterMessage: string = '';
/**
* Text to display when there is no data. Defaults to global value in i18n translation configuration.
* @group Props
*/
@Input() emptyMessage: string = '';
/**
* Clears the filter value when hiding the dropdown.
* @group Props
*/
@Input({ transform: booleanAttribute }) resetFilterOnHide: boolean = false;
/**
* Icon class of the dropdown icon.
* @group Props
*/
@Input() dropdownIcon: string | 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;
/**
* Name of the label field of an option group.
* @group Props
*/
@Input() optionGroupLabel: string | undefined = 'label';
/**
* Name of the options field of an option group.
* @group Props
*/
@Input() optionGroupChildren: string = 'items';
/**
* Whether to show the header.
* @group Props
*/
@Input({ transform: booleanAttribute }) showHeader: boolean = true;
/**
* When filtering is enabled, filterBy decides which field or fields (comma separated) to search against.
* @group Props
*/
@Input() filterBy: string | undefined;
/**
* Height of the viewport in pixels, a scrollbar is defined if height of list exceeds this value.
* @group Props
*/
@Input() scrollHeight: string = '200px';
/**
* Defines if data is loaded and interacted with in lazy manner.
* @group Props
*/
@Input({ transform: booleanAttribute }) lazy: boolean = false;
/**
* Whether the data should be loaded on demand during scroll.
* @group Props
*/
@Input({ transform: booleanAttribute }) virtualScroll: boolean | undefined;
/**
* Whether the multiselect is in loading state.
* @group Props
*/
@Input({ transform: booleanAttribute }) loading: boolean | undefined = false;
/**
* Height of an item in the list for VirtualScrolling.
* @group Props
*/
@Input({ transform: numberAttribute }) virtualScrollItemSize: number | undefined;
/**
* Icon to display in loading state.
* @group Props
*/
@Input() loadingIcon: string | undefined;
/**
* Whether to use the scroller feature. The properties of scroller component can be used like an object in it.
* @group Props
*/
@Input() virtualScrollOptions: ScrollerOptions | undefined;
/**
* Whether to use overlay API feature. The properties of overlay API can be used like an object in it.
* @group Props
*/
@Input() overlayOptions: OverlayOptions | undefined;
/**
* Defines a string that labels the filter input.
* @group Props
*/
@Input() ariaFilterLabel: string | undefined;
/**
* Defines how the items are filtered.
* @group Props
*/
@Input() filterMatchMode: 'contains' | 'startsWith' | 'endsWith' | 'equals' | 'notEquals' | 'in' | 'lt' | 'lte' | 'gt' | 'gte' = 'contains';
/**
* Advisory information to display in a tooltip on hover.
* @group Props
*/
@Input() tooltip: string = '';
/**
* Position of the tooltip.
* @group Props
*/
@Input() tooltipPosition: 'top' | 'left' | 'right' | 'bottom' = 'right';
/**
* Type of CSS position.
* @group Props
*/
@Input() tooltipPositionStyle: string = 'absolute';
/**
* Style class of the tooltip.
* @group Props
*/
@Input() tooltipStyleClass: string | undefined;
/**
* Applies focus to the filter element when the overlay is shown.
* @group Props
*/
@Input({ transform: booleanAttribute }) autofocusFilter: boolean = true;
/**
* Defines how the selected items are displayed.
* @group Props
*/
@Input() display: string | 'comma' | 'chip' = 'comma';
/**
* Defines the autocomplete is active.
* @group Props
*/
@Input() autocomplete: string = 'off';
/**
* When enabled, a clear icon is displayed to clear the value.
* @group Props
*/
@Input({ transform: booleanAttribute }) showClear: boolean = false;
/**
* When present, it specifies that the component should automatically get focus on load.
* @group Props
*/
@Input({ transform: booleanAttribute }) autofocus: boolean | undefined;
/**
* @deprecated since v14.2.0, use overlayOptions property instead.
* Whether to automatically manage layering.
* @group Props
*/
@Input() get autoZIndex(): boolean | undefined {
return this._autoZIndex;
}
set autoZIndex(val: boolean | undefined) {
this._autoZIndex = val;
console.warn('The autoZIndex property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* @deprecated since v14.2.0, use overlayOptions property instead.
* Base zIndex value to use in layering.
* @group Props
*/
@Input() get baseZIndex(): number | undefined {
return this._baseZIndex;
}
set baseZIndex(val: number | undefined) {
this._baseZIndex = val;
console.warn('The baseZIndex property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* Transition options of the show animation.
* @group Props
* @deprecated since v14.2.0, use overlayOptions property instead.
*/
@Input() get showTransitionOptions(): string | undefined {
return this._showTransitionOptions;
}
set showTransitionOptions(val: string | undefined) {
this._showTransitionOptions = val;
console.warn('The showTransitionOptions property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* Transition options of the hide animation.
* @group Props
* @deprecated since v14.2.0, use overlayOptions property instead.
*/
@Input() get hideTransitionOptions(): string | undefined {
return this._hideTransitionOptions;
}
set hideTransitionOptions(val: string | undefined) {
this._hideTransitionOptions = val;
console.warn('The hideTransitionOptions property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* Label to display when there are no selections.
* @group Props
* @deprecated Use placeholder instead.
*/
@Input() set defaultLabel(val: string | undefined) {
this._defaultLabel = val;
console.warn('defaultLabel property is deprecated since 16.6.0, use placeholder instead');
}
get defaultLabel(): string | undefined {
return this._defaultLabel;
}
/**
* Label to display when there are no selections.
* @group Props
*/
@Input() set placeholder(val: string | undefined) {
this._placeholder.set(val);
}
get placeholder(): Signal<string | undefined> {
return this._placeholder.asReadonly();
}
/**
* An array of objects to display as the available options.
* @group Props
*/
@Input() get options(): any[] | undefined {
const options = this._options();
return options;
}
set options(val: any[] | undefined) {
if (!ObjectUtils.deepEquals(this._options(), val)) {
this._options.set(val);
}
}
/**
* When specified, filter displays with this value.
* @group Props
*/
@Input() get filterValue(): string | undefined | null {
return this._filterValue();
}
set filterValue(val: string | undefined | null) {
this._filterValue.set(val);
}
/**
* Item size of item to be virtual scrolled.
* @group Props
* @deprecated use virtualScrollItemSize property instead.
*/
@Input() get itemSize(): number | undefined {
return this._itemSize;
}
set itemSize(val: number | undefined) {
this._itemSize = val;
console.warn('The itemSize property is deprecated, use virtualScrollItemSize property instead.');
}
/**
* Whether all data is selected.
* @group Props
*/
@Input() get selectAll(): boolean | undefined | null {
return this._selectAll;
}
set selectAll(value: boolean | undefined | null) {
this._selectAll = value;
}
/**
* Indicates whether to focus on options when hovering over them, defaults to optionLabel.
* @group Props
*/
@Input({ transform: booleanAttribute }) focusOnHover: boolean = false;
/**
* Fields used when filtering the options, defaults to optionLabel.
* @group Props
*/
@Input() filterFields: any[] | undefined;
/**
* Determines if the option will be selected on focus.
* @group Props
*/
@Input({ transform: booleanAttribute }) selectOnFocus: boolean = false;
/**
* Whether to focus on the first visible or selected element when the overlay panel is shown.
* @group Props
*/
@Input({ transform: booleanAttribute }) autoOptionFocus: boolean = true;
/**
* Callback to invoke when value changes.
* @param {MultiSelectChangeEvent} event - Custom change event.
* @group Emits
*/
@Output() onChange: EventEmitter<MultiSelectChangeEvent> = new EventEmitter<MultiSelectChangeEvent>();
/**
* Callback to invoke when data is filtered.
* @param {MultiSelectFilterEvent} event - Custom filter event.
* @group Emits
*/
@Output() onFilter: EventEmitter<MultiSelectFilterEvent> = new EventEmitter<MultiSelectFilterEvent>();
/**
* Callback to invoke when multiselect receives focus.
* @param {MultiSelectFocusEvent} event - Custom focus event.
* @group Emits
*/
@Output() onFocus: EventEmitter<MultiSelectFocusEvent> = new EventEmitter<MultiSelectFocusEvent>();
/**
* Callback to invoke when multiselect loses focus.
* @param {MultiSelectBlurEvent} event - Custom blur event.
* @group Emits
*/
@Output() onBlur: EventEmitter<MultiSelectBlurEvent> = new EventEmitter<MultiSelectBlurEvent>();
/**
* Callback to invoke when component is clicked.
* @param {Event} event - Browser event.
* @group Emits
*/
@Output() onClick: EventEmitter<Event> = new EventEmitter<Event>();
/**
* Callback to invoke when input field is cleared.
* @group Emits
*/
@Output() onClear: EventEmitter<void> = new EventEmitter<void>();
/**
* Callback to invoke when overlay panel becomes visible.
* @group Emits
*/
@Output() onPanelShow: EventEmitter<void> = new EventEmitter<void>();
/**
* Callback to invoke when overlay panel becomes hidden.
* @group Emits
*/
@Output() onPanelHide: EventEmitter<void> = new EventEmitter<void>();
/**
* Callback to invoke in lazy mode to load new data.
* @param {MultiSelectLazyLoadEvent} event - Lazy load event.
* @group Emits
*/
@Output() onLazyLoad: EventEmitter<MultiSelectLazyLoadEvent> = new EventEmitter<MultiSelectLazyLoadEvent>();
/**
* Callback to invoke in lazy mode to load new data.
* @param {MultiSelectRemoveEvent} event - Remove event.
* @group Emits
*/
@Output() onRemove: EventEmitter<MultiSelectRemoveEvent> = new EventEmitter<MultiSelectRemoveEvent>();
/**
* Callback to invoke when all data is selected.
* @param {MultiSelectSelectAllChangeEvent} event - Custom select event.
* @group Emits
*/
@Output() onSelectAllChange: EventEmitter<MultiSelectSelectAllChangeEvent> = new EventEmitter<MultiSelectSelectAllChangeEvent>();
@ViewChild('container') containerViewChild: Nullable<ElementRef>;
@ViewChild('overlay') overlayViewChild: Nullable<Overlay>;
@ViewChild('filterInput') filterInputChild: Nullable<ElementRef>;
@ViewChild('focusInput') focusInputViewChild: Nullable<ElementRef>;
@ViewChild('items') itemsViewChild: Nullable<ElementRef>;
@ViewChild('scroller') scroller: Nullable<Scroller>;
@ViewChild('lastHiddenFocusableEl') lastHiddenFocusableElementOnOverlay: Nullable<ElementRef>;
@ViewChild('firstHiddenFocusableEl') firstHiddenFocusableElementOnOverlay: Nullable<ElementRef>;
@ViewChild('headerCheckbox') headerCheckboxViewChild: Nullable<ElementRef>;
@ContentChild(Footer) footerFacet: any;
@ContentChild(Header) headerFacet: any;
@ContentChildren(PrimeTemplate) templates: Nullable<QueryList<PrimeTemplate>>;
searchValue: Nullable<string>;
searchTimeout: any;
_selectAll: boolean | undefined | null = null;
_autoZIndex: boolean | undefined;
_baseZIndex: number | undefined;
_showTransitionOptions: string | undefined;
_hideTransitionOptions: string | undefined;
_defaultLabel: string | undefined;
_placeholder = signal<string | undefined>(undefined);
_itemSize: number | undefined;
_selectionLimit: number | undefined;
_disableTooltip = false;
value: any[];
public _filteredOptions: any[] | undefined | null;
public onModelChange: Function = () => {};
public onModelTouched: Function = () => {};
public valuesAsString: string | undefined;
public focus: boolean | undefined;
public filtered: boolean | undefined;
public itemTemplate: TemplateRef<any> | undefined;
public groupTemplate: TemplateRef<any> | undefined;
public loaderTemplate: TemplateRef<any> | undefined;