| 38 | } |
| 39 | |
| 40 | export class SelectList implements Component { |
| 41 | private items: SelectItem[] = []; |
| 42 | private filteredItems: SelectItem[] = []; |
| 43 | private selectedIndex: number = 0; |
| 44 | private maxVisible: number = 5; |
| 45 | private theme: SelectListTheme; |
| 46 | private layout: SelectListLayoutOptions; |
| 47 | |
| 48 | public onSelect?: (item: SelectItem) => void; |
| 49 | public onCancel?: () => void; |
| 50 | public onSelectionChange?: (item: SelectItem) => void; |
| 51 | |
| 52 | constructor(items: SelectItem[], maxVisible: number, theme: SelectListTheme, layout: SelectListLayoutOptions = {}) { |
| 53 | this.items = items; |
| 54 | this.filteredItems = items; |
| 55 | this.maxVisible = maxVisible; |
| 56 | this.theme = theme; |
| 57 | this.layout = layout; |
| 58 | } |
| 59 | |
| 60 | setFilter(filter: string): void { |
| 61 | this.filteredItems = this.items.filter((item) => item.value.toLowerCase().startsWith(filter.toLowerCase())); |
| 62 | // Reset selection when filter changes |
| 63 | this.selectedIndex = 0; |
| 64 | } |
| 65 | |
| 66 | setSelectedIndex(index: number): void { |
| 67 | this.selectedIndex = Math.max(0, Math.min(index, this.filteredItems.length - 1)); |
| 68 | } |
| 69 | |
| 70 | invalidate(): void { |
| 71 | // No cached state to invalidate currently |
| 72 | } |
| 73 | |
| 74 | render(width: number): string[] { |
| 75 | const lines: string[] = []; |
| 76 | |
| 77 | // If no items match filter, show message |
| 78 | if (this.filteredItems.length === 0) { |
| 79 | lines.push(this.theme.noMatch(" No matching commands")); |
| 80 | return lines; |
| 81 | } |
| 82 | |
| 83 | const primaryColumnWidth = this.getPrimaryColumnWidth(); |
| 84 | |
| 85 | // Calculate visible range with scrolling |
| 86 | const startIndex = Math.max( |
| 87 | 0, |
| 88 | Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible), |
| 89 | ); |
| 90 | const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length); |
| 91 | |
| 92 | // Render visible items |
| 93 | for (let i = startIndex; i < endIndex; i++) { |
| 94 | const item = this.filteredItems[i]; |
| 95 | if (!item) continue; |
| 96 | |
| 97 | const isSelected = i === this.selectedIndex; |
nothing calls this directly
no outgoing calls
no test coverage detected