Skip to content

Commit b5e4cb8

Browse files
committed
feat(sender): optimize SenderButton component
1 parent 3a19451 commit b5e4cb8

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

.changes/optimize-sender-button.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@matechat/react": patch
3+
---
4+
5+
Optimize the `SenderButton` component in `sender.tsx`.
6+
7+
This optimization includes:
8+
9+
- Add `icon` and `isSending` option to `SenderButton` component.
10+
- Add doc-string for `SenderButton` and `Sender` props.

src/sender.tsx

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,31 @@ 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 SenderButtonProps extends React.ComponentProps<"button"> {
11+
/**
12+
* Icon to display in the button.
13+
*
14+
* Defaults to a send icon when `isSending` is false,
15+
* and a stop icon when `isSending` is true. The icon
16+
* will be overridden if provided.
17+
*/
18+
icon?: React.ReactNode;
19+
/**
20+
* Whether runtime is currently sending a message.
21+
*
22+
* If true, the button will display a stop icon
23+
* instead of the send icon.
24+
*
25+
* @default false
26+
*/
27+
isSending?: boolean;
28+
}
1029
export function SenderButton({
1130
className,
31+
icon,
32+
isSending = false,
1233
...props
13-
}: React.ComponentProps<"button">) {
34+
}: SenderButtonProps) {
1435
return (
1536
<button
1637
type="button"
@@ -22,15 +43,48 @@ export function SenderButton({
2243
),
2344
)}
2445
{...props}
25-
/>
46+
>
47+
{icon ?? (
48+
<img
49+
className="filter brightness-0 invert"
50+
src={isSending ? QuickStop : PublishNew}
51+
alt={isSending ? "icon-quick-stop" : "icon-publish-new"}
52+
/>
53+
)}
54+
</button>
2655
);
2756
}
2857

58+
/**
59+
* Props for the message sender component.
60+
* @extends React.ComponentProps<"div">
61+
*/
2962
export interface SenderProps extends React.ComponentProps<"div"> {
63+
/**
64+
* Initial message to display in the input field.
65+
* @default ""
66+
*/
3067
initialMessage?: string;
68+
/**
69+
* Placeholder text for the input field.
70+
* @default "Type your message here..."
71+
*/
3172
placeholder?: string;
73+
/**
74+
* Function to handle input changes.
75+
*/
3276
input: Backend["input"];
77+
/**
78+
* Function to handle message changes.
79+
* @param message - The new message.
80+
*/
3381
onMessageChange?: (message: string) => void;
82+
/**
83+
* Function to handle the send action.
84+
* This function is called when the send button is clicked.
85+
* It receives an AbortController that can be used to abort the request.
86+
* @param controller - The AbortController to abort the request.
87+
*/
3488
onSend?: (controller: AbortController) => void;
3589
}
3690
export function Sender({
@@ -111,21 +165,7 @@ export function Sender({
111165
<div className="flex items-center gap-2">
112166
<span className="text-sm text-gray-500">{message.length} / 500</span>
113167
</div>
114-
<SenderButton onClick={handleSend}>
115-
{isSending ? (
116-
<img
117-
className="filter brightness-0 invert"
118-
src={QuickStop}
119-
alt="icon-quick-stop"
120-
/>
121-
) : (
122-
<img
123-
className="filter brightness-0 invert"
124-
src={PublishNew}
125-
alt="icon-publish-new"
126-
/>
127-
)}
128-
</SenderButton>
168+
<SenderButton onClick={handleSend} isSending={isSending} />
129169
</div>
130170
</div>
131171
);

0 commit comments

Comments
 (0)