Skip to content

PromptInput 提示词输入框 ​

AI 应用专用的输入框组件,支持自适应高度、Enter 发送、流式状态指示、无障碍访问及自定义操作栏。

基础用法 ​

Enter 发送消息,Shift+Enter 换行。

<script setup>
import { ref } from 'vue'
import PromptInput from '@/vue/components/PromptInput.vue'

const value = ref('')

function handleSubmit(text) {
  alert('Submitted: ' + text)
  value.value = ''
}
</script>
<template>
  <PromptInput v-model="value" @submit="handleSubmit" />
</template>

流式状态 ​

设置 streaming 属性禁用输入并显示流式状态,配合 stoppable 可显示中断按钮。

<script setup>
import { ref } from 'vue'
import PromptInput from '@/vue/components/PromptInput.vue'

const value = ref('')
</script>
<template>
  <PromptInput v-model="value" streaming />
</template>

字数限制 ​

设置 maxLength 和 showCount 显示字数统计。

<script setup>
import { ref } from 'vue'
import PromptInput from '@/vue/components/PromptInput.vue'

const value = ref('')
</script>
<template>
  <PromptInput v-model="value" :max-length="200" show-count />
</template>

双框架使用示例 ​

vue
<template>
  <PromptInput
    v-model="value"
    placeholder="输入提示词..."
    :streaming="isStreaming"
    :stoppable="true"
    hint-text="内容由 AI 生成,请注意甄别"
    @submit="handleSubmit"
    @stop="handleStop"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { PromptInput } from '@bobocn/element/vue'
import '@bobocn/element/style.css'

const value = ref('')
const isStreaming = ref(false)

const handleSubmit = (text: string) => {
  console.log('提交:', text)
  isStreaming.value = true
}

const handleStop = () => {
  isStreaming.value = false
}
</script>
tsx
import React, { useState } from 'react'
import { PromptInput } from '@bobocn/element/react'
import '@bobocn/element/style.css'

export default function App() {
  const [value, setValue] = useState('')
  const [isStreaming, setIsStreaming] = useState(false)

  const handleSubmit = (text: string) => {
    console.log('Submitted:', text)
    setValue('')
    setIsStreaming(true)
  }

  const handleStop = () => {
    setIsStreaming(false)
  }

  return (
    <PromptInput
      value={value}
      onChange={setValue}
      placeholder="输入提示词..."
      streaming={isStreaming}
      stoppable
      hintText="内容由 AI 生成,请注意甄别"
      onSubmit={handleSubmit}
      onStop={handleStop}
    />
  )
}

API ​

属性 (Attributes / Props) ​

属性名说明类型默认值
modelValue / value输入值(Vue v-model / React value)string''
placeholder占位符文本string'输入消息...'
disabled是否禁用booleanfalse
maxLength最大字数number—
rows初始行数number1
autoResize是否开启高度自适应booleantrue
maxRows最大自适应行数number6
showCount是否显示字数统计booleanfalse
streaming是否处于流式响应中booleanfalse
stoppablestreaming 时是否显示停止生成按钮(旧 stopable 已弃用但兼容)booleanfalse
stopText停止按钮文案string'停止'
streamingText停止生成悬浮文案string'停止生成'
hintText底部辅助说明 / 免责声明提示文案string—
size输入框尺寸'small' | 'default' | 'large''default'
variant输入框风格变体'default' | 'filled''default'
clearable是否显示清除按钮booleanfalse
id原生 textarea id 标识string—
ariaLabeltextarea 无障碍标签string—
ariaLabelledbytextarea 无障碍关联标签 IDstring—
stopAriaLabel停止按钮无障碍标签string'停止生成'
sendAriaLabel发送按钮无障碍标签string'发送消息'
clearAriaLabel清空按钮无障碍标签string'清空输入内容'

事件 (Events) ​

事件名说明回调参数
update:modelValue / onChange输入内容变更时触发(value: string)
submit / onSubmit按 Enter 键提交时触发(支持外部 e.preventDefault() 拦截)(value: string)
stop / onStop点击停止生成按钮时触发—
keydown键盘按键原生事件(event: KeyboardEvent)

方法 (Exposes / Ref) ​

方法名说明类型
focus聚焦输入框() => void
blur失焦输入框() => void
clear清空内容() => void
textareaReftextarea 原生 DOM 引用HTMLTextAreaElement

插槽 / Children ​

名称说明
actions / children自定义底部右侧操作栏按钮

基于 MIT 许可发布