import React, { useState, useRef, useEffect } from "react"; import { Modal, Form, Input, Cascader, message } from "antd"; import styles from "./update.module.less"; import { resourceCategory } from "../../../../api/index"; interface PropInterface { id: number; open: boolean; onCancel: () => void; } interface Option { value: string | number; label: string; children?: Option[]; } export const ResourceCategoryUpdate: React.FC = ({ id, open, onCancel, }) => { const [form] = Form.useForm(); const [loading, setLoading] = useState(true); const [categories, setCategories] = useState([]); const [parent_id, setParentId] = useState(0); useEffect(() => { getParams(); }, []); useEffect(() => { if (id === 0) { return; } getDetail(); }, [id]); const getParams = () => { resourceCategory.createResourceCategory().then((res: any) => { const categories = res.data.categories; if (JSON.stringify(categories) !== "{}") { const new_arr: Option[] = checkArr(categories, 0); new_arr.unshift({ label: "无", value: 0, }); setCategories(new_arr); } }); }; const getDetail = () => { resourceCategory.resourceCategory(id).then((res: any) => { let data = res.data; let arr = data.parent_chain.split(","); let new_arr: any[] = []; arr.map((num: any) => { new_arr.push(Number(num)); }); form.setFieldsValue({ name: data.name, parent_id: new_arr, }); setParentId(data.parent_id); }); }; const checkArr = (categories: any[], id: number) => { const arr = []; for (let i = 0; i < categories[id].length; i++) { if (!categories[categories[id][i].id]) { arr.push({ label: categories[id][i].name, value: categories[id][i].id, }); } else { const new_arr: Option[] = checkArr(categories, categories[id][i].id); arr.push({ label: categories[id][i].name, value: categories[id][i].id, children: new_arr, }); } } return arr; }; const onFinish = (values: any) => { resourceCategory .updateResourceCategory(id, values.name, parent_id || 0, 0) .then((res: any) => { message.success("保存成功!"); onCancel(); }); }; const onFinishFailed = (errorInfo: any) => { console.log("Failed:", errorInfo); }; const handleChange = (value: any) => { if (value !== undefined) { let it = value[value.length - 1]; if (it === id) { setParentId(0); } else { setParentId(it); } } else { setParentId(0); } }; const displayRender = (label: any, selectedOptions: any) => { if (selectedOptions && selectedOptions[0]) { let current = selectedOptions[selectedOptions.length - 1].value; if (current === id) { message.error("不能选择自己作为父类"); return "无"; } } return label[label.length - 1]; }; return ( <> form.submit()} onCancel={() => onCancel()} >
); };