-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,219 additions
and
680 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./use-crud"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Emitter } from "./types"; | ||
|
||
import { mittAsync } from "@yusui/utils"; | ||
|
||
export function useHooks<T, P>() { | ||
/** emitter */ | ||
const emitter = mittAsync() as Emitter<T, P>; | ||
/** 获取列表数据前 beforeGetList((params)=>{}) */ | ||
const beforeGetList = (cb: (params: P) => Promise<any> | any | void) => { | ||
emitter.on("beforeGetList", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 获取列表数据后 afterGetList((res)=>{}) */ | ||
const afterGetList = (cb: (res: any) => Promise<any> | any | void) => { | ||
emitter.on("afterGetList", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 删除数据前 beforeDel((row)=>{}) */ | ||
const beforeDel = (cb: (row: T) => Promise<any> | any | void) => { | ||
emitter.on("beforeDel", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 删除数据后 afterDel((res)=>{}) */ | ||
const afterDel = (cb: (res: any) => Promise<any> | any | void) => { | ||
emitter.on("afterDel", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 获取表单数据前 beforeGetInfo((urlFormData)=>{}) */ | ||
const beforeGetInfo = (cb: (urlFormData: any) => Promise<any> | any | void) => { | ||
emitter.on("beforeGetInfo", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 获取表单数据后 afterGetInfo((res)=>{}) */ | ||
const afterGetInfo = (cb: (res: any) => Promise<any> | any | void) => { | ||
emitter.on("afterGetInfo", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 提交数据前,可对form内的数据进行修改 beforeSubmit((form)=>{}) */ | ||
const beforeSubmit = (cb: (form: T) => Promise<any> | any | void) => { | ||
emitter.on("beforeSubmit", async (...arg) => await cb?.(...arg)); | ||
}; | ||
/** 提交数据后 afterSubmit((res)=>{}) */ | ||
const afterSubmit = (cb: (res: any) => Promise<any> | any | void) => { | ||
emitter.on("afterSubmit", async (...arg) => await cb?.(...arg)); | ||
}; | ||
|
||
return { | ||
emitter, | ||
beforeGetList, | ||
afterGetList, | ||
beforeDel, | ||
afterDel, | ||
beforeGetInfo, | ||
afterGetInfo, | ||
beforeSubmit, | ||
afterSubmit | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import type { Data } from "@yusui/types"; | ||
import type { UseCrudOptions } from "./types"; | ||
|
||
import { ref, computed, toRefs } from "vue"; | ||
|
||
import { useCrudState } from "./state"; | ||
import { useHooks } from "./hooks"; | ||
import { useCrudMethods } from "./methods"; | ||
import { useLifeCycle } from "./lifeCycle"; | ||
|
||
export function useCrud<T extends Data = Data, P extends Data = Data>(options: UseCrudOptions<T, P>) { | ||
/** listRef.value等同于this.$refs.listRef */ | ||
const listRef = ref(); | ||
/** formRef.value等同于this.$refs.formRef */ | ||
const formRef = ref(); | ||
|
||
/** state */ | ||
const crudState = useCrudState(options); | ||
const crudStateRefs = toRefs(crudState); | ||
|
||
/** hooks */ | ||
const { | ||
emitter, | ||
beforeGetList, | ||
afterGetList, | ||
beforeDel, | ||
afterDel, | ||
beforeGetInfo, | ||
afterGetInfo, | ||
beforeSubmit, | ||
afterSubmit | ||
} = useHooks(); | ||
|
||
/** methods */ | ||
let { | ||
getDataList, | ||
handleDel, | ||
loadMore, | ||
handleRefresh, | ||
handleSearch, | ||
filterChange, | ||
handleAdd, | ||
handleEdit, | ||
handleView, | ||
getFormData, | ||
handleSubmit | ||
// @ts-ignore | ||
} = useCrudMethods({ crudState, emitter }); | ||
|
||
/** 重置 */ | ||
getDataList = options.getDataList ?? getDataList; | ||
handleDel = options.handleDel ?? handleDel; | ||
loadMore = options.loadMore ?? loadMore; | ||
handleRefresh = options.handleRefresh ?? handleRefresh; | ||
handleSearch = options.handleSearch ?? handleSearch; | ||
filterChange = options.filterChange ?? filterChange; | ||
handleAdd = options.handleAdd ?? handleAdd; | ||
handleEdit = options.handleEdit ?? handleEdit; | ||
handleView = options.handleView ?? handleView; | ||
getFormData = options.getFormData ?? getFormData; | ||
handleSubmit = options.handleSubmit ?? handleSubmit; | ||
|
||
/** 使用v-bind绑定的值 */ | ||
const bindVal = computed(() => ({ | ||
// 属性 | ||
ref: "listRef", | ||
data: crudState.listData, | ||
option: crudState.listOption ?? {}, //列表配置 | ||
// filterForm: crudState.searchForm, | ||
status: crudState.loadStatus, | ||
scrollTop: crudState.scrollTop, | ||
searchValue: crudState.searchForm[crudState.crudOption.searchKey] ?? "", | ||
// 事件 | ||
onSearch: handleSearch, | ||
onLoadmore: loadMore, | ||
// onFilterChange: filterChange, | ||
"onUpdate:filterForm": (form: any) => Object.assign(crudState.searchForm, form), | ||
"onUpdate:searchValue": (val: any) => (crudState.searchForm[crudState.crudOption.searchKey] = val) | ||
})); | ||
|
||
/** 使用一些默认的生命周期 */ | ||
useLifeCycle({ crudState, loadMore, handleRefresh }); | ||
|
||
return { | ||
listRef, | ||
formRef, | ||
crudState, | ||
crudStateRefs, | ||
bindVal, | ||
getDataList, | ||
handleDel, | ||
loadMore, | ||
handleRefresh, | ||
handleSearch, | ||
filterChange, | ||
handleAdd, | ||
handleEdit, | ||
handleView, | ||
getFormData, | ||
handleSubmit, | ||
beforeGetList, | ||
afterGetList, | ||
beforeDel, | ||
afterDel, | ||
beforeGetInfo, | ||
afterGetInfo, | ||
beforeSubmit, | ||
afterSubmit | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { onReachBottom, onPullDownRefresh, onPageScroll } from "@dcloudio/uni-app"; | ||
|
||
export const useLifeCycle = ({ crudState, loadMore, handleRefresh }: any) => { | ||
/** 触底加载更多数据 */ | ||
onReachBottom(() => { | ||
const { isReachBottom } = crudState.crudOption; | ||
isReachBottom && loadMore(); | ||
}); | ||
/** 下拉刷新 */ | ||
onPullDownRefresh(() => { | ||
const { isPullDown } = crudState.crudOption; | ||
isPullDown && handleRefresh(); | ||
}); | ||
/** @ts-ignore 返回顶部按钮 */ | ||
onPageScroll(e => { | ||
if (crudState.crudOption.isSrollTop) { | ||
crudState.scrollTop = e.scrollTop; | ||
} | ||
}); | ||
}; |
Oops, something went wrong.