1
- import { list , save , del } from '@/services/api/menu' ;
1
+ import { list , save , del , tree } from '@/services/api/menu' ;
2
2
import { PlusOutlined } from '@ant-design/icons' ;
3
3
import type { ActionType , ProColumns } from '@ant-design/pro-components' ;
4
- import { ModalForm , PageContainer , ProCard , ProFormDigit , ProFormText } from '@ant-design/pro-components' ;
4
+ import { ModalForm , PageContainer , ProCard , ProFormDigit , ProFormText , ProFormTreeSelect } from '@ant-design/pro-components' ;
5
5
import { Button , Form , message , Popconfirm } from 'antd' ;
6
6
import { MyTable } from 'coding-components' ;
7
7
import React , { useRef , useState , useEffect } from 'react' ;
@@ -13,20 +13,13 @@ const MenuPage: React.FC = () => {
13
13
const [ form ] = Form . useForm ( ) ;
14
14
const [ parentId , setParentId ] = useState < number > ( 1 ) ;
15
15
const [ createModalOpen , handleModalOpen ] = useState < boolean > ( false ) ;
16
+ const [ treeData , setTreeData ] = useState < any [ ] > ( [ ] ) ;
17
+ const treeRef = useRef ( ) ;
16
18
17
19
const handleAdd = async ( fields : any ) => {
18
20
const hide = message . loading ( '正在添加' ) ;
19
21
try {
20
- const parentId = fields . parentId ;
21
- delete fields . parentId ;
22
- const request = {
23
- ...fields ,
24
- parent : {
25
- id : parentId
26
- }
27
- }
28
- console . log ( request ) ;
29
- await save ( request ) ;
22
+ await save ( fields ) ;
30
23
hide ( ) ;
31
24
message . success ( '保存成功' ) ;
32
25
return true ;
@@ -46,6 +39,8 @@ const MenuPage: React.FC = () => {
46
39
message . success ( '删除成功' ) ;
47
40
if ( actionRef . current ) {
48
41
actionRef . current . reload ( ) ;
42
+ //@ts -ignore
43
+ treeRef . current ?. refresh ( ) ;
49
44
}
50
45
return true ;
51
46
} catch ( error ) {
@@ -115,6 +110,16 @@ const MenuPage: React.FC = () => {
115
110
} ,
116
111
] ;
117
112
113
+ const reloadTree = ( ) => {
114
+ tree ( ) . then ( res => {
115
+ setTreeData ( [ res . data ] ) ;
116
+ } )
117
+ }
118
+
119
+ useEffect ( ( ) => {
120
+ reloadTree ( ) ;
121
+ } , [ ] ) ;
122
+
118
123
useEffect ( ( ) => {
119
124
if ( actionRef . current ) {
120
125
actionRef . current . reload ( ) ;
@@ -126,6 +131,7 @@ const MenuPage: React.FC = () => {
126
131
< ProCard gutter = { 8 } >
127
132
< ProCard colSpan = "20%" >
128
133
< MenuTree
134
+ ref = { treeRef }
129
135
onSelect = { ( selectedKeys ) => {
130
136
setParentId ( Number ( selectedKeys [ 0 ] ) ) ;
131
137
} }
@@ -186,8 +192,11 @@ const MenuPage: React.FC = () => {
186
192
if ( actionRef . current ) {
187
193
actionRef . current . reload ( ) ;
188
194
}
195
+ //@ts -ignore
196
+ treeRef . current ?. refresh ( ) ;
189
197
}
190
- } }
198
+ }
199
+ }
191
200
>
192
201
< ProFormText
193
202
hidden = { true }
@@ -199,6 +208,38 @@ const MenuPage: React.FC = () => {
199
208
name = "parentId"
200
209
/>
201
210
211
+ < ProFormTreeSelect
212
+ name = "parentId"
213
+ label = "父级菜单"
214
+ request = { ( ) => {
215
+ const id = form . getFieldValue ( 'id' ) ;
216
+ const fetchTree = ( root : any , list : any ) => {
217
+ return list . map ( ( item : any ) => {
218
+ const disable = root ? root : item . id === id ;
219
+ return {
220
+ label : item . name ,
221
+ value : item . id ,
222
+ disabled : disable ,
223
+ children : item . children ? fetchTree ( disable , item . children ) : [ ]
224
+ }
225
+ } ) ;
226
+ }
227
+ return fetchTree ( null , treeData ) ;
228
+ } }
229
+ fieldProps = {
230
+ {
231
+ onChange : ( value ) => {
232
+ form . setFieldValue ( 'typeId' , value ) ;
233
+ } ,
234
+ showArrow : false ,
235
+ filterTreeNode : true ,
236
+ showSearch : true ,
237
+ multiple : false ,
238
+ treeDefaultExpandAll : true ,
239
+ }
240
+ }
241
+ />
242
+
202
243
< ProFormText
203
244
placeholder = "请输入菜单名称"
204
245
label = "菜单名称"
0 commit comments