Skip to content
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

fix: Don't allow empty chat messages #1137

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions apps/www/registry/default/example/cards/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function CardsChat() {
content: "I can't log in.",
},
])
const [input, setInput] = React.useState("")
const inputLength = input.trim().length

return (
<>
Expand All @@ -115,6 +117,7 @@ export function CardsChat() {
onClick={() => setOpen(true)}
>
<Plus className="h-4 w-4" />
<span className="sr-only">New message</span>
</Button>
</TooltipTrigger>
<TooltipContent sideOffset={10}>New message</TooltipContent>
Expand Down Expand Up @@ -142,24 +145,27 @@ export function CardsChat() {
<form
onSubmit={(event) => {
event.preventDefault()
if (inputLength === 0) return
setMessages([
...messages,
{
role: "user",
content: event.currentTarget.message.value,
content: input,
},
])

event.currentTarget.message.value = ""
setInput("")
}}
className="flex w-full items-center space-x-2"
>
<Input
id="message"
placeholder="Type your message..."
className="flex-1"
autoComplete="off"
value={input}
onChange={(event) => setInput(event.target.value)}
/>
<Button type="submit" size="icon">
<Button type="submit" size="icon" disabled={inputLength === 0}>
<Send className="h-4 w-4" />
<span className="sr-only">Send</span>
</Button>
Expand Down
13 changes: 9 additions & 4 deletions apps/www/registry/new-york/example/cards/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function CardsChat() {
content: "I can't log in.",
},
])
const [input, setInput] = React.useState("")
const inputLength = input.trim().length

return (
<>
Expand Down Expand Up @@ -143,24 +145,27 @@ export function CardsChat() {
<form
onSubmit={(event) => {
event.preventDefault()
if (inputLength === 0) return
setMessages([
...messages,
{
role: "user",
content: event.currentTarget.message.value,
content: input,
},
])

event.currentTarget.message.value = ""
setInput("")
}}
className="flex w-full items-center space-x-2"
>
<Input
id="message"
placeholder="Type your message..."
className="flex-1"
autoComplete="off"
value={input}
onChange={(event) => setInput(event.target.value)}
/>
<Button type="submit" size="icon">
<Button type="submit" size="icon" disabled={inputLength === 0}>
<PaperPlaneIcon className="h-4 w-4" />
<span className="sr-only">Send</span>
</Button>
Expand Down