import { Component, Prop, Event, EventEmitter, State, h } from '@stencil/core'; @Component({ tag: 'internal-message-input', styleUrl: 'message-input.css', shadow: true }) export class InternalMessageInput { @Prop() placeholder: string = '输入消息...'; @Prop() disabled: boolean = false; @Prop() maxLength: number = 1000; @Prop() multiline: boolean = false; @Prop() showCharCount: boolean = true; @Prop() sendButtonText: string = '发送'; @State() private inputValue: string = ''; @State() private isComposing: boolean = false; @Event() messageSend: EventEmitter<{ content: string; timestamp: number }>; @Event() inputChange: EventEmitter; @Event() inputFocus: EventEmitter; @Event() inputBlur: EventEmitter; private handleInput = (event: Event) => { const target = event.target as HTMLInputElement | HTMLTextAreaElement; this.inputValue = target.value; this.inputChange.emit(this.inputValue); }; private handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter') { if (this.multiline) { if (event.shiftKey) { // Shift+Enter 换行 return; } else { // Enter 发送 event.preventDefault(); this.handleSend(); } } else { // 单行模式 Enter 发送 event.preventDefault(); this.handleSend(); } } }; private handleCompositionStart = () => { this.isComposing = true; }; private handleCompositionEnd = () => { this.isComposing = false; this.handleInput({ target: { value: this.inputValue } } as any); }; private handleSend = () => { const content = this.inputValue.trim(); if (content && !this.disabled && !this.isComposing) { const timestamp = Date.now(); this.messageSend.emit({ content, timestamp }); this.inputValue = ''; } }; private handleFocus = () => { this.inputFocus.emit(); }; private handleBlur = () => { this.inputBlur.emit(); }; render() { const isSendDisabled = this.disabled || !this.inputValue.trim(); return (
{this.multiline ? (