From 0baba36f7f0034419607ddeeeb83f6e5b53efad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4DELTA?= Date: Mon, 20 Apr 2026 10:02:33 +0900 Subject: [PATCH] Make Dropdown to Clear Its Input History Everytime --- src/app/components/dropdown/dropdown.html | 8 ++++---- src/app/components/dropdown/dropdown.ts | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/app/components/dropdown/dropdown.html b/src/app/components/dropdown/dropdown.html index 5dc01e7..3359dd7 100644 --- a/src/app/components/dropdown/dropdown.html +++ b/src/app/components/dropdown/dropdown.html @@ -3,12 +3,12 @@ {{ icon() }} } - - - @if (!isOpen()) { + (input)="onSearch($event)" (click)="handleInputClick($event)" (mousedown)="handleInputMouseDown()" (beforeinput)="preprocessInputContent($event)">{{ this.initialInputText() }} + } @else { {{ selectedLabel() }} } keyboard_arrow_down diff --git a/src/app/components/dropdown/dropdown.ts b/src/app/components/dropdown/dropdown.ts index d2ec326..481a254 100644 --- a/src/app/components/dropdown/dropdown.ts +++ b/src/app/components/dropdown/dropdown.ts @@ -38,7 +38,7 @@ export interface DropdownOption { '[class.open]': 'isOpen()', tabindex: '0', '(click)': 'toggle()', - '(focus)': 'onFocus($event)', + '(focus)': 'onFocus()', '(mousedown)': 'onMouseDown($event)', '(keydown)': 'onKeyDown($event)', '(keydown.enter)': 'onEnter($event)', @@ -79,9 +79,6 @@ export class Dropdown implements OnInit { if (!isDropdownOpen) { if (untracked(this.inputFocusMode)) this.inputFocusMode.set(false); 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)); }); + initialInputText = computed(() => { + let currentValue = this.normalizedOptions().find((o) => o.value === this.value())?.label; + return currentValue ? currentValue : ""; + }) + toggle() { if (this.isLatestMouseDownInsideInput) { this.isLatestMouseDownInsideInput = false; @@ -124,18 +126,20 @@ export class Dropdown implements OnInit { this.isOpen.update((v) => !v); if (this.isOpen()) { 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; searchInputElement.focus(); this.setCursorToEnd(searchInputElement); - }); + }, 0); } } - onFocus(event: Event) { + onFocus() { if (this.isOpen()) { const searchInputElement = this.searchInput()!.nativeElement;