-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dropdown.ts
executable file
·1963 lines (1693 loc) · 72.3 KB
/
dropdown.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,
ContentChildren,
effect,
ElementRef,
EventEmitter,
forwardRef,
Input,
NgModule,
NgZone,
numberAttribute,
OnInit,
Output,
QueryList,
Renderer2,
Signal,
signal,
SimpleChanges,
TemplateRef,
ViewChild,
ViewEncapsulation,
ViewRef
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { FilterService, OverlayOptions, PrimeNGConfig, PrimeTemplate, SelectItem, SharedModule, TranslationKeys } from 'primeng/api';
import { AutoFocusModule } from 'primeng/autofocus';
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 { TimesIcon } from 'primeng/icons/times';
import { CheckIcon } from 'primeng/icons/check';
import { BlankIcon } from 'primeng/icons/blank';
import { ChevronDownIcon } from 'primeng/icons/chevrondown';
import { SearchIcon } from 'primeng/icons/search';
import { DropdownChangeEvent, DropdownFilterEvent, DropdownFilterOptions, DropdownLazyLoadEvent } from './dropdown.interface';
import { Nullable } from 'primeng/ts-helpers';
export const DROPDOWN_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Dropdown),
multi: true
};
@Component({
selector: 'p-dropdownItem',
template: `
<li
[id]="id"
(click)="onOptionClick($event)"
(mouseenter)="onOptionMouseEnter($event)"
role="option"
pRipple
[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"
[ngStyle]="{ height: itemSize + 'px' }"
[ngClass]="{ 'p-dropdown-item': true, 'p-highlight': selected, 'p-disabled': disabled, 'p-focus': focused }"
>
<ng-container *ngIf="checkmark">
<CheckIcon *ngIf="selected" [styleClass]="'p-dropdown-check-icon'" />
<BlankIcon *ngIf="!selected" [styleClass]="'p-dropdown-blank-icon'" />
</ng-container>
<span *ngIf="!template">{{ label ?? 'empty' }}</span>
<ng-container *ngTemplateOutlet="template; context: { $implicit: option }"></ng-container>
</li>
`,
host: {
class: 'p-element'
}
})
export class DropdownItem {
@Input() id: string | undefined;
@Input() option: SelectItem | undefined;
@Input({ transform: booleanAttribute }) selected: boolean | undefined;
@Input({ transform: booleanAttribute }) focused: boolean | undefined;
@Input() label: string | undefined;
@Input({ transform: booleanAttribute }) disabled: boolean | undefined;
@Input({ transform: booleanAttribute }) visible: boolean | undefined;
@Input({ transform: numberAttribute }) itemSize: number | undefined;
@Input() ariaPosInset: string | undefined;
@Input() ariaSetSize: string | undefined;
@Input() template: TemplateRef<any> | undefined;
@Input({ transform: booleanAttribute }) checkmark: boolean;
@Output() onClick: EventEmitter<any> = new EventEmitter();
@Output() onMouseEnter: EventEmitter<any> = new EventEmitter();
ngOnInit() {}
onOptionClick(event: Event) {
this.onClick.emit(event);
}
onOptionMouseEnter(event: Event) {
this.onMouseEnter.emit(event);
}
}
/**
* Dropdown also known as Select, is used to choose an item from a collection of options.
* @group Components
*/
@Component({
selector: 'p-dropdown',
template: `
<div #container [attr.id]="id" [ngClass]="containerClass" (click)="onContainerClick($event)" [ngStyle]="style" [class]="styleClass">
<span
#focusInput
[ngClass]="inputClass"
*ngIf="!editable"
[pTooltip]="tooltip"
[tooltipPosition]="tooltipPosition"
[positionStyle]="tooltipPositionStyle"
[tooltipStyleClass]="tooltipStyleClass"
[attr.aria-disabled]="disabled"
[attr.id]="inputId"
role="combobox"
[attr.aria-label]="ariaLabel || (label() === 'p-emptylabel' ? undefined : label())"
[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"
pAutoFocus
[autofocus]="autofocus"
[attr.aria-activedescendant]="focused ? focusedOptionId : undefined"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
(keydown)="onKeyDown($event)"
[attr.aria-required]="required"
[attr.required]="required"
>
<ng-container *ngIf="!selectedItemTemplate; else defaultPlaceholder">{{ label() === 'p-emptylabel' ? ' ' : label() }}</ng-container>
<ng-container *ngIf="selectedItemTemplate && !isSelectedOptionEmpty()" [ngTemplateOutlet]="selectedItemTemplate" [ngTemplateOutletContext]="{ $implicit: selectedOption }"></ng-container>
<ng-template #defaultPlaceholder>
<span *ngIf="isSelectedOptionEmpty()">{{ label() === 'p-emptylabel' ? ' ' : label() }}</span>
</ng-template>
</span>
<input
*ngIf="editable"
#editableInput
type="text"
[attr.id]="inputId"
[attr.maxlength]="maxlength"
[ngClass]="inputClass"
[disabled]="disabled"
aria-haspopup="listbox"
[attr.placeholder]="modelValue() === undefined || modelValue() === null ? placeholder() : undefined"
[attr.aria-label]="ariaLabel || (label() === 'p-emptylabel' ? undefined : label())"
(input)="onEditableInput($event)"
(keydown)="onKeyDown($event)"
pAutoFocus
[autofocus]="autofocus"
[attr.aria-activedescendant]="focused ? focusedOptionId : undefined"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
/>
<ng-container *ngIf="isVisibleClearIcon">
<TimesIcon [styleClass]="'p-dropdown-clear-icon'" (click)="clear($event)" *ngIf="!clearIconTemplate" [attr.data-pc-section]="'clearicon'" />
<span class="p-dropdown-clear-icon" (click)="clear($event)" *ngIf="clearIconTemplate" [attr.data-pc-section]="'clearicon'">
<ng-template *ngTemplateOutlet="clearIconTemplate"></ng-template>
</span>
</ng-container>
<div class="p-dropdown-trigger" role="button" aria-label="dropdown trigger" (mousedown)="onMouseDown($event)" aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible ?? false" [attr.data-pc-section]="'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-dropdown-trigger-icon pi-spin ' + loadingIcon" aria-hidden="true"></span>
<span *ngIf="!loadingIcon" [class]="'p-dropdown-trigger-icon pi pi-spinner pi-spin'" aria-hidden="true"></span>
</ng-container>
</ng-container>
<ng-template #elseBlock>
<ng-container *ngIf="!dropdownIconTemplate">
<span class="p-dropdown-trigger-icon" *ngIf="dropdownIcon" [ngClass]="dropdownIcon"></span>
<ChevronDownIcon *ngIf="!dropdownIcon" [styleClass]="'p-dropdown-trigger-icon'" />
</ng-container>
<span *ngIf="dropdownIconTemplate" class="p-dropdown-trigger-icon">
<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 [ngClass]="'p-dropdown-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>
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
<div class="p-dropdown-header" *ngIf="filter" (click)="$event.stopPropagation()">
<ng-container *ngIf="filterTemplate; else builtInFilterElement">
<ng-container *ngTemplateOutlet="filterTemplate; context: { options: filterOptions }"></ng-container>
</ng-container>
<ng-template #builtInFilterElement>
<div class="p-dropdown-filter-container">
<input
#filter
type="text"
role="searchbox"
autocomplete="off"
[value]="_filterValue() || ''"
class="p-dropdown-filter p-inputtext p-component"
[ngClass]="{ 'p-variant-filled': variant === 'filled' || config.inputStyle() === 'filled' }"
[attr.placeholder]="filterPlaceholder"
[attr.aria-owns]="id + '_list'"
(input)="onFilterInputChange($event)"
[attr.aria-label]="ariaFilterLabel"
[attr.aria-activedescendant]="focusedOptionId"
(keydown)="onFilterKeyDown($event)"
(blur)="onFilterBlur($event)"
/>
<SearchIcon *ngIf="!filterIconTemplate" [styleClass]="'p-dropdown-filter-icon'" />
<span *ngIf="filterIconTemplate" class="p-dropdown-filter-icon">
<ng-template *ngTemplateOutlet="filterIconTemplate"></ng-template>
</span>
</div>
</ng-template>
</div>
<div
class="p-dropdown-items-wrapper"
[ngStyle]="{
'max-height': virtualScroll ? 'auto' : scrollHeight || 'auto'
}"
tabindex="0"
>
<p-scroller
*ngIf="virtualScroll"
#scroller
[items]="visibleOptions()"
[style]="{ height: scrollHeight }"
[itemSize]="virtualScrollItemSize || _itemSize"
[autoSize]="true"
[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 [attr.id]="id + '_list'" [attr.aria-label]="listLabel" class="p-dropdown-items" [ngClass]="scrollerOptions.contentStyleClass" [ngStyle]="scrollerOptions.contentStyle" role="listbox">
<ng-template ngFor let-option [ngForOf]="items" let-i="index">
<ng-container *ngIf="isOptionGroup(option)">
<li class="p-dropdown-item-group" [attr.id]="id + '_' + getOptionIndex(i, scrollerOptions)" [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-dropdownItem
[id]="id + '_' + getOptionIndex(i, scrollerOptions)"
[option]="option"
[checkmark]="checkmark"
[selected]="isSelected(option)"
[label]="getOptionLabel(option)"
[disabled]="isOptionDisabled(option)"
[template]="itemTemplate"
[focused]="focusedOptionIndex() === getOptionIndex(i, scrollerOptions)"
[ariaPosInset]="getAriaPosInset(getOptionIndex(i, scrollerOptions))"
[ariaSetSize]="ariaSetSize"
(onClick)="onOptionSelect($event, option)"
(onMouseEnter)="onOptionMouseEnter($event, getOptionIndex(i, scrollerOptions))"
></p-dropdownItem>
</ng-container>
</ng-template>
<li *ngIf="filterValue && isEmpty()" class="p-dropdown-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="!filterValue && isEmpty()" class="p-dropdown-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>
<ng-container *ngTemplateOutlet="footerTemplate"></ng-container>
<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-filled]': 'filled()',
'[class.p-inputwrapper-focus]': 'focused || overlayVisible'
},
providers: [DROPDOWN_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styleUrls: ['./dropdown.css']
})
export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterViewChecked, ControlValueAccessor {
/**
* Unique identifier of the component
* @group Props
*/
@Input() id: 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';
/**
* When specified, displays an input field to filter the items on keyup.
* @group Props
*/
@Input({ transform: booleanAttribute }) filter: boolean | undefined;
/**
* Name of the input element.
* @group Props
*/
@Input() name: string | undefined;
/**
* Inline style of the element.
* @group Props
*/
@Input() style: { [klass: string]: any } | null | undefined;
/**
* Inline style of the overlay panel element.
* @group Props
*/
@Input() panelStyle: { [klass: string]: any } | null | undefined;
/**
* Style class of the element.
* @group Props
*/
@Input() styleClass: string | undefined;
/**
* Style class of the overlay panel element.
* @group Props
*/
@Input() panelStyleClass: string | undefined;
/**
* When present, it specifies that the component cannot be edited.
* @group Props
*/
@Input({ transform: booleanAttribute }) readonly: boolean | undefined;
/**
* When present, it specifies that an input field must be filled out before submitting the form.
* @group Props
*/
@Input({ transform: booleanAttribute }) required: boolean | undefined;
/**
* When present, custom value instead of predefined options can be entered using the editable input field.
* @group Props
*/
@Input({ transform: booleanAttribute }) editable: boolean | undefined;
/**
* 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;
/**
* Index of the element in tabbing order.
* @group Props
*/
@Input({ transform: numberAttribute }) tabindex: number | undefined = 0;
/**
* Default text to display when no option is selected.
* @group Props
*/
@Input() set placeholder(val: string | undefined) {
this._placeholder.set(val);
}
get placeholder(): Signal<string | undefined> {
return this._placeholder.asReadonly();
}
/**
* Icon to display in loading state.
* @group Props
*/
@Input() loadingIcon: string | undefined;
/**
* Placeholder text to show when filter input is empty.
* @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 input variant of the component.
* @group Props
*/
@Input() variant: 'filled' | 'outlined' = 'outlined';
/**
* Identifier of the accessible input element.
* @group Props
*/
@Input() inputId: string | undefined;
/**
* A property to uniquely identify a value in options.
* @group Props
*/
@Input() dataKey: string | undefined;
/**
* When filtering is enabled, filterBy decides which field or fields (comma separated) to search against.
* @group Props
*/
@Input() filterBy: string | undefined;
/**
* Fields used when filtering the options, defaults to optionLabel.
* @group Props
*/
@Input() filterFields: any[] | undefined;
/**
* When present, it specifies that the component should automatically get focus on load.
* @group Props
*/
@Input({ transform: booleanAttribute }) autofocus: boolean | undefined;
/**
* Clears the filter value when hiding the dropdown.
* @group Props
*/
@Input({ transform: booleanAttribute }) resetFilterOnHide: boolean = false;
/**
* Whether the selected option will be shown with a check mark.
* @group Props
*/
@Input({ transform: booleanAttribute }) checkmark: boolean = false;
/**
* Icon class of the dropdown icon.
* @group Props
*/
@Input() dropdownIcon: string | undefined;
/**
* Whether the dropdown is in loading state.
* @group Props
*/
@Input({ transform: booleanAttribute }) loading: boolean | undefined = false;
/**
* 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 display the first item as the label if no placeholder is defined and value is null.
* @deprecated since v17.3.0, set initial value by model instead.
* @group Props
*/
@Input({ transform: booleanAttribute }) autoDisplayFirst: boolean = true;
/**
* Whether to display options as grouped when nested options are provided.
* @group Props
*/
@Input({ transform: booleanAttribute }) group: boolean | undefined;
/**
* When enabled, a clear icon is displayed to clear the value.
* @group Props
*/
@Input({ transform: booleanAttribute }) showClear: boolean | undefined;
/**
* Text to display when filtering does not return any results. Defaults to global value in i18n translation configuration.
* @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 = '';
/**
* 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;
/**
* Height of an item in the list for VirtualScrolling.
* @group Props
*/
@Input({ transform: numberAttribute }) virtualScrollItemSize: number | 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;
/**
* Used to define a aria label attribute the current element.
* @group Props
*/
@Input() ariaLabel: 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;
/**
* Defines how the items are filtered.
* @group Props
*/
@Input() filterMatchMode: 'contains' | 'startsWith' | 'endsWith' | 'equals' | 'notEquals' | 'in' | 'lt' | 'lte' | 'gt' | 'gte' = 'contains';
/**
* Maximum number of character allows in the editable input field.
* @group Props
*/
@Input({ transform: numberAttribute }) maxlength: number | undefined;
/**
* 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;
/**
* Fields used when filtering the options, defaults to optionLabel.
* @group Props
*/
@Input({ transform: booleanAttribute }) focusOnHover: boolean = false;
/**
* 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;
/**
* Applies focus to the filter element when the overlay is shown.
* @group Props
*/
@Input({ transform: booleanAttribute }) autofocusFilter: boolean = true;
/**
* Determines if the panel will be shown when the input is focused and receives a character key down event.
* @group Props
*/
@Input({ transform: booleanAttribute }) autoShowPanelOnPrintableCharacterKeyDown: boolean = true;
/**
* When present, it specifies that the component should be disabled.
* @group Props
*/
@Input() get disabled(): boolean | undefined {
return this._disabled;
}
set disabled(_disabled: boolean | undefined) {
if (_disabled) {
this.focused = false;
if (this.overlayVisible) this.hide();
}
this._disabled = _disabled;
if (!(this.cd as ViewRef).destroyed) {
this.cd.detectChanges();
}
}
/**
* 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.');
}
_itemSize: number | undefined;
/**
* Whether to automatically manage layering.
* @group Props
* @deprecated since v14.2.0, use overlayOptions property instead.
*/
@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.');
}
_autoZIndex: boolean | undefined;
/**
* Base zIndex value to use in layering.
* @group Props
* @deprecated since v14.2.0, use overlayOptions property instead.
*/
@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.');
}
_baseZIndex: number | undefined;
/**
* 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.');
}
_showTransitionOptions: string | undefined;
/**
* 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.');
}
_hideTransitionOptions: string | undefined;
/**
* When specified, filter displays with this value.
* @group Props
*/
@Input() get filterValue(): string | undefined | null {
return this._filterValue();
}
set filterValue(val: string | undefined | null) {
setTimeout(() => {
this._filterValue.set(val);
});
}
/**
* 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(val, this._options())) {
this._options.set(val);
}
}
/**
* Callback to invoke when value of dropdown changes.
* @param {DropdownChangeEvent} event - custom change event.
* @group Emits
*/
@Output() onChange: EventEmitter<DropdownChangeEvent> = new EventEmitter<DropdownChangeEvent>();
/**
* Callback to invoke when data is filtered.
* @param {DropdownFilterEvent} event - custom filter event.
* @group Emits
*/
@Output() onFilter: EventEmitter<DropdownFilterEvent> = new EventEmitter<DropdownFilterEvent>();
/**
* Callback to invoke when dropdown gets focus.
* @param {Event} event - Browser event.
* @group Emits
*/
@Output() onFocus: EventEmitter<Event> = new EventEmitter<Event>();
/**
* Callback to invoke when dropdown loses focus.
* @param {Event} event - Browser event.
* @group Emits
*/
@Output() onBlur: EventEmitter<Event> = new EventEmitter<Event>();
/**
* Callback to invoke when component is clicked.
* @param {MouseEvent} event - Mouse event.
* @group Emits
*/
@Output() onClick: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
/**
* Callback to invoke when dropdown overlay gets visible.
* @param {AnimationEvent} event - Animation event.
* @group Emits
*/
@Output() onShow: EventEmitter<AnimationEvent> = new EventEmitter<AnimationEvent>();
/**
* Callback to invoke when dropdown overlay gets hidden.
* @param {AnimationEvent} event - Animation event.
* @group Emits
*/
@Output() onHide: EventEmitter<AnimationEvent> = new EventEmitter<AnimationEvent>();
/**
* Callback to invoke when dropdown clears the value.
* @param {Event} event - Browser event.
* @group Emits
*/
@Output() onClear: EventEmitter<Event> = new EventEmitter<Event>();
/**
* Callback to invoke in lazy mode to load new data.
* @param {DropdownLazyLoadEvent} event - Lazy load event.
* @group Emits
*/
@Output() onLazyLoad: EventEmitter<DropdownLazyLoadEvent> = new EventEmitter<DropdownLazyLoadEvent>();
@ViewChild('container') containerViewChild: Nullable<ElementRef>;
@ViewChild('filter') filterViewChild: Nullable<ElementRef>;
@ViewChild('focusInput') focusInputViewChild: Nullable<ElementRef>;
@ViewChild('editableInput') editableInputViewChild: Nullable<ElementRef>;
@ViewChild('items') itemsViewChild: Nullable<ElementRef>;
@ViewChild('scroller') scroller: Nullable<Scroller>;
@ViewChild('overlay') overlayViewChild: Nullable<Overlay>;
@ViewChild('firstHiddenFocusableEl') firstHiddenFocusableElementOnOverlay: Nullable<ElementRef>;
@ViewChild('lastHiddenFocusableEl') lastHiddenFocusableElementOnOverlay: Nullable<ElementRef>;
@ContentChildren(PrimeTemplate) templates: Nullable<QueryList<PrimeTemplate>>;
_disabled: boolean | undefined;
itemsWrapper: Nullable<HTMLDivElement>;
itemTemplate: Nullable<TemplateRef<any>>;
groupTemplate: Nullable<TemplateRef<any>>;
loaderTemplate: Nullable<TemplateRef<any>>;
selectedItemTemplate: Nullable<TemplateRef<any>>;
headerTemplate: Nullable<TemplateRef<any>>;
filterTemplate: Nullable<TemplateRef<any>>;
footerTemplate: Nullable<TemplateRef<any>>;
emptyFilterTemplate: Nullable<TemplateRef<any>>;
emptyTemplate: Nullable<TemplateRef<any>>;
dropdownIconTemplate: Nullable<TemplateRef<any>>;
loadingIconTemplate: Nullable<TemplateRef<any>>;
clearIconTemplate: Nullable<TemplateRef<any>>;
filterIconTemplate: Nullable<TemplateRef<any>>;
filterOptions: DropdownFilterOptions | undefined;
_options = signal<any[] | undefined>(null);
_placeholder = signal<string | undefined>(undefined);
modelValue = signal<any>(null);
value: any;
onModelChange: Function = () => {};
onModelTouched: Function = () => {};
hover: Nullable<boolean>;
focused: Nullable<boolean>;
overlayVisible: Nullable<boolean>;
optionsChanged: Nullable<boolean>;
panel: Nullable<HTMLDivElement>;
dimensionsUpdated: Nullable<boolean>;
hoveredItem: any;
selectedOptionUpdated: Nullable<boolean>;
_filterValue = signal<any>(null);
searchValue: Nullable<string>;
searchIndex: Nullable<number>;
searchTimeout: any;
previousSearchChar: Nullable<string>;
currentSearchChar: Nullable<string>;
preventModelTouched: Nullable<boolean>;
focusedOptionIndex = signal<number>(-1);
labelId: Nullable<string>;
listId: Nullable<string>;
clicked = signal<boolean>(false);
get emptyMessageLabel(): string {
return this.emptyMessage || this.config.getTranslation(TranslationKeys.EMPTY_MESSAGE);
}
get emptyFilterMessageLabel(): string {
return this.emptyFilterMessage || this.config.getTranslation(TranslationKeys.EMPTY_FILTER_MESSAGE);
}
get isVisibleClearIcon(): boolean | undefined {
return this.modelValue() != null && this.hasSelectedOption() && this.showClear && !this.disabled;
}
get listLabel(): string {
return this.config.getTranslation(TranslationKeys.ARIA)['listLabel'];
}
get containerClass() {
return {
'p-dropdown p-component p-inputwrapper': true,
'p-disabled': this.disabled,
'p-dropdown-clearable': this.showClear && !this.disabled,
'p-focus': this.focused,
'p-inputwrapper-filled': this.modelValue() !== undefined && this.modelValue() !== null && !this.modelValue().length,
'p-inputwrapper-focus': this.focused || this.overlayVisible,
'p-variant-filled': this.variant === 'filled' || this.config.inputStyle() === 'filled',
'p-dropdown-open': this.overlayVisible
};
}
get inputClass() {
const label = this.label();
return {
'p-dropdown-label p-inputtext': true,
'p-placeholder': this.placeholder() && label === this.placeholder(),
'p-dropdown-label-empty': !this.editable && !this.selectedItemTemplate && (label === undefined || label === null || label === 'p-emptylabel' || label.length === 0)
};
}
get panelClass() {
return {
'p-dropdown-panel p-component': true,
'p-input-filled': this.config.inputStyle() === 'filled',
'p-ripple-disabled': this.config.ripple === false
};
}
get focusedOptionId() {
return this.focusedOptionIndex() !== -1 ? `${this.id}_${this.focusedOptionIndex()}` : null;
}
visibleOptions = computed(() => {
const options = this.getAllVisibleAndNonVisibleOptions();
if (this._filterValue()) {
const _filterBy = this.filterBy || this.optionLabel;
const filteredOptions =
!_filterBy && !this.filterFields && !this.optionValue
? this.options.filter((option) => {
if (option.label) {
return option.label.toString().toLowerCase().indexOf(this._filterValue().toLowerCase().trim()) !== -1;
}
return option.toString().toLowerCase().indexOf(this._filterValue().toLowerCase().trim()) !== -1;
})
: this.filterService.filter(options, this.searchFields(), this._filterValue().trim(), this.filterMatchMode, this.filterLocale);
if (this.group) {
const optionGroups = this.options || [];
const filtered = [];
optionGroups.forEach((group) => {
const groupChildren = this.getOptionGroupChildren(group);
const filteredItems = groupChildren.filter((item) => filteredOptions.includes(item));
if (filteredItems.length > 0) filtered.push({ ...group, [typeof this.optionGroupChildren === 'string' ? this.optionGroupChildren : 'items']: [...filteredItems] });
});
return this.flatOptions(filtered);
}