import { type ChangeEvent, useRef } from "react"; import { useTranslation } from "react-i18next"; import { UploadOutlined as UploadOutlinedIcon } from "@ant-design/icons"; import { Button, type ButtonProps, Input, Space, type UploadProps } from "antd"; import { type TextAreaProps } from "antd/es/input/TextArea"; import { mergeCls } from "@/utils/css"; import { readFileContent } from "@/utils/file"; export interface TextFileInputProps extends Omit { accept?: UploadProps["accept"]; uploadButtonProps?: Omit; uploadText?: string; onChange?: (value: string) => void; } const TextFileInput = ({ className, style, accept, disabled, readOnly, uploadText, uploadButtonProps, onChange, ...props }: TextFileInputProps) => { const { t } = useTranslation(); const fileInputRef = useRef(null); const handleButtonClick = () => { if (fileInputRef.current) { fileInputRef.current.click(); } }; const handleFileChange = async (e: ChangeEvent) => { const { files } = e.target as HTMLInputElement; if (files?.length) { const value = await readFileContent(files[0]); onChange?.(value); } }; return ( onChange?.(e.target.value)} /> {!readOnly && ( <> )} ); }; export default TextFileInput;