(props: PromptTokenFieldProps)
| 300 | } |
| 301 | |
| 302 | export function PromptTokenField(props: PromptTokenFieldProps) { |
| 303 | let {completionTrigger, renderCompletions, children} = props; |
| 304 | let { |
| 305 | prompt, |
| 306 | setPrompt, |
| 307 | acceptedAttachmentTypes, |
| 308 | setAttachments, |
| 309 | onAddAttachments, |
| 310 | inputRef, |
| 311 | onSubmit |
| 312 | } = useContext(PromptFieldContext); |
| 313 | let [isFocused, setFocused] = useState(false); |
| 314 | |
| 315 | let [filterAnchor, filterValue] = useMemo(() => { |
| 316 | if (completionTrigger) { |
| 317 | let filterAnchor = prompt.findText( |
| 318 | prompt.caretPosition, |
| 319 | Direction.Backward, |
| 320 | completionTrigger |
| 321 | ); |
| 322 | if (filterAnchor != null) { |
| 323 | let filterValue = prompt.slice(filterAnchor, prompt.caretPosition).toString(); |
| 324 | return [filterAnchor, filterValue]; |
| 325 | } |
| 326 | } |
| 327 | return [null, null]; |
| 328 | }, [completionTrigger, prompt]); |
| 329 | |
| 330 | let items = useMemo(() => { |
| 331 | return filterValue != null ? renderCompletions?.(filterValue) : null; |
| 332 | }, [filterValue, renderCompletions]); |
| 333 | |
| 334 | return ( |
| 335 | <Autocomplete> |
| 336 | <TokenField |
| 337 | value={prompt} |
| 338 | onChange={setPrompt} |
| 339 | multiline |
| 340 | aria-label="Prompt" |
| 341 | data-placeholder="Ready to get started? Ask a question, share an idea, or add a task." |
| 342 | ref={inputRef} |
| 343 | onFocus={e => { |
| 344 | if (e.isTrusted) { |
| 345 | setFocused(true); |
| 346 | } |
| 347 | }} |
| 348 | onBlur={e => { |
| 349 | if (e.isTrusted) { |
| 350 | setFocused(false); |
| 351 | } |
| 352 | }} |
| 353 | onPaste={ |
| 354 | acceptedAttachmentTypes |
| 355 | ? e => { |
| 356 | let clipboardData = e.clipboardData as DataTransfer; |
| 357 | let attachments: PromptFieldAttachment[] = []; |
| 358 | for (let item of clipboardData.items) { |
| 359 | if (matchMimeType(item.type, acceptedAttachmentTypes)) { |
nothing calls this directly
no test coverage detected