Skip to content

ChatInput Component

The message input with file attachments and submit button.

Import

tsx
import { ChatInput } from "@cognipeer/chat-ui";

Usage

tsx
<ChatInput
  onSubmit={(message, files) => {
    console.log("Send:", message, files);
  }}
/>

Props

PropTypeDefaultDescription
onSubmit(message: string, files: File[]) => void-Submit callback
isLoadingbooleanfalseDisable input while loading
onStop() => void-Stop streaming callback
enableFileUploadbooleantrueEnable file uploads
allowedFileTypesstring[]["*"]Allowed MIME types
maxFileSizenumber10MBMax file size in bytes
maxFilesnumber10Max files per submission
placeholderstring"Send a message..."Input placeholder
disabledbooleanfalseDisable input
autoFocusbooleantrueAuto-focus on mount
classNamestring-Container class name

Examples

Basic

tsx
<ChatInput
  onSubmit={(message) => {
    sendMessage(message);
  }}
/>

With Loading State

tsx
<ChatInput
  onSubmit={handleSubmit}
  isLoading={isLoading}
  onStop={() => abortController.abort()}
/>

Controlled Input

tsx
function ControlledInput() {
  const [value, setValue] = useState("");

  return (
    <ChatInput
      value={value}
      onChange={setValue}
      onSubmit={(message) => {
        sendMessage(message);
        setValue("");
      }}
    />
  );
}

File Uploads Disabled

tsx
<ChatInput
  onSubmit={handleSubmit}
  enableFileUpload={false}
/>

Images Only

tsx
<ChatInput
  onSubmit={handleSubmit}
  enableFileUpload={true}
  allowedFileTypes={["image/*"]}
  maxFiles={1}
/>

Custom Placeholder

tsx
<ChatInput
  onSubmit={handleSubmit}
  placeholder="Ask a question about your document..."
/>

With Custom Submit Button

tsx
<ChatInput
  onSubmit={handleSubmit}
  renderSubmitButton={({ isLoading, disabled, onClick }) => (
    <button
      onClick={onClick}
      disabled={disabled}
      className="custom-submit"
    >
      {isLoading ? "Sending..." : "Send"}
    </button>
  )}
/>

Keyboard Shortcuts

  • Enter - Send message
  • Shift+Enter - New line
  • Ctrl+V / Cmd+V - Paste (including images)
  • Escape - Cancel file selection

File Handling

The input supports:

  • File picker button
  • Drag and drop
  • Paste from clipboard
  • Multiple file selection
tsx
<ChatInput
  onSubmit={(message, files) => {
    console.log("Message:", message);
    console.log("Files:", files.map(f => f.name));
  }}
  allowedFileTypes={["image/*", ".pdf", ".txt"]}
  maxFileSize={5 * 1024 * 1024} // 5MB
  maxFiles={3}
/>

Styling

css
.chat-input {
  padding: 16px;
  border-top: 1px solid var(--chat-border-primary);
}

.chat-input textarea {
  width: 100%;
  resize: none;
  background: var(--chat-bg-tertiary);
  border: 1px solid var(--chat-border-primary);
  border-radius: 12px;
  padding: 12px 16px;
  color: var(--chat-text-primary);
}

.chat-input textarea:focus {
  outline: none;
  border-color: var(--chat-accent-primary);
}

.chat-input-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
}

Accessibility

The input includes:

  • Proper ARIA labels
  • Keyboard navigation
  • Focus management
  • Screen reader support

Released under the MIT License.