import { type ChangeEvent, useContext, useRef } from "react"; import { useTranslation } from "react-i18next"; import { IconFileImport } from "@tabler/icons-react"; import { Button, type ButtonProps, Input, type UploadProps } from "antd"; import DisabledContext from "antd/es/config-provider/DisabledContext"; import { type TextAreaProps } from "antd/es/input/TextArea"; import { readFileAsText } 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 injectedDisabled = useContext(DisabledContext); const mergedDisabled = disabled ?? injectedDisabled; 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 readFileAsText(files[0]); onChange?.(value); } }; return (
onChange?.(e.target.value)} /> {!readOnly && ( <> )}
); }; export default TextFileInput;