Make Dropdown to Clear Its Input History Everytime

This commit is contained in:
copecone committed 2026-04-20 10:02:33 +09:00
1 parent dceac30c39
commit 0baba36f7f
2 files changed
+15 -11

No files matched your search

+3 -3
View File
@@ -3,12 +3,12 @@
<span class="material-symbols-outlined icon-24 prefix-icon">{{ icon() }}</span> <span class="material-symbols-outlined icon-24 prefix-icon">{{ icon() }}</span>
} }
@if (isOpen()) {
<div #searchInput class="dropdown-input" [class.hidden]="!isOpen()" [class.focus-mode]="inputFocusMode()" <div #searchInput class="dropdown-input" [class.hidden]="!isOpen()" [class.focus-mode]="inputFocusMode()"
contenteditable="true" role="textbox" aria-multiline="false" [aria-placeholder]="translation.common.selectOption" contenteditable="true" role="textbox" aria-multiline="false" [aria-placeholder]="translation.common.selectOption"
[attr.placeholder]="translation.common.selectOption" tabindex="-1" spellcheck="false" [attr.placeholder]="translation.common.selectOption" tabindex="-1" spellcheck="false"
(input)="onSearch($event)" (click)="handleInputClick($event)" (mousedown)="handleInputMouseDown()" (beforeinput)="preprocessInputContent($event)"></div> (input)="onSearch($event)" (click)="handleInputClick($event)" (mousedown)="handleInputMouseDown()" (beforeinput)="preprocessInputContent($event)">{{ this.initialInputText() }}</div>
} @else {
@if (!isOpen()) {
<span class="placeholder">{{ selectedLabel() }}</span> <span class="placeholder">{{ selectedLabel() }}</span>
} }
<span class="material-symbols-outlined icon-24 chevron" [class.open]="isOpen()">keyboard_arrow_down</span> <span class="material-symbols-outlined icon-24 chevron" [class.open]="isOpen()">keyboard_arrow_down</span>
+12 -8
View File
@@ -38,7 +38,7 @@ export interface DropdownOption {
'[class.open]': 'isOpen()', '[class.open]': 'isOpen()',
tabindex: '0', tabindex: '0',
'(click)': 'toggle()', '(click)': 'toggle()',
'(focus)': 'onFocus($event)', '(focus)': 'onFocus()',
'(mousedown)': 'onMouseDown($event)', '(mousedown)': 'onMouseDown($event)',
'(keydown)': 'onKeyDown($event)', '(keydown)': 'onKeyDown($event)',
'(keydown.enter)': 'onEnter($event)', '(keydown.enter)': 'onEnter($event)',
@@ -79,9 +79,6 @@ export class Dropdown implements OnInit {
if (!isDropdownOpen) { if (!isDropdownOpen) {
if (untracked(this.inputFocusMode)) this.inputFocusMode.set(false); if (untracked(this.inputFocusMode)) this.inputFocusMode.set(false);
this.searchQuery.set(''); this.searchQuery.set('');
let currentValue = untracked(this.normalizedOptions).find((o) => o.value === this.value());
if (currentValue) this.searchInput()!.nativeElement.innerText = currentValue.label;
} }
}); });
} }
@@ -115,6 +112,11 @@ export class Dropdown implements OnInit {
return opts.filter((o) => o.label.toLowerCase().normalize('NFKD').includes(query)); return opts.filter((o) => o.label.toLowerCase().normalize('NFKD').includes(query));
}); });
initialInputText = computed<string>(() => {
let currentValue = this.normalizedOptions().find((o) => o.value === this.value())?.label;
return currentValue ? currentValue : "";
})
toggle() { toggle() {
if (this.isLatestMouseDownInsideInput) { if (this.isLatestMouseDownInsideInput) {
this.isLatestMouseDownInsideInput = false; this.isLatestMouseDownInsideInput = false;
@@ -124,18 +126,20 @@ export class Dropdown implements OnInit {
this.isOpen.update((v) => !v); this.isOpen.update((v) => !v);
if (this.isOpen()) { if (this.isOpen()) {
this.searchQuery.set(''); this.searchQuery.set('');
this.focusedIndex.set(this.normalizedOptions().findIndex((o) => o.value === this.value()));
requestAnimationFrame(() => { const currentIndex = this.normalizedOptions().findIndex((o) => o.value === this.value());
this.focusedIndex.set(currentIndex);
setTimeout(() => {
const searchInputElement = this.searchInput()!.nativeElement; const searchInputElement = this.searchInput()!.nativeElement;
searchInputElement.focus(); searchInputElement.focus();
this.setCursorToEnd(searchInputElement); this.setCursorToEnd(searchInputElement);
}); }, 0);
} }
} }
onFocus(event: Event) { onFocus() {
if (this.isOpen()) { if (this.isOpen()) {
const searchInputElement = this.searchInput()!.nativeElement; const searchInputElement = this.searchInput()!.nativeElement;