pull/661/head
vapao 2024-02-27 23:00:34 +08:00
parent 756599e2e2
commit 1d22adf6f5
2 changed files with 50 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import { useRef, useState, useEffect } from 'react' import { useRef, useState, useEffect } from 'react'
import { Card, Tree, Dropdown, Input } from 'antd' import { Card, Tree, Dropdown, Input, Spin } from 'antd'
import { FaServer } from 'react-icons/fa6' import { FaServer } from 'react-icons/fa6'
import { IoMdMore } from 'react-icons/io' import { IoMdMore } from 'react-icons/io'
import { AiOutlineFolder, AiOutlineFolderAdd, AiOutlineEdit, AiOutlineFileAdd, AiOutlineScissor, AiOutlineClose, AiOutlineDelete } from 'react-icons/ai' import { AiOutlineFolder, AiOutlineFolderAdd, AiOutlineEdit, AiOutlineFileAdd, AiOutlineScissor, AiOutlineClose, AiOutlineDelete } from 'react-icons/ai'
@ -8,11 +8,13 @@ import { http, findNodeByKey } from '@/libs'
import css from './index.module.scss' import css from './index.module.scss'
let clickNode = null let clickNode = null
let rawTreeData = []
function Group() { function Group() {
const inputRef = useRef(null) const inputRef = useRef(null)
const [expandedKeys, setExpandedKeys] = useState([]) const [expandedKeys, setExpandedKeys] = useState([])
const [treeData, updateTreeData] = useImmer([]) const [treeData, updateTreeData] = useImmer([])
const [loading, setLoading] = useState(false)
const menuItems = [ const menuItems = [
{ label: '新建根分组', key: 'newRoot', icon: <AiOutlineFolder size={18} /> }, { label: '新建根分组', key: 'newRoot', icon: <AiOutlineFolder size={18} /> },
@ -32,10 +34,13 @@ function Group() {
}, []) }, [])
function fetchData() { function fetchData() {
setLoading(true)
http.get('/api/host/group/') http.get('/api/host/group/')
.then(res => { .then(res => {
rawTreeData = res.treeData
updateTreeData(res.treeData) updateTreeData(res.treeData)
}) })
.finally(() => setLoading(false))
} }
function handleNodeClick(e, node) { function handleNodeClick(e, node) {
@ -45,7 +50,6 @@ function Group() {
function handleMenuClick({ key, domEvent }) { function handleMenuClick({ key, domEvent }) {
domEvent.stopPropagation() domEvent.stopPropagation()
console.log(key, clickNode.key)
switch (key) { switch (key) {
case 'newRoot': case 'newRoot':
updateTreeData(draft => { updateTreeData(draft => {
@ -81,7 +85,9 @@ function Group() {
console.log('删除主机') console.log('删除主机')
break break
case 'deleteGroup': case 'deleteGroup':
console.log('删除此分组') setLoading(true)
http.delete('/api/host/group/', { id: clickNode.key })
.then(() => fetchData(), () => setLoading(false))
break break
default: default:
break break
@ -93,13 +99,31 @@ function Group() {
} }
} }
function handleInputSubmit(e) { function handleInputSubmit(e, node) {
console.log('提交: ', e.target.value) const value = e.target.value.trim()
if (value) {
let form = { name: value }
if (node.action === 'newChild') {
form.parent_id = clickNode.key
} else if (node.action === 'rename') {
form.id = node.key
}
setLoading(true)
http.post('/api/host/group/', form)
.then(() => fetchData(), () => setLoading(false))
} else {
updateTreeData(rawTreeData)
}
} }
function titleRender(node) { function titleRender(node) {
return ['newRoot', 'newChild', 'rename'].includes(node.action) ? ( return ['newRoot', 'newChild', 'rename'].includes(node.action) ? (
<Input ref={inputRef} defaultValue={node.title} onPressEnter={handleInputSubmit} onBlur={handleInputSubmit} /> <Input
ref={inputRef}
defaultValue={node.title}
onPressEnter={e => handleInputSubmit(e, node)}
onBlur={e => handleInputSubmit(e, node)}
placeholder="请输入" />
) : ( ) : (
<div className={css.treeTitle}> <div className={css.treeTitle}>
<FaServer /> <FaServer />
@ -117,15 +141,18 @@ function Group() {
return ( return (
<Card title="分组列表" className={css.group}> <Card title="分组列表" className={css.group}>
<Tree.DirectoryTree <Spin spinning={loading}>
defaultExpandParent <Tree.DirectoryTree
showIcon={false} className={css.tree}
treeData={treeData} defaultExpandParent
expandedKeys={expandedKeys} showIcon={false}
expandAction="doubleClick" treeData={treeData}
titleRender={titleRender} expandedKeys={expandedKeys}
onExpand={keys => setExpandedKeys(keys)} expandAction="doubleClick"
/> titleRender={titleRender}
onExpand={keys => setExpandedKeys(keys)}
/>
</Spin>
</Card> </Card>
) )
} }

View File

@ -6,6 +6,14 @@
border-top-right-radius: 0; border-top-right-radius: 0;
border-bottom-right-radius: 0; border-bottom-right-radius: 0;
:global(.ant-card-head) {
height: 58px;
}
.tree {
min-height: 200px;
}
.treeTitle { .treeTitle {
display: flex; display: flex;
flex-direction: row; flex-direction: row;