# Demo
# 文件后缀
*.jsx
1
# Demo
import React, {useEffect} from 'react';
import {
Button,
Page,
Table,
FilterPro, Message, Pagination, ConfirmButton, Dialog, Title, ProForm, Form, Placeholder, TitleCard
} from '@sto/cn-ui';
import {
addCenter,
deleteCenterById, modifyCenter,
queryCenterPage,
queryRegionList
} from "@/pages/ResourceAccess/CenterManage/service";
import DeptManage from "@/pages/ResourceAccess/CenterManage/DeptManage";
export default function CenterManage() {
// React提供的一个性能优化Hook,主要用于在渲染过程中执行一些昂贵的计算,并且仅在依赖项发生变化时重新计算
// 参数1:函数
// 参数2:依赖项数组
// 效果:当依赖项数组内值发生变化,重新执行函数计算结果
// 查询表单
const form = React.useMemo(() => {
// 执行耗时操作并返回
return FilterPro.createForm({
initialValues: {
regionName: '',
centerName: '',
},
effects: () => {
},
});
}, []);
// State Hook让函数组件也可以有state状态, 并进行状态数据的读写操作
// 语法: const [xxx, setXxx] = React.useState(initValue)
// 数组中第一个参数,是后面的值,第二个参数是改变值的方法
// 用法:setXxx((oldValue) => oldValue + 1)
// 常量
const [params, setParams] = React.useState(form.values);
const [regionMetas, setRegionMetas] = React.useState([]);
// 表格
const [stateKey, setKeys] = React.useState({
keys: [],
record: [],
});
const [dataSource, setDataSource] = React.useState([]);
const [currentPage, setCurrentPage] = React.useState(1);
const [pageSize, setPageSize] = React.useState(5);
const [pageTotal, setPageTotal] = React.useState(0);
// 新增表单
const [addVisible, setAddVisible] = React.useState(false);
const addForm = React.useMemo(() => {
return FilterPro.createForm({
initialValues: {
regionId: '',
centerName: '',
},
effects: () => {
},
});
}, []);
const [addFormParams, setAddFormParams] = React.useState(addForm.values);
// 编辑表单
const [updVisible, setUpdVisible] = React.useState(false);
const updForm = React.useMemo(() => {
return FilterPro.createForm({
initialValues: {
id: null,
regionId: '',
centerName: '',
},
effects: () => {
},
});
}, []);
const [updFormParams, setUpdFormParams] = React.useState(updForm.values);
// useEffect 允许你在组件渲染后执行副作用操作,像数据获取这种操作就属于副作用
// 参数1:函数
// 参数2:依赖项数组
// 效果:当依赖项数组内值发生变化,重新执行副作用操作
useEffect(() => {
loadCenterPage(currentPage, pageSize, params).then();
}, []);
// 远程调用
const loadCenterPage = async (currentPage, pageSize, queryParams) => {
const args = {
pageIndex: currentPage,
pageSize: pageSize,
requestModel: {
regionName: queryParams?.regionName,
centerName: queryParams?.centerName,
}
};
const data = await queryCenterPage(args);
const total = data.total;
const records = data.records;
setCurrentPage(currentPage);
setPageSize(pageSize);
setPageTotal(total);
setDataSource(records)
}
// 远程调用
const deleteCenter = async (id) => {
await deleteCenterById({id: id}).then(data => {
Message.success("操作成功");
loadCenterPage(1, pageSize, params).then();
});
}
// 远程调用
const onAddCenterClick = async () => {
const data = await queryRegionList();
const options = [];
for (let dataKey in data) {
options.push({label: data[dataKey]?.regionName, value: data[dataKey]?.regionId});
}
setRegionMetas(options)
addFormParams.regionId = '';
addFormParams.centerName = '';
setAddFormParams(addFormParams)
setAddVisible(true);
}
// 远程调用
const onUpdCenterClick = async (record) => {
const data = await queryRegionList();
const options = [];
for (let dataKey in data) {
options.push({label: data[dataKey]?.regionName, value: data[dataKey]?.regionId});
}
setRegionMetas(options)
updFormParams.id = record.id;
updFormParams.regionId = record.regionId;
updFormParams.centerName = record.centerName;
setUpdFormParams(updFormParams => updFormParams)
setUpdVisible(true);
}
// 渲染的内容
return (
<Page>
<TitleCard title="中心管理" hasGutter>
<FilterPro
form={form}
cacheSearch
onSearch={setParams}
extendButtons={<>
<Button type={'primary'} onClick={onAddCenterClick}>新增</Button>
</>}
onChange={(values) => {
// console.log('changed values:', values);
}}
onReset={() => {
// Message.notice('onReset');
params.regionName = '';
params.centerName = '';
setParams(params);
loadCenterPage(1, pageSize, params).then();
}}
onSubmit={(values) => {
// console.log('submit values:', values);
// Message.notice('onSubmit');
loadCenterPage(1, pageSize, values).then();
}}
/>
<Table
storageKey="centerManageId"
primaryKey="id"
columns={[
{title: '大区名称', dataIndex: 'regionName', width: 200},
{title: '大区编号', dataIndex: 'regionId', width: 200},
{title: '创建人', dataIndex: 'createUserName'},
]}
rowSelection={{
type: 'single',
onChange(keys, record) {
console.log('keys', keys, 'rowSelection.onChange', record);
setKeys({keys, record});
},
selectedRowKeys: stateKey.keys,
}}
operateColumn={{
width: 200,
buttons: [
(record, index) => {
return (
<>
<Button type={'primary'} onClick={() => onUpdCenterClick(record)}
text>编辑</Button>
<ConfirmButton
text
type={'primary'}
style={{marginLeft: 10}}
btnContent="删除"
dialogTitle="中心删除确认"
dialogContent="确认删除当前中心?"
successMsg={false}
errorMsg={false}
onConfirmSuccess={() => {
deleteCenter(record.id).then();
}}
/>
</>
);
},
],
}}
dataSource={dataSource}
/>
<Pagination
currentPage={currentPage}
onChange={(value) => {
setCurrentPage(value)
loadCenterPage(value, pageSize, params).then();
}}
pageSize={pageSize}
onPageSizeChange={(value) => {
setPageSize(value)
loadCenterPage(1, value, params).then();
}}
totalCount={pageTotal}
pageSizeOptions={[5, 10, 15]}
showJump={false}
/>
<Dialog
hasGutter
visible={addVisible}
title={'新增中心'}
closeMode={['close', 'mask', 'esc']}
size={'small'}
onClose={() => setAddVisible(false)}
onCancel={() => setAddVisible(false)}
footer={false}
>
<Form
form={addForm}
onSubmit={(values) => {
// console.error('values', values);
addCenter(values).then(data => {
Message.success('操作成功');
setAddVisible(false);
loadCenterPage(1, pageSize, params).then();
});
}}
onReset={() => {
addFormParams.regionId = '';
addFormParams.centerName = '';
setAddFormParams(addFormParams);
}}
hasFooterSubmit
footerConfig={{mode: 'block'}}
formLayoutProps={{labelCol: {fixedSpan: 5}}}
>
</Form>
</Dialog>
<Dialog
hasGutter
visible={updVisible}
title={'编辑中心'}
closeMode={['close', 'mask', 'esc']}
size={'small'}
onClose={() => setUpdVisible(false)}
onCancel={() => setUpdVisible(false)}
footer={false}
>
<Form
form={updForm}
onSubmit={(values) => {
console.error('values', values);
modifyCenter(values).then(data => {
Message.success('操作成功');
setUpdVisible(false);
loadCenterPage(1, pageSize, params).then();
});
}}
onReset={() => {
updFormParams.regionId = '';
updFormParams.centerName = '';
setUpdFormParams(updFormParams);
}}
hasFooterSubmit
footerConfig={{mode: 'block'}}
formLayoutProps={{labelCol: {fixedSpan: 5}}}
>
</Form>
</Dialog>
</TitleCard>
<TitleCard title="部门管理" hasGutter>
{/*'?.'表达式,判断stateKey内是否有属性record,没有属性,则整个调用链返回undefined*/}s
<DeptManage {...stateKey?.record}/>
</TitleCard>
</Page>
);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# 父子传值
import React from 'react';
export default function ParentDemo() {
const [helloValue, setHelloValue] = React.useState('Hello World!');
return <ChildDemo {...{ name1: helloValue, name2: helloValue + 'hei hei!' }}/>;
}
1
2
3
4
5
6
2
3
4
5
6
import React, {useEffect} from 'react';
// 写法1
export default function ChildDemo(props) {
useEffect(() => {
// 当父组件传入的name1、name2值改变的时候,执行某些方法
doSomething();
}, [props.name1, props.name2]);
return <h1>Hello, {props.name2}</h1>;
}
// 写法2:使用了解构函数
export default function ChildDemo({name1, name2}) {
useEffect(() => {
// 当父组件传入的name1、name2值改变的时候,执行某些方法
doSomething();
}, [name1, name2]);
return <h1>Hello, {name2}</h1>;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19