添加背单词功能
This commit is contained in:
17
src/hooks/useFileUpload.ts
Normal file
17
src/hooks/useFileUpload.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useRef } from "react";
|
||||
|
||||
export default function useFileUpload(callback: (file: File) => void) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const upload = (type: string = "*") => {
|
||||
const input = inputRef.current;
|
||||
if (input) {
|
||||
input.click();
|
||||
input.setAttribute("accept", type);
|
||||
input.onchange = () => {
|
||||
const file = input.files?.[0];
|
||||
if (file) callback(file);
|
||||
};
|
||||
}
|
||||
};
|
||||
return { upload, inputRef };
|
||||
}
|
||||
Reference in New Issue
Block a user