import React, { useEffect, useState } from 'react'; import { Modal, message, Input, Form, Select, Upload, Radio, TreeSelect, } from 'antd'; import { request } from '@/utils/http'; import config from '@/utils/config'; import { UploadOutlined } from '@ant-design/icons'; const { Option } = Select; const EditScriptNameModal = ({ handleCancel, treeData, visible, }: { visible: boolean; treeData: any[]; handleCancel: (file?: { filename: string; path: string; key: string; }) => void; }) => { const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [dirs, setDirs] = useState([]); const [file, setFile] = useState(); const [type, setType] = useState<'blank' | 'upload'>('blank'); const handleOk = async (values: any) => { setLoading(true); values.path = values.path || ''; const formData = new FormData(); formData.append('file', file as any); formData.append('filename', values.filename); formData.append('path', values.path); formData.append('content', ''); request .post(`${config.apiPrefix}scripts`, { data: formData, }) .then(({ code, data }) => { if (code === 200) { message.success('保存文件成功'); const key = values.path ? `${values.path}/` : ''; const filename = file ? file.name : values.filename; handleCancel({ filename, path: values.path, key: `${key}${filename}`, }); } else { message.error(data); } setLoading(false); }) .finally(() => setLoading(false)); }; const beforeUpload = (file: File) => { setFile(file); return false; }; const typeChange = (e) => { setType(e.target.value); }; const getDirs = (data) => { for (const item of data) { if (item.children && item.children.length > 0) { item.children = item.children .filter((x) => x.type === 'directory') .map((x) => ({ ...x, disabled: false })); getDirs(item.children); } } return data; }; useEffect(() => { const originDirs = treeData .filter((x) => x.type === 'directory') .map((x) => ({ ...x, disabled: false })); const dirs = getDirs(originDirs); setDirs(dirs); }, [treeData]); useEffect(() => { form.resetFields(); }, [visible]); return ( { form .validateFields() .then((values) => { handleOk(values); }) .catch((info) => { console.log('Validate Failed:', info); }); }} onCancel={() => handleCancel()} confirmLoading={loading} >
空文件 本地上传 {type === 'blank' && ( )} {type === 'upload' && (

点击或者拖拽文件到此区域上传

)}
); }; export default EditScriptNameModal;