Skip to content

Commit 31f4edf

Browse files
authored
feat(file-upload): add file-upload component (#31)
1 parent 32cd9c4 commit 31f4edf

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

.changes/file-upload.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@matechat/react": patch:feat
3+
---
4+
5+
Add `FileUpload` component for file selections.

src/file-upload.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import clsx from "clsx";
2+
import type React from "react";
3+
import { useRef } from "react";
4+
import { twMerge } from "tailwind-merge";
5+
import Appendix from "./icons/appendix.svg";
6+
7+
export interface FileUploadProps extends React.ComponentProps<"button"> {
8+
onFilesSelect: (files: File[]) => void;
9+
}
10+
11+
export function FileUpload({
12+
className,
13+
onFilesSelect,
14+
...props
15+
}: FileUploadProps) {
16+
const fileInputRef = useRef<HTMLInputElement>(null);
17+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
18+
const files = Array.from(e.target.files ?? []);
19+
onFilesSelect(files);
20+
if (fileInputRef.current) fileInputRef.current.value = "";
21+
};
22+
23+
return (
24+
<button
25+
data-slot="file-upload"
26+
type="button"
27+
className={twMerge(
28+
clsx("cursor-pointer text-gray-500 hover:text-gray-500/80", className),
29+
)}
30+
onClick={() => fileInputRef.current?.click()}
31+
{...props}
32+
>
33+
<img
34+
src={Appendix}
35+
alt="upload"
36+
className="w-5 h-5 dark:filter dark:filter-invert hover:scale-105"
37+
/>
38+
<input
39+
type="file"
40+
className="hidden"
41+
ref={fileInputRef}
42+
onChange={handleChange}
43+
multiple
44+
/>
45+
</button>
46+
);
47+
}

src/icons/appendix.svg

Lines changed: 9 additions & 0 deletions
Loading

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./bubble";
22
export * from "./button";
3+
export * from "./file-upload";
34
export * from "./list";
45
export * from "./prompt";
56
export * from "./sender";

0 commit comments

Comments
 (0)