import React, { useState, useRef, KeyboardEvent } from 'react'; import { Input, Button, Space, Tooltip, Dropdown, Tag } from 'antd'; import { SendOutlined, PaperClipOutlined, FileTextOutlined, CloseCircleOutlined } from '@ant-design/icons'; import type { MenuProps } from 'antd'; import './MessageInput.css'; const { TextArea } = Input; interface KnowledgeBase { id: string; name: string; } interface MessageInputProps { onSend: (content: string, knowledgeBaseIds: string[]) => void; loading?: boolean; knowledgeBases?: KnowledgeBase[]; placeholder?: string; } const MessageInput: React.FC = ({ onSend, loading = false, knowledgeBases = [], placeholder = '输入你的问题...(Shift+Enter换行,Enter发送)', }) => { const [content, setContent] = useState(''); const [selectedKnowledgeBases, setSelectedKnowledgeBases] = useState([]); const textAreaRef = useRef(null); // 处理发送消息 const handleSend = () => { const trimmedContent = content.trim(); if (!trimmedContent || loading) return; onSend(trimmedContent, selectedKnowledgeBases); setContent(''); setSelectedKnowledgeBases([]); // 重置输入框高度 if (textAreaRef.current) { textAreaRef.current.resizableTextArea.textArea.style.height = 'auto'; } }; // 处理键盘事件 const handleKeyDown = (e: KeyboardEvent) => { // Enter发送,Shift+Enter换行 if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; // 添加知识库 const handleSelectKnowledgeBase = (kbId: string) => { if (!selectedKnowledgeBases.includes(kbId)) { setSelectedKnowledgeBases([...selectedKnowledgeBases, kbId]); } }; // 移除知识库 const handleRemoveKnowledgeBase = (kbId: string) => { setSelectedKnowledgeBases(selectedKnowledgeBases.filter(id => id !== kbId)); }; // 知识库下拉菜单 const knowledgeBaseMenuItems: MenuProps['items'] = knowledgeBases.map(kb => ({ key: kb.id, label: kb.name, icon: , onClick: () => handleSelectKnowledgeBase(kb.id), disabled: selectedKnowledgeBases.includes(kb.id), })); // 获取选中的知识库名称 const getKnowledgeBaseName = (kbId: string) => { const kb = knowledgeBases.find(k => k.id === kbId); return kb ? kb.name : kbId; }; return (
{/* 已选择的知识库标签 */} {selectedKnowledgeBases.length > 0 && (
{selectedKnowledgeBases.map(kbId => ( handleRemoveKnowledgeBase(kbId)} closeIcon={} > {getKnowledgeBaseName(kbId)} ))}
)} {/* 输入框和工具栏 */}