@@ -2,6 +2,7 @@ import React, { PureComponent } from 'react';
22import { Table , Button , Popconfirm } from 'antd' ;
33import { newUUID } from '@/utils/utils' ;
44import { EditableCell , EditableFormRow } from './EditableCell' ;
5+ import AddDialog from './AddDialog' ;
56
67import styles from './index.less' ;
78
@@ -66,6 +67,7 @@ export default class MenuAction extends PureComponent {
6667
6768 this . state = {
6869 dataSource : fillKey ( props . value ) ,
70+ addVisible : false ,
6971 } ;
7072 }
7173
@@ -76,6 +78,73 @@ export default class MenuAction extends PureComponent {
7678 return state ;
7779 }
7880
81+ handleAddCancel = ( ) => {
82+ this . setState ( { addVisible : false } ) ;
83+ } ;
84+
85+ handleAddSubmit = item => {
86+ const tplData = [
87+ {
88+ code : 'query' ,
89+ name : `查询${ item . name } ` ,
90+ method : 'GET' ,
91+ path : item . router ,
92+ } ,
93+ {
94+ code : 'get' ,
95+ name : `精确查询${ item . name } ` ,
96+ method : 'GET' ,
97+ path : `${ item . router } /:id` ,
98+ } ,
99+ {
100+ code : 'create' ,
101+ name : `创建${ item . name } ` ,
102+ method : 'POST' ,
103+ path : item . router ,
104+ } ,
105+ {
106+ code : 'update' ,
107+ name : `更新${ item . name } ` ,
108+ method : 'PUT' ,
109+ path : `${ item . router } /:id` ,
110+ } ,
111+ {
112+ code : 'delete' ,
113+ name : `删除${ item . name } ` ,
114+ method : 'DELETE' ,
115+ path : `${ item . router } /:id` ,
116+ } ,
117+ ] ;
118+
119+ const newData = tplData . map ( v => ( { key : v . code , ...v } ) ) ;
120+
121+ const { dataSource } = this . state ;
122+ const data = [ ...dataSource ] ;
123+ for ( let i = 0 ; i < newData . length ; i += 1 ) {
124+ let exists = false ;
125+ for ( let j = 0 ; j < dataSource . length ; j += 1 ) {
126+ if ( dataSource [ j ] . key === newData [ i ] . key ) {
127+ exists = true ;
128+ break ;
129+ }
130+ }
131+ if ( ! exists ) {
132+ data . push ( newData [ i ] ) ;
133+ }
134+ }
135+
136+ this . setState (
137+ {
138+ dataSource : data ,
139+ } ,
140+ ( ) => {
141+ this . triggerChange ( data ) ;
142+ }
143+ ) ;
144+
145+ this . handleAddCancel ( ) ;
146+ } ;
147+
79148 handleDelete = key => {
80149 const { dataSource } = this . state ;
81150 const data = dataSource . filter ( item => item . key !== key ) ;
@@ -84,6 +153,10 @@ export default class MenuAction extends PureComponent {
84153 } ) ;
85154 } ;
86155
156+ handleAddTpl = ( ) => {
157+ this . setState ( { addVisible : true } ) ;
158+ } ;
159+
87160 handleAdd = ( ) => {
88161 const { dataSource } = this . state ;
89162 const item = {
@@ -127,7 +200,7 @@ export default class MenuAction extends PureComponent {
127200 } ;
128201
129202 render ( ) {
130- const { dataSource } = this . state ;
203+ const { dataSource, addVisible } = this . state ;
131204 const components = {
132205 body : {
133206 row : EditableFormRow ,
@@ -155,6 +228,9 @@ export default class MenuAction extends PureComponent {
155228 < Button onClick = { this . handleAdd } size = "small" type = "primary" >
156229 新增
157230 </ Button >
231+ < Button onClick = { this . handleAddTpl } size = "small" type = "primary" >
232+ 使用模板
233+ </ Button >
158234 </ div >
159235 < Table
160236 components = { components }
@@ -163,6 +239,11 @@ export default class MenuAction extends PureComponent {
163239 columns = { columns }
164240 pagination = { false }
165241 />
242+ < AddDialog
243+ visible = { addVisible }
244+ onCancel = { this . handleAddCancel }
245+ onSubmit = { this . handleAddSubmit }
246+ />
166247 </ div >
167248 ) ;
168249 }
0 commit comments