Skip to content

Commit 331dd51

Browse files
committed
feat: TablePageBox 适配 初始 不需要请求,依赖外部请求到参数之后,再手动调用请求,或者可以配置初始调用参数
1 parent 3e7f2af commit 331dd51

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/components/TablePageBox.tsx

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export interface TablePageBoxProps<T = any, F = any> {
9696
* @description 弹窗删除函数,一般用于删除数据。|| Modal delete function, generally used to delete data.
9797
*/
9898
modalDeleteFn: (record: T) => void
99+
/**
100+
* @description 获取表格数据的函数,当 getOptions.manual 为 true 时,需要手动调用该函数获取数据。|| Function to get table data, when getOptions.manual is true, you need to manually call this function to get the data.
101+
*/
102+
getRun: (params: { page: number, pageSize: number, [key: string]: any }) => void
99103
}>
100104
/**
101105
* @description 弹窗标题。|| Modal title.
@@ -110,6 +114,19 @@ export interface TablePageBoxProps<T = any, F = any> {
110114
total: number
111115
}
112116
}>>
117+
/**
118+
* @description 获取表格数据的参数配置。|| Configuration for parameters to get table data.
119+
*/
120+
getOptions?: {
121+
/**
122+
* @description 是否手动触发获取表格数据,默认为 false ,如果设置为 true ,则需要使用 ref 手动调用 getRun 函数获取数据。|| Whether to manually trigger the function to get table data, default is false, if set to true, you need to manually call the getRun function through ref to get the data.
123+
*/
124+
manual?: boolean,
125+
/**
126+
* @description 默认获取表格数据的参数,当手动触发获取表格数据时,会合并默认参数和手动传入的参数。|| Default parameters to get table data, when manually trigger the function to get table data, the default parameters and manually passed parameters will be merged.
127+
*/
128+
defaultParams?: any,
129+
}
113130
/**
114131
* @description 添加表格数据的函数。|| Function to add table data.
115132
*/
@@ -152,6 +169,9 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
152169
ref,
153170
modalTitle,
154171
getFn,
172+
getOptions = {
173+
manual: false,
174+
},
155175
addFn,
156176
putFn,
157177
delFn,
@@ -177,9 +197,12 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
177197
}
178198
}, [isModalOpen, formData, formModal])
179199

180-
const { data: tableData, error, loading, refresh, run: getStudentListRun } = useRequest(getFn, {
200+
const { data: tableData, error, loading, refresh, run: getRun } = useRequest(getFn, {
201+
...getOptions,
202+
retryCount: 3,
181203
defaultParams: [
182204
{
205+
...(getOptions.defaultParams || {}),
183206
page,
184207
pageSize,
185208
}
@@ -190,33 +213,36 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
190213
if (data.total > 0 && page > 1 && data.data.length === 0) {
191214
const newPage = Math.ceil(data.total / pageSize)
192215
setPage(newPage)
193-
getStudentListRun({
216+
getRun({
194217
page: newPage,
195218
pageSize,
196219
...formQuery.getFieldsValue()
197220
})
198221
}
199222
}
200223
})
201-
const { run: addStudentRun, loading: addLoading } = useRequest(addFn, {
224+
const { run: addRun, loading: addLoading } = useRequest(addFn, {
202225
manual: true,
226+
retryCount: 3,
203227
onSuccess: () => {
204228
refresh()
205229
setIsModalOpen(false)
206230
}
207231
})
208-
const { run: putStudentRun, loading: putLoading } = useRequest(putFn, {
232+
const { run: putRun, loading: putLoading } = useRequest(putFn, {
209233
manual: true,
234+
retryCount: 3,
210235
onSuccess: () => {
211236
refresh()
212237
setIsModalOpen(false)
213238
}
214239
})
215-
const { runAsync: deleteStudentRun } = useRequest(delFn, {
216-
manual: true
240+
const { runAsync: deleteRunAsync } = useRequest(delFn, {
241+
manual: true,
242+
retryCount: 3,
217243
})
218244
const handleDeleteOk = async (id: number) => {
219-
await deleteStudentRun(id)
245+
await deleteRunAsync(id)
220246
refresh()
221247
}
222248
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -248,9 +274,10 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
248274
formQuery,
249275
modalAddFn,
250276
modalEditFn,
251-
modalDeleteFn
277+
modalDeleteFn,
278+
getRun,
252279
}
253-
}, [formQuery, modalEditFn, modalDeleteFn])
280+
}, [formQuery, modalEditFn, modalDeleteFn, getRun])
254281

255282
const operationButtonIcon = ['icon', 'icon-text'].includes(operationButtonDisplay)
256283
const operationButtonText = ['text', 'icon-text'].includes(operationButtonDisplay)
@@ -269,12 +296,12 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
269296

270297
const handleModalOk = (values: T) => {
271298
if (isEdit) {
272-
putStudentRun({
299+
putRun({
273300
...formData,
274301
...values
275302
})
276303
} else {
277-
addStudentRun(values)
304+
addRun(values)
278305
}
279306
}
280307
const handleModalCancel = () => {
@@ -331,7 +358,7 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
331358
onChange={(pagination) => {
332359
setPage(pagination.current!)
333360
setPageSize(pagination.pageSize!)
334-
getStudentListRun({
361+
getRun({
335362
page: pagination.current!,
336363
pageSize: pagination.pageSize!,
337364
})
@@ -342,7 +369,7 @@ function TablePageBox<T = any, F = any>(props: TablePageBoxProps<T, F>) {
342369
layout: 'inline',
343370
onFinish: (values) => {
344371
setPage(1)
345-
getStudentListRun({
372+
getRun({
346373
page: 1,
347374
pageSize,
348375
...values

0 commit comments

Comments
 (0)