Add Dropdown Default Feature

This commit is contained in:
copecone committed 2026-03-17 21:27:47 +09:00
1 parent 88f72e767f
commit f71dc03f67
4 files changed
+25 -6

No files matched your search

+13 -2
View File
@@ -1,4 +1,4 @@
import { Component, input, model, output, signal, HostListener, ElementRef, computed
import { Component, input, model, output, signal, HostListener, ElementRef, computed, OnInit
} from '@angular/core'
import { CommonModule } from '@angular/common'
@@ -30,11 +30,12 @@ export interface DropdownOption {
'(keydown.arrowup)': 'onArrowUp($event)'
}
})
export class Dropdown {
export class Dropdown implements OnInit {
options = input<Array<DropdownOption | string>>([]);
placeholder = input<string>('Select an option');
ariaLabel = input<string | undefined>(undefined);
icon = input<string>();
defaultIndex = input<number>();
value = model<any>(null);
selectionChange = output<any>();
@@ -46,6 +47,16 @@ export class Dropdown {
constructor(private el: ElementRef) {}
ngOnInit() {
const opts = this.normalizedOptions();
const dIndex = this.defaultIndex();
const currentValue = this.value();
if (currentValue === null && dIndex !== undefined && dIndex >= 0 && dIndex < opts.length) {
this.value.set(opts[dIndex].value);
}
}
normalizedOptions = computed(() => {
return this.options().map(opt => {
if (typeof opt === 'string') {