| 8 | * Follows Single Responsibility Principle - only handles UI rendering and user interaction. |
| 9 | */ |
| 10 | export class SearchBox { |
| 11 | private container: HTMLElement; |
| 12 | private onSearch: (term: string) => void; |
| 13 | private debounceMs: number; |
| 14 | |
| 15 | private searchBoxEl: HTMLElement | null = null; |
| 16 | private inputEl: HTMLInputElement | null = null; |
| 17 | private clearBtnEl: HTMLElement | null = null; |
| 18 | |
| 19 | private debouncedSearch: ((term: string) => void) | null = null; |
| 20 | private destroyed = false; |
| 21 | |
| 22 | /** |
| 23 | * @param container - Parent container element |
| 24 | * @param onSearch - Callback function called when search term changes |
| 25 | * @param debounceMs - Debounce delay in milliseconds (default: 300) |
| 26 | */ |
| 27 | constructor( |
| 28 | container: HTMLElement, |
| 29 | onSearch: (term: string) => void, |
| 30 | debounceMs = 300 |
| 31 | ) { |
| 32 | this.container = container; |
| 33 | this.onSearch = onSearch; |
| 34 | this.debounceMs = debounceMs; |
| 35 | |
| 36 | // Create debounced search handler with destroyed check |
| 37 | this.debouncedSearch = debounce( |
| 38 | (term: string) => { |
| 39 | // Don't execute if component has been destroyed |
| 40 | if (!this.destroyed) { |
| 41 | this.onSearch(term); |
| 42 | } |
| 43 | }, |
| 44 | this.debounceMs, |
| 45 | false // trailing debounce |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Render the search box UI |
| 51 | */ |
| 52 | render(): HTMLElement { |
| 53 | // Use container's document for pop-out window support |
| 54 | const doc = this.container.ownerDocument; |
| 55 | |
| 56 | // Create main container |
| 57 | this.searchBoxEl = doc.createElement('div'); |
| 58 | this.searchBoxEl.className = 'tn-search-box'; |
| 59 | |
| 60 | // Create input wrapper |
| 61 | const inputWrapper = doc.createElement('div'); |
| 62 | inputWrapper.className = 'tn-search-box__input-wrapper'; |
| 63 | |
| 64 | // Create search icon using Lucide icon (like Obsidian) |
| 65 | const icon = doc.createElement('div'); |
| 66 | icon.className = 'tn-search-box__icon'; |
| 67 | setIcon(icon, 'search'); |
nothing calls this directly
no test coverage detected