Skip to content

ChatInput 输入栏 ​

用于 AI 聊天场景的输入组件,支持自动高度、思维链开关、模型选择。

基础用法 ​

tsx
import { ChatInput } from '@airo-ui/react'

function Demo() {
  return (
    <ChatInput
      placeholder="输入你的问题..."
      onSend={(value) => console.log('发送:', value)}
    />
  )
}

受控与非受控 ​

tsx
import { useState } from 'react'
import { ChatInput } from '@airo-ui/react'

function ControlledDemo() {
  const [value, setValue] = useState('')

  return (
    <ChatInput
      value={value}
      onChange={setValue}
      placeholder="受控输入栏"
      onSend={(val) => {
        console.log('发送:', val)
        setValue('')
      }}
    />
  )
}

function UncontrolledDemo() {
  return (
    <ChatInput
      defaultValue="初始内容"
      placeholder="非受控输入栏"
      onSend={(val) => console.log('发送:', val)}
    />
  )
}

自定义发送按钮 ​

tsx
import { ChatInput } from '@airo-ui/react'

function Demo() {
  return (
    <ChatInput
      sendLabel="提交"
      placeholder="输入内容"
      onSend={(val) => console.log('已提交:', val)}
    />
  )
}

最大行数 ​

tsx
import { ChatInput } from '@airo-ui/react'

function Demo() {
  return (
    <ChatInput
      maxRows={5}
      placeholder="最多显示5行"
      onSend={(val) => console.log('发送:', val)}
    />
  )
}

思维链模式 ​

tsx
import { useState } from 'react'
import { ChatInput } from '@airo-ui/react'

function Demo() {
  const [thinking, setThinking] = useState(false)

  return (
    <ChatInput
      showThinking
      thinking={thinking}
      thinkingLabel="深度思考"
      onThinkingChange={setThinking}
      placeholder="输入问题"
      onSend={(val) => console.log('发送:', val, '思考模式:', thinking)}
    />
  )
}

模型选择 ​

tsx
import { useState } from 'react'
import { ChatInput } from '@airo-ui/react'

const models = [
  { label: 'GPT-4', value: 'gpt-4' },
  { label: 'GPT-3.5', value: 'gpt-3.5' },
]

function Demo() {
  const [model, setModel] = useState('gpt-4')

  return (
    <ChatInput
      showModelSelect
      model={model}
      models={models}
      modelPlaceholder="选择模型"
      onModelChange={setModel}
      placeholder="输入问题"
      onSend={(val) => console.log('发送:', val, '模型:', model)}
    />
  )
}

自定义操作区 ​

tsx
import { ChatInput } from '@airo-ui/react'

function Demo() {
  return (
    <ChatInput
      actionsLeft={<button>附件</button>}
      actionsRight={<button>设置</button>}
      placeholder="输入内容"
      onSend={(val) => console.log('发送:', val)}
    />
  )
}

API ​

Props ​

名称类型默认值说明
valuestring—受控模式当前值
onChange(value: string) => void—值变化时触发
placeholderstring—占位文本
disabledbooleanfalse是否禁用
maxRowsnumber—最大显示行数
sendLabelstring'发送'发送按钮文本
onSend(value: string) => void—点击发送时触发
onFocus(e: FocusEvent) => void—获得焦点时触发
onBlur(e: FocusEvent) => void—失去焦点时触发
showThinkingbooleanfalse是否显示思维链开关
thinkingbooleanfalse思维链开关状态
thinkingLabelstring—思维链开关标签
onThinkingChange(value: boolean) => void—思维链状态变化时触发
showModelSelectbooleanfalse是否显示模型选择
modelstring—当前选中的模型
modelsChatInputModelOption[]—模型选项列表
modelPlaceholderstring—模型选择占位文本
onModelChange(value: string) => void—模型切换时触发
actionsLeftReactNode—左侧自定义操作区
actionsRightReactNode—右侧自定义操作区

MIT License