Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

chat-input.tsx

177 lines4.7 KB

1import { useRef, useState } from 'react'
2import { useChatContext } from './chat'
3import type { ReactNode, RefObject } from 'react'
4​
5export interface ChatInputRenderProps {
6 /** Current input value */
7 value: string
8 /** Set input value */
9 onChange: (value: string) => void
10 /** Submit the message */
11 onSubmit: () => void
12 /** Is the chat currently loading */
13 isLoading: boolean
14 /** Is input disabled */
15 disabled: boolean
16 /** Ref to the input element */
17 inputRef: RefObject<HTMLInputElement | HTMLTextAreaElement | null>
19​
20export interface ChatInputProps {
21 /** Render prop for full control */
22 children?: (props: ChatInputRenderProps) => ReactNode
23 /** CSS class name */
24 className?: string
25 /** Placeholder text */
26 placeholder?: string
27 /** Disable input */
28 disabled?: boolean
29 /** Submit on Enter (Shift+Enter for new line) */
30 submitOnEnter?: boolean
32​
33/**
34 * @deprecated Use `createChatUI()` and an application-owned input component.
35 * Deprecated in 0.9.0. Removed in 1.0.0.
36 *
37 * Chat input component - handles message input and submission
38 *
39 * Features:
40 * - Auto-growing textarea
41 * - Submit on Enter (Shift+Enter for new line)
42 * - Loading state management
43 * - Full render prop support for custom UIs
44 *
45 * @example
46 * ```tsx
47 * <Chat.Input placeholder="Type your message..." />
48 * ```
49 *
50 * @example Custom UI with render prop
51 * ```tsx
52 * <Chat.Input>
53 * {({ value, onChange, onSubmit, isLoading }) => (
54 * <div>
55 * <textarea value={value} onChange={(e) => onChange(e.target.value)} />
56 * <button onClick={onSubmit} disabled={isLoading}>Send</button>
57 * </div>
58 * )}
59 * </Chat.Input>
60 * ```
61 */
62export function ChatInput({
63 children,
64 className,
65 placeholder = 'Type a message...',
66 disabled: disabledProp,
67 submitOnEnter = true,
68}: ChatInputProps) {
69 const { sendMessage, isLoading } = useChatContext()
70 const [value, setValue] = useState('')
71 const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null)
72​
73 const disabled = disabledProp || isLoading
74​
75 const handleSubmit = () => {
76 if (!value.trim() || disabled) return
77 void sendMessage(value)
78 setValue('')
79 }
80​
81 const renderProps: ChatInputRenderProps = {
82 value,
83 onChange: setValue,
84 onSubmit: handleSubmit,
85 isLoading,
86 disabled,
87 inputRef,
88 }
89​
90 // Render prop pattern
91 if (children) {
92 return <>{children(renderProps)}</>
93 }
94​
95 // Default implementation
96 return (
97 <div
98 className={className}
99 data-chat-input
100 style={{
101 display: 'flex',
102 gap: '0.75rem',
103 alignItems: 'center',
104 width: '100%',
105 }}
106 >
107 <input
108 ref={inputRef as React.RefObject<HTMLInputElement>}
109 type="text"
110 value={value}
111 onChange={(e) => setValue(e.target.value)}
112 onKeyDown={(e) => {
113 if (submitOnEnter && e.key === 'Enter') {
114 e.preventDefault()
115 handleSubmit()
116 }
117 }}
118 placeholder={placeholder}
119 disabled={disabled}
120 data-chat-textarea
121 style={{
122 flex: 1,
123 padding: '0.75rem 1rem',
124 fontSize: '0.875rem',
125 border: '1px solid rgba(255, 255, 255, 0.1)',
126 borderRadius: '0.75rem',
127 backgroundColor: 'rgba(31, 41, 55, 0.5)',
128 color: 'white',
129 outline: 'none',
130 transition: 'all 0.2s',
131 }}
132 onFocus={(e) => {
133 e.target.style.borderColor = 'rgba(249, 115, 22, 0.4)'
134 e.target.style.boxShadow = '0 0 0 2px rgba(249, 115, 22, 0.2)'
135 }}
136 onBlur={(e) => {
137 e.target.style.borderColor = 'rgba(255, 255, 255, 0.1)'
138 e.target.style.boxShadow = 'none'
139 }}
140 />
141 <button
142 onClick={handleSubmit}
143 disabled={disabled || !value.trim()}
144 data-chat-submit
145 style={{
146 padding: '0.75rem 1.5rem',
147 fontSize: '0.875rem',
148 fontWeight: 500,
149 color: 'white',
150 backgroundColor:
151 disabled || !value.trim()
152 ? 'rgba(107, 114, 128, 0.5)'
153 : 'rgb(249, 115, 22)',
154 border: 'none',
155 borderRadius: '0.75rem',
156 cursor: disabled || !value.trim() ? 'not-allowed' : 'pointer',
157 transition: 'all 0.2s',
158 whiteSpace: 'nowrap',
159 }}
160 onMouseEnter={(e) => {
161 if (!disabled && value.trim()) {
162 ;(e.target as HTMLButtonElement).style.backgroundColor =
163 'rgb(234, 88, 12)'
164 }
165 }}
166 onMouseLeave={(e) => {
167 if (!disabled && value.trim()) {
168 ;(e.target as HTMLButtonElement).style.backgroundColor =
169 'rgb(249, 115, 22)'
170 }
171 }}
172 >
173 {isLoading ? 'Sending...' : 'Send'}
174 </button>
175 </div>
176 )