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' | 'directory'>('blank'); const handleOk = async (values: any) => { setLoading(true); const { path = '', filename: inputFilename, directory = '' } = values; const formData = new FormData(); formData.append('file', file as any); formData.append('filename', inputFilename); formData.append('path', path); formData.append('content', ''); formData.append('directory', directory); request .post(`${config.apiPrefix}scripts`, { data: formData, }) .then(({ code, data }) => { if (code === 200) { message.success(directory ? '新建文件夹成功' : '新建文件成功'); const key = path ? `${path}/` : ''; const filename = file ? file.name : inputFilename; handleCancel({ filename, path, key: `${key}${filename}`, }); } 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' && ( value.includes('/') ? Promise.reject(new Error('文件名不能包含斜杠')) : Promise.resolve(), }, ]} > )} {type === 'directory' && ( )} {type === 'upload' && (

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

)}
); }; export default EditScriptNameModal;