import React, { useState, useEffect } from "react"; import { Row, Col, Form, Input, Cascader, Button, message } from "antd"; import styles from "./create.module.less"; import { department } from "../../api/index"; import { useNavigate } from "react-router-dom"; import { BackBartment } from "../../compenents"; interface Option { value: string | number; label: string; children?: Option[]; } export const DepartmentCreatePage: React.FC = () => { const navigate = useNavigate(); const [form] = Form.useForm(); const [loading, setLoading] = useState(true); const [categories, setCategories] = useState([]); const [parent_id, setParentId] = useState(0); useEffect(() => { getParams(); }, []); const getParams = () => { department.createDepartment().then((res: any) => { const departments = res.data.departments; const new_arr: Option[] = checkArr(departments, 0); setCategories(new_arr); }); }; const checkArr = (departments: any[], id: number) => { const arr = []; for (let i = 0; i < departments[id].length; i++) { if (!departments[departments[id][i].id]) { arr.push({ label: departments[id][i].name, value: departments[id][i].id, }); } else { const new_arr: Option[] = checkArr(departments, departments[id][i].id); arr.push({ label: departments[id][i].name, value: departments[id][i].id, children: new_arr, }); } } return arr; }; const onFinish = (values: any) => { department .storeDepartment(values.name, parent_id || 0, values.sort) .then((res: any) => { message.success("保存成功!"); navigate(-1); }); }; const onFinishFailed = (errorInfo: any) => { console.log("Failed:", errorInfo); }; const handleChange = (value: any) => { if (value !== undefined) { let it = value[value.length - 1]; setParentId(it); } else { setParentId(0); } }; const displayRender = (label: any, selectedOptions: any) => { return label[label.length - 1]; }; return ( <>
); };