Input
带有光标、占位文本和焦点状态的文本输入框。聚焦输入框后即可接收键盘输入。
基本用法
Renderable API
import { InputRenderable, InputRenderableEvents, createCliRenderer } from "@opentui/core"
const renderer = await createCliRenderer()
const input = new InputRenderable(renderer, {
id: "name-input",
width: 25,
placeholder: "Enter your name...",
})
input.on(InputRenderableEvents.CHANGE, (value) => {
console.log("Input value:", value)
})
input.focus()
renderer.root.add(input)
Construct API
import { Input, createCliRenderer } from "@opentui/core"
const renderer = await createCliRenderer()
const input = Input({
placeholder: "Enter your name...",
width: 25,
})
input.focus()
renderer.root.add(input)
焦点状态
输入框在获得焦点时会改变外观:
const input = new InputRenderable(renderer, {
id: "styled-input",
width: 30,
placeholder: "Type here...",
backgroundColor: "#1a1a1a",
focusedBackgroundColor: "#2a2a2a",
textColor: "#FFFFFF",
cursorColor: "#00FF00",
})
事件
Input 事件
在每次按键值发生变化时触发:
import { InputRenderableEvents } from "@opentui/core"
input.on(InputRenderableEvents.INPUT, (value: string) => {
console.log("Current value:", value)
})
Change 事件
在输入框失去焦点(blur)或按下回车键时触发,但仅在自获得焦点以来值发生变化时触发:
input.on(InputRenderableEvents.CHANGE, (value: string) => {
console.log("Value committed:", value)
})
Enter 事件
在用户按下回车键时触发:
input.on(InputRenderableEvents.ENTER, (value: string) => {
console.log("Submitted value:", value)
})
获取当前值
const currentValue = input.value
设置值
input.value = "New value"
属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
width | number | - | 输入框宽度 |
value | string | "" | 初始文本值 |
placeholder | string | "" | 空值时的占位文本 |
maxLength | number | 1000 | 最大字符数 |
backgroundColor | string | RGBA | - | 未聚焦时的背景色 |
focusedBackgroundColor | string | RGBA | - | 聚焦时的背景色 |
textColor | string | RGBA | - | 文本颜色 |
cursorColor | string | RGBA | - | 光标颜色 |
position | "static" | "relative" | "absolute" | "relative" | 定位模式 |
示例:登录表单
import { Box, Text, Input, delegate, createCliRenderer } from "@opentui/core"
const renderer = await createCliRenderer()
function LabeledInput(props: { id: string; label: string; placeholder: string }) {
return delegate(
{ focus: `${props.id}-input` },
Box(
{ flexDirection: "row", marginBottom: 1 },
Text({ content: props.label.padEnd(12), fg: "#888888" }),
Input({
id: `${props.id}-input`,
placeholder: props.placeholder,
width: 20,
backgroundColor: "#222",
focusedBackgroundColor: "#333",
textColor: "#FFF",
cursorColor: "#0F0",
}),
),
)
}
const usernameInput = LabeledInput({
id: "username",
label: "Username:",
placeholder: "Enter username",
})
const passwordInput = LabeledInput({
id: "password",
label: "Password:",
placeholder: "Enter password",
})
const form = Box(
{
width: 40,
borderStyle: "rounded",
title: "Login",
padding: 1,
},
usernameInput,
passwordInput,
)
// Focus the username input
usernameInput.focus()
renderer.root.add(form)
Tab 导航
在输入框之间添加 Tab 导航:
const inputs = [usernameInput, passwordInput]
let focusIndex = 0
renderer.keyInput.on("keypress", (key) => {
if (key.name === "tab") {
focusIndex = (focusIndex + 1) % inputs.length
inputs[focusIndex].focus()
}
})