Skip to content

Commit 5468879

Browse files
authored
feat(sender): add toolbar feature (#40)
Support customize toolbar of Sender component.
1 parent f66e5b8 commit 5468879

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

playground/src/Chat.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { MessageSquarePlus } from "lucide-react";
22
import { useState } from "react";
33
import { BubbleList } from "../../dist/bubble";
44
import { Button } from "../../dist/button";
5+
import { FileUpload } from "../../dist/file-upload";
56
import {
67
Prompt,
78
PromptDescription,
89
Prompts,
910
PromptTitle,
1011
} from "../../dist/prompt";
11-
import { Sender } from "../../dist/sender";
12+
import { InputCount, Sender } from "../../dist/sender";
1213
import type { MessageParam } from "../../dist/utils";
1314
import { useChat } from "../../dist/utils/chat";
1415
import { useMateChat } from "../../dist/utils/core";
@@ -93,6 +94,12 @@ export function Chat() {
9394
initialMessage={prompt}
9495
input={input}
9596
onMessageChange={setPrompt}
97+
toolbar={
98+
<div className="flex flex-row justify-between w-full">
99+
<InputCount count={prompt.length} limit={500} />
100+
<FileUpload />
101+
</div>
102+
}
96103
/>
97104
</main>
98105
</div>

src/file-upload.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { twMerge } from "tailwind-merge";
55
import Appendix from "./icons/appendix.svg";
66

77
export interface FileUploadProps extends React.ComponentProps<"button"> {
8-
onFilesSelect: (files: File[]) => void;
8+
onFilesSelect?: (files: File[]) => void;
99
}
1010

1111
export function FileUpload({
@@ -17,7 +17,7 @@ export function FileUpload({
1717
const handleChange = useCallback(
1818
(e: React.ChangeEvent<HTMLInputElement>) => {
1919
const files = Array.from(e.target.files ?? []);
20-
onFilesSelect(files);
20+
onFilesSelect?.(files);
2121
if (fileInputRef.current) fileInputRef.current.value = "";
2222
},
2323
[onFilesSelect],

src/sender.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ import PublishNew from "./icons/publish-new.svg";
77
import QuickStop from "./icons/quick-stop.svg";
88
import type { Backend } from "./utils";
99

10+
export interface InputCountProps extends React.ComponentProps<"span"> {
11+
count: number;
12+
limit: number;
13+
className: string;
14+
}
15+
16+
export function InputCount({
17+
count,
18+
limit,
19+
className,
20+
...props
21+
}: InputCountProps) {
22+
return (
23+
<span className={className} {...props}>
24+
{count} / {limit}
25+
</span>
26+
);
27+
}
28+
1029
export interface SenderButtonProps extends React.ComponentProps<"button"> {
1130
/**
1231
* Icon to display in the button.
@@ -86,6 +105,7 @@ export interface SenderProps extends React.ComponentProps<"div"> {
86105
* @param controller - The AbortController to abort the request.
87106
*/
88107
onSend?: (controller: AbortController) => void;
108+
toolbar?: React.ReactNode;
89109
}
90110
export function Sender({
91111
className,
@@ -94,6 +114,7 @@ export function Sender({
94114
onMessageChange,
95115
input,
96116
onSend,
117+
toolbar,
97118
...props
98119
}: SenderProps) {
99120
const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -164,11 +185,13 @@ export function Sender({
164185
className="w-full pt-4 px-4 border-0 rounded-2xl resize-none focus:ring-0 focus:outline-none text-gray-700 placeholder-gray-400"
165186
rows={2}
166187
/>
167-
<div className="flex items-center justify-between w-full px-4 py-2">
168-
<div className="flex items-center gap-2">
169-
<span className="text-sm text-gray-500">{message.length} / 500</span>
170-
</div>
171-
<SenderButton onClick={handleSend} isSending={isSending} />
188+
<div className="flex items-center w-full px-4 py-2 gap-4">
189+
{toolbar}
190+
<SenderButton
191+
onClick={handleSend}
192+
isSending={isSending}
193+
className="ml-auto"
194+
/>
172195
</div>
173196
</div>
174197
);

0 commit comments

Comments
 (0)