-
Notifications
You must be signed in to change notification settings - Fork 53
Form Element Pass-Through Props #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pmn4
wants to merge
1
commit into
run-llama:main
Choose a base branch
from
pmn4:pmn4.form-props
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@llamaindex/chat-ui': patch | ||
| --- | ||
|
|
||
| Add Pass-through props to ChatInput components for form elements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { Send, Square } from 'lucide-react' | ||
| import { createContext, useContext, useRef, useState } from 'react' | ||
| import { cn } from '../lib/utils' | ||
| import { Button } from '../ui/button' | ||
| import { Button, ButtonProps } from '../ui/button' | ||
| import { Textarea } from '../ui/textarea' | ||
| import { FileUploader } from '../widgets/index.js' // this import needs the file extension as it's importing the widget bundle | ||
| import { useChatUI } from './chat.context' | ||
|
|
@@ -15,11 +15,16 @@ interface ChatInputProps extends React.PropsWithChildren { | |
| attachments?: MessagePart[] | ||
| } | ||
|
|
||
| interface ChatInputFormProps extends React.PropsWithChildren { | ||
| type FormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onSubmit'> | ||
| interface ChatInputFormProps extends FormProps, React.PropsWithChildren { | ||
| className?: string | ||
| } | ||
|
|
||
| interface ChatInputFieldProps { | ||
| type TextAreaProps = Omit< | ||
| React.TextareaHTMLAttributes<HTMLTextAreaElement>, | ||
| 'onChange' | ||
| > | ||
| interface ChatInputFieldProps extends TextAreaProps { | ||
| className?: string | ||
| placeholder?: string | ||
| } | ||
|
|
@@ -31,7 +36,7 @@ interface ChatInputUploadProps { | |
| multiple?: boolean | ||
| } | ||
|
|
||
| interface ChatInputSubmitProps extends React.PropsWithChildren { | ||
| interface ChatInputSubmitProps extends ButtonProps, React.PropsWithChildren { | ||
| className?: string | ||
| disabled?: boolean | ||
| } | ||
|
|
@@ -113,7 +118,8 @@ function ChatInput(props: ChatInputProps) { | |
|
|
||
| function ChatInputForm(props: ChatInputFormProps) { | ||
| const { handleSubmit } = useChatInput() | ||
| const children = props.children ?? ( | ||
| const { children: childrenProp, ...formProps } = props | ||
| const children = childrenProp ?? ( | ||
| <> | ||
| <ChatInputField /> | ||
| <ChatInputSubmit /> | ||
|
|
@@ -123,6 +129,7 @@ function ChatInputForm(props: ChatInputFormProps) { | |
| return ( | ||
| <form | ||
| onSubmit={handleSubmit} | ||
| {...formProps} | ||
| className={cn('relative flex gap-2', props.className)} | ||
| > | ||
| {children} | ||
|
|
@@ -134,6 +141,7 @@ function ChatInputField(props: ChatInputFieldProps) { | |
| const { input, setInput } = useChatUI() | ||
| const { handleKeyDown, setIsComposing } = useChatInput() | ||
| const textareaRef = useRef<HTMLTextAreaElement>(null) | ||
| const { placeholder, className, ...textareaProps } = props | ||
|
|
||
| // auto resize the textarea based on the content | ||
| const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
|
|
@@ -150,12 +158,14 @@ function ChatInputField(props: ChatInputFieldProps) { | |
|
|
||
| return ( | ||
| <Textarea | ||
| ref={textareaRef} | ||
| name="input" | ||
| placeholder={props.placeholder ?? 'Type a message...'} | ||
| spellCheck={false} | ||
| {...textareaProps} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the defaults above the pass-through props, and the things that should not be overridden (or can already be overridden, like |
||
| ref={textareaRef} | ||
| placeholder={placeholder ?? 'Type a message...'} | ||
| className={cn( | ||
| 'bg-secondary h-[100px] max-h-[400px] min-h-0 flex-1 resize-none overflow-y-auto rounded-2xl p-4', | ||
| props.className | ||
| className | ||
| )} | ||
| value={input} | ||
| onChange={handleInputChange} | ||
|
|
@@ -164,7 +174,6 @@ function ChatInputField(props: ChatInputFieldProps) { | |
| onCompositionEnd={() => { | ||
| setTimeout(() => setIsComposing(false), 100) | ||
| }} | ||
| spellCheck={false} | ||
| /> | ||
| ) | ||
| } | ||
|
|
@@ -199,6 +208,7 @@ function ChatInputUpload(props: ChatInputUploadProps) { | |
| function ChatInputSubmit(props: ChatInputSubmitProps) { | ||
| const { stop, isLoading } = useChatUI() | ||
| const { isDisabled } = useChatInput() | ||
| const { disabled, className, children, ...buttonProps } = props | ||
|
|
||
| if (stop && isLoading) { | ||
| return ( | ||
|
|
@@ -217,10 +227,11 @@ function ChatInputSubmit(props: ChatInputSubmitProps) { | |
| <Button | ||
| type="submit" | ||
| size="icon" | ||
| disabled={props.disabled ?? isDisabled} | ||
| className={cn('absolute bottom-2 right-2 rounded-full', props.className)} | ||
| {...buttonProps} | ||
| disabled={disabled ?? isDisabled} | ||
| className={cn('absolute bottom-2 right-2 rounded-full', className)} | ||
| > | ||
| {props.children ?? <Send className="size-4" />} | ||
| {children ?? <Send className="size-4" />} | ||
| </Button> | ||
| ) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
order matters, so I added the pass-through props after
onSubmitso if you really know what you're doing, you can override the default behavior.cn+props.classNameis already a way to override the default, so I placed it after