select.mdx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ---
  2. title: Select
  3. description: List selection component for choosing from options
  4. order: 5
  5. ---
  6. # Select
  7. A vertical list for choosing from multiple options. Focus the select to enable keyboard input.
  8. ## Basic usage
  9. ### Renderable API
  10. ```typescript
  11. import { SelectRenderable, SelectRenderableEvents, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const menu = new SelectRenderable(renderer, {
  14. id: "menu",
  15. width: 30,
  16. height: 8,
  17. options: [
  18. { name: "New File", description: "Create a new file" },
  19. { name: "Open File", description: "Open an existing file" },
  20. { name: "Save", description: "Save current file" },
  21. { name: "Exit", description: "Exit the application" },
  22. ],
  23. })
  24. menu.on(SelectRenderableEvents.ITEM_SELECTED, (index, option) => {
  25. console.log("Selected:", option.name)
  26. })
  27. menu.focus()
  28. renderer.root.add(menu)
  29. ```
  30. ### Construct API
  31. ```typescript
  32. import { Select, createCliRenderer } from "@opentui/core"
  33. const renderer = await createCliRenderer()
  34. const menu = Select({
  35. width: 30,
  36. height: 8,
  37. options: [
  38. { name: "Option 1", description: "First option" },
  39. { name: "Option 2", description: "Second option" },
  40. { name: "Option 3", description: "Third option" },
  41. ],
  42. })
  43. menu.focus()
  44. renderer.root.add(menu)
  45. ```
  46. ## Keyboard navigation
  47. When focused, the select responds to these keys:
  48. | Key | Action |
  49. | ------------------------- | --------------------- |
  50. | `Up` / `k` | Move selection up |
  51. | `Down` / `j` | Move selection down |
  52. | `Shift+Up` / `Shift+Down` | Fast scroll (5 items) |
  53. | `Enter` | Select current item |
  54. ## Events
  55. ### Item selected
  56. Fires when the user presses Enter on an option:
  57. ```typescript
  58. import { SelectRenderableEvents } from "@opentui/core"
  59. menu.on(SelectRenderableEvents.ITEM_SELECTED, (index: number, option: SelectOption) => {
  60. console.log(`Selected index ${index}: ${option.name}`)
  61. })
  62. ```
  63. ### Selection changed
  64. Fires when the highlighted item changes:
  65. ```typescript
  66. menu.on(SelectRenderableEvents.SELECTION_CHANGED, (index: number, option: SelectOption) => {
  67. console.log(`Highlighted: ${option.name}`)
  68. // Update a preview pane, for example
  69. })
  70. ```
  71. ## Option structure
  72. ```typescript
  73. interface SelectOption {
  74. name: string // Display text
  75. description: string // Displays below the name when showDescription is true
  76. value?: any // Optional value
  77. }
  78. ```
  79. ## Styling
  80. ```typescript
  81. const styledMenu = new SelectRenderable(renderer, {
  82. id: "styled-menu",
  83. width: 40,
  84. height: 10,
  85. options: [...],
  86. backgroundColor: "#1a1a1a",
  87. selectedBackgroundColor: "#333366",
  88. selectedTextColor: "#FFFFFF",
  89. textColor: "#AAAAAA",
  90. descriptionColor: "#666666",
  91. })
  92. ```
  93. ## Properties
  94. | Property | Type | Default | Description |
  95. | -------------------------- | ---------------- | ------------- | --------------------------------- |
  96. | `width` | `number` | - | Component width |
  97. | `height` | `number` | - | Component height |
  98. | `options` | `SelectOption[]` | `[]` | Available options |
  99. | `selectedIndex` | `number` | `0` | Initially selected index |
  100. | `backgroundColor` | `string \| RGBA` | `transparent` | Background color |
  101. | `textColor` | `string \| RGBA` | `#FFFFFF` | Normal text color |
  102. | `focusedBackgroundColor` | `string \| RGBA` | `#1a1a1a` | Background when focused |
  103. | `focusedTextColor` | `string \| RGBA` | `#FFFFFF` | Text color when focused |
  104. | `selectedBackgroundColor` | `string \| RGBA` | `#334455` | Selected item background |
  105. | `selectedTextColor` | `string \| RGBA` | `#FFFF00` | Selected item text color |
  106. | `descriptionColor` | `string \| RGBA` | `#888888` | Description text color |
  107. | `selectedDescriptionColor` | `string \| RGBA` | `#CCCCCC` | Selected item description color |
  108. | `showDescription` | `boolean` | `true` | Show option descriptions |
  109. | `showScrollIndicator` | `boolean` | `false` | Show scroll position indicator |
  110. | `wrapSelection` | `boolean` | `false` | Wrap selection at list boundaries |
  111. | `itemSpacing` | `number` | `0` | Spacing between items |
  112. | `fastScrollStep` | `number` | `5` | Items to skip with Shift+Up/Down |
  113. ## Example: file menu
  114. ```typescript
  115. import { Box, Select, createCliRenderer } from "@opentui/core"
  116. const renderer = await createCliRenderer()
  117. const fileMenu = Select({
  118. width: 25,
  119. height: 12,
  120. options: [
  121. { name: "New", description: "Create new file (Ctrl+N)" },
  122. { name: "Open...", description: "Open file (Ctrl+O)" },
  123. { name: "Save", description: "Save file (Ctrl+S)" },
  124. { name: "Save As...", description: "Save with new name" },
  125. { name: "---", description: "" }, // Separator (visual only)
  126. { name: "Exit", description: "Quit application (Ctrl+Q)" },
  127. ],
  128. })
  129. const menuPanel = Box(
  130. {
  131. borderStyle: "single",
  132. borderColor: "#666",
  133. },
  134. fileMenu,
  135. )
  136. fileMenu.focus()
  137. renderer.root.add(menuPanel)
  138. ```
  139. ## Programmatic control
  140. ```typescript
  141. // Get current selection index
  142. const currentIndex = menu.getSelectedIndex()
  143. // Get currently selected option
  144. const option = menu.getSelectedOption()
  145. // Set selection programmatically
  146. menu.setSelectedIndex(2)
  147. // Navigate programmatically
  148. menu.moveUp() // Move up one item
  149. menu.moveDown() // Move down one item
  150. menu.moveUp(3) // Move up multiple items
  151. menu.selectCurrent() // Trigger selection of current item
  152. // Update options dynamically
  153. menu.options = [
  154. { name: "New Option 1", description: "First" },
  155. { name: "New Option 2", description: "Second" },
  156. ]
  157. // Toggle display options
  158. menu.showDescription = false
  159. menu.showScrollIndicator = true
  160. menu.wrapSelection = true
  161. ```