TabSelect

带有描述和滚动支持的水平标签式选择组件。组件必须获得焦点才能接收键盘输入。

基本用法

Renderable API

import { TabSelectRenderable, TabSelectRenderableEvents, createCliRenderer } from "@opentui/core"

const renderer = await createCliRenderer()

const tabs = new TabSelectRenderable(renderer, {
  id: "tabs",
  width: 60,
  options: [
    { name: "Home", description: "Dashboard and overview" },
    { name: "Files", description: "File management" },
    { name: "Settings", description: "Application settings" },
  ],
  tabWidth: 20,
})

tabs.on(TabSelectRenderableEvents.ITEM_SELECTED, (index, option) => {
  console.log("Tab selected:", option.name)
})

tabs.focus()
renderer.root.add(tabs)

Construct API

import { TabSelect, createCliRenderer } from "@opentui/core"

const renderer = await createCliRenderer()

const tabs = TabSelect({
  width: 60,
  tabWidth: 15,
  options: [
    { name: "Tab 1", description: "First tab" },
    { name: "Tab 2", description: "Second tab" },
    { name: "Tab 3", description: "Third tab" },
  ],
})

tabs.focus()
renderer.root.add(tabs)

键盘导航

当获得焦点时,TabSelect 响应以下按键:

按键操作
Left / [移动到上一个标签
Right / ]移动到下一个标签
Enter选择当前标签

事件

选项选中

当用户在某个标签上按下 Enter 时触发:

import { TabSelectRenderableEvents, type TabSelectOption } from "@opentui/core"

tabs.on(TabSelectRenderableEvents.ITEM_SELECTED, (index: number, option: TabSelectOption) => {
  console.log(`Selected tab ${index}: ${option.name}`)
  // Switch to the corresponding panel
})

选择变更

当高亮标签发生变化时触发:

import { TabSelectRenderableEvents, type TabSelectOption } from "@opentui/core"

tabs.on(TabSelectRenderableEvents.SELECTION_CHANGED, (index: number, option: TabSelectOption) => {
  console.log(`Hovering: ${option.name}`)
})

属性

属性类型默认值描述
widthnumber-组件总宽度
optionsTabSelectOption[][]可用标签
tabWidthnumber20每个标签的宽度
backgroundColorstring | RGBAtransparent背景颜色
textColorstring | RGBA#FFFFFF普通标签文本颜色
focusedBackgroundColorstring | RGBA#1a1a1a聚焦时的背景颜色
focusedTextColorstring | RGBA#FFFFFF聚焦时的文本颜色
selectedBackgroundColorstring | RGBA#334455选中标签的背景颜色
selectedTextColorstring | RGBA#FFFF00选中标签的文本颜色
selectedDescriptionColorstring | RGBA#CCCCCC描述文本颜色
showScrollArrowsbooleantrue显示滚动指示器
showDescriptionbooleantrue显示标签描述
showUnderlinebooleantrue在选中标签上显示下划线
wrapSelectionbooleanfalse导航时循环选择
keyBindingsTabSelectKeyBinding[]-自定义按键绑定
keyAliasMapRecord<string, string>-按键别名映射

示例:标签页界面

import { Box, Text, TabSelect, createCliRenderer } from "@opentui/core"

const renderer = await createCliRenderer()

// Create content panels
const panels = {
  home: Box({ padding: 1 }, Text({ content: "Home content here" })),
  files: Box({ padding: 1 }, Text({ content: "File browser here" })),
  settings: Box({ padding: 1 }, Text({ content: "Settings form here" })),
}

// Create the tabbed container
const container = Box({
  width: 60,
  height: 20,
  borderStyle: "rounded",
})

const tabs = TabSelect({
  width: 60,
  tabWidth: 20,
  options: [
    { name: "Home", description: "Dashboard" },
    { name: "Files", description: "Browse files" },
    { name: "Settings", description: "Preferences" },
  ],
})

// Content area
let currentPanel = panels.home
const contentArea = Box({
  flexGrow: 1,
  padding: 1,
})
contentArea.add(currentPanel)

// Handle tab changes
tabs.on("itemSelected", (index, option) => {
  // Remove current panel
  if (currentPanel) {
    contentArea.remove(currentPanel.id)
  }
  // Add new panel based on selection
  switch (option.name) {
    case "Home":
      currentPanel = panels.home
      break
    case "Files":
      currentPanel = panels.files
      break
    case "Settings":
      currentPanel = panels.settings
      break
  }
  contentArea.add(currentPanel)
})

container.add(tabs)
container.add(contentArea)

tabs.focus()
renderer.root.add(container)

编程控制

// Get current tab index
const currentIndex = tabs.getSelectedIndex()

// Set tab programmatically
tabs.setSelectedIndex(1)

// Update tabs dynamically
tabs.setOptions([
  { name: "New Tab 1", description: "Updated" },
  { name: "New Tab 2", description: "Also updated" },
])

滚动行为

当标签数量超出组件宽度时,组件会在你使用键盘导航时自动处理水平滚动。