perf: add useCrudSchemas demo
This commit is contained in:
@@ -18,8 +18,8 @@ type CrudSearchParams = {
|
||||
show?: boolean
|
||||
// 字典名称,会去取全局的字典
|
||||
dictName?: string
|
||||
// 接口路径
|
||||
dictUrl?: string
|
||||
// 接口
|
||||
api?: () => Promise<any>
|
||||
} & Omit<FormSchema, 'field'>
|
||||
|
||||
type CrudTableParams = {
|
||||
@@ -28,6 +28,10 @@ type CrudTableParams = {
|
||||
} & Omit<FormSchema, 'field'>
|
||||
|
||||
type CrudFormParams = {
|
||||
// 字典名称,会去取全局的字典
|
||||
dictName?: string
|
||||
// 接口
|
||||
api?: () => Promise<any>
|
||||
// 是否显示表单项
|
||||
show?: boolean
|
||||
} & Omit<FormSchema, 'field'>
|
||||
@@ -68,7 +72,7 @@ export const useCrudSchemas = (
|
||||
const tableColumns = filterTableSchema(crudSchema)
|
||||
allSchemas.tableColumns = tableColumns || []
|
||||
|
||||
const formSchema = filterFormSchema(crudSchema)
|
||||
const formSchema = filterFormSchema(crudSchema, allSchemas)
|
||||
allSchemas.formSchema = formSchema
|
||||
|
||||
const detailSchema = filterDescriptionsSchema(crudSchema)
|
||||
@@ -157,27 +161,57 @@ const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => {
|
||||
}
|
||||
|
||||
// 过滤 form 结构
|
||||
const filterFormSchema = (crudSchema: CrudSchema[]): FormSchema[] => {
|
||||
const filterFormSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
|
||||
const formSchema: FormSchema[] = []
|
||||
|
||||
// 获取字典列表队列
|
||||
const formRequestTask: Array<() => Promise<void>> = []
|
||||
|
||||
eachTree(crudSchema, (schemaItem: CrudSchema) => {
|
||||
// 判断是否显示
|
||||
if (schemaItem?.form?.show !== false) {
|
||||
if (schemaItem?.form?.show) {
|
||||
const formSchemaItem = {
|
||||
// 默认为 input
|
||||
component: (schemaItem.form && schemaItem.form.component) || 'Input',
|
||||
component: schemaItem.form.component || 'Input',
|
||||
componentProps: {},
|
||||
...schemaItem.form,
|
||||
field: schemaItem.field,
|
||||
label: schemaItem.form?.label || schemaItem.label
|
||||
label: schemaItem.search?.label || schemaItem.label
|
||||
}
|
||||
|
||||
if (formSchemaItem.dictName) {
|
||||
// 如果有 dictName 则证明是从字典中获取数据
|
||||
const dictArr = dictStore.getDictObj[formSchemaItem.dictName]
|
||||
formSchemaItem.componentProps!.options = filterOptions(dictArr)
|
||||
} else if (formSchemaItem.api) {
|
||||
formRequestTask.push(async () => {
|
||||
const res = await (formSchemaItem.api as () => AxiosPromise)()
|
||||
if (res) {
|
||||
const index = findIndex(allSchemas.formSchema, (v: FormSchema) => {
|
||||
return v.field === formSchemaItem.field
|
||||
})
|
||||
if (index !== -1) {
|
||||
allSchemas.formSchema[index]!.componentProps!.options = filterOptions(
|
||||
res,
|
||||
formSchemaItem.componentProps.optionsAlias?.labelField
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 删除不必要的字段
|
||||
delete formSchemaItem.show
|
||||
delete formSchemaItem.dictName
|
||||
|
||||
formSchema.push(formSchemaItem)
|
||||
}
|
||||
})
|
||||
|
||||
for (const task of formRequestTask) {
|
||||
task()
|
||||
}
|
||||
|
||||
return formSchema
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user