Skip to content

Commit ff4fcbd

Browse files
committed
feat: add function list.reset & list.proxy.reset
1 parent 436e372 commit ff4fcbd

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,23 @@ const { list } = useAdmate()
357357
list.search() // 手动检索
358358
```
359359

360+
#### list.reset
361+
362+
重置筛选条件后执行 `list.read`
363+
364+
```ts
365+
const { list } = useAdmate()
366+
367+
/**
368+
* PS: 以下为原始函数签名,如果你配置了 list.proxy.reset ,则以 list.proxy.reset 为准
369+
*
370+
* @param {any} [payload = list.filter]
371+
* @param {'data'|'params'|'config'} [payloadAs] 指定 payload 的用途
372+
* @returns {Promise<any>} 接口返回值
373+
*/
374+
list.reset() // 手动重置
375+
```
376+
360377
#### list.proxy.read
361378

362379
你可以使用 `list.proxy.read` 来代理 `list.read`,以便在 `list.read` 前后做一些操作,或改变 `list.read` 的行为
@@ -430,6 +447,41 @@ const { list } = useAdmate({
430447
})
431448
```
432449

450+
#### list.proxy.reset
451+
452+
你可以使用 `list.proxy.reset` 来代理 `list.reset`,以便在 `list.reset` 前后执行一些操作,或改变 `list.reset` 的行为
453+
454+
```ts
455+
useAdmate({
456+
list: {
457+
proxy: {
458+
/**
459+
* @param {Function} resetList 被代理的原始 resetList
460+
*/
461+
reset(resetList) {},
462+
},
463+
},
464+
})
465+
```
466+
467+
```ts
468+
// 示例: 使用 UI 组件库的表单重置函数来重置筛选条件
469+
470+
useAdmate({
471+
list: {
472+
proxy: {
473+
reset(resetList) {
474+
listFilterElFormRef.value.resetFields()
475+
// 如果分页组件不归属于表单,则表单重置时页码不会被重置,需调用 list.search
476+
if (!list.watchFilter) {
477+
list.search()
478+
}
479+
},
480+
}
481+
}
482+
})
483+
```
484+
433485
<br>
434486

435487
### 读取状态

src/index.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ export default function useAdmate({
9494
data: [],
9595
loading: false,
9696
total: 0,
97-
...userProp?.pageNumberAt && {
98-
filter: setValue({}, userProp.pageNumberAt, 1),
99-
},
97+
filter: setValue({}, userProp?.pageNumberAt, 1),
10098
watchFilter: true,
10199
debounceInterval: 300,
100+
proxy: {},
102101
}),
103102
defaultIsDynamic: true,
104103
})
105104

106-
const listExported = reactive(getInitialList())
105+
const initialList = getInitialList()
106+
const initialListFilter = cloneDeep(initialList.filter)
107+
const listExported = reactive(initialList)
107108

108109
const getInitialForm = () =>
109110
cloneDeep({
@@ -116,6 +117,7 @@ export default function useAdmate({
116117
loading: false,
117118
submitting: false,
118119
title: '',
120+
proxy: {},
119121
...form,
120122
})
121123

@@ -157,6 +159,13 @@ export default function useAdmate({
157159
})
158160
}
159161

162+
const resetList = (payload, payloadAs) => {
163+
listExported.filter = cloneDeep(initialListFilter)
164+
if (!listExported.watchFilter) {
165+
readList(payload = listExported.filter, payloadAs)
166+
}
167+
}
168+
160169
let oldPageNumber = 1
161170

162171
listExported.read = (...args) => {
@@ -216,6 +225,21 @@ export default function useAdmate({
216225
} */
217226
}
218227

228+
// 列表重置
229+
listExported.reset = (...args) => {
230+
// args 是用户直接调用 list.reset 传的参,优先级低
231+
// argsProxy 是用户在 list.proxy.reset 内部调用 resetList 传的参,优先级高
232+
const result = listExported.proxy.reset
233+
? listExported.proxy.reset(
234+
(...argsProxy) => resetList(...(argsProxy.length ? argsProxy : args)),
235+
)
236+
: resetList(...args)
237+
238+
readListTrigger.value = undefined
239+
240+
return result
241+
}
242+
219243
// 列表筛选,页码重置
220244
listExported.search = (...args) => {
221245
if (getValue(listExported.filter, listExported.pageNumberAt) === 1) {

0 commit comments

Comments
 (0)