159 lines
5.6 KiB
TypeScript
159 lines
5.6 KiB
TypeScript
//获取表单Schema
|
||
import {useFormModel} from "@/components/BasicForm/hooks/useFormModel";
|
||
import {forEach, isObject, mapValues} from "lodash";
|
||
|
||
export function ifShow(item: FormsSchema, model: any) {
|
||
if (!item.field || item.colSlot) return false
|
||
if (typeof item.ifShow == 'undefined') return true
|
||
if (typeof item.ifShow == 'boolean') return !item.ifShow
|
||
if (typeof item.ifShow == 'function') return !item.ifShow(model)
|
||
return true
|
||
}
|
||
|
||
export function ifTitleShow(item: FormsSchema, model: any) {
|
||
if (typeof item.ifShow == 'undefined') return true
|
||
if (typeof item.ifShow == 'boolean') return !item.ifShow
|
||
if (typeof item.ifShow == 'function') return !item.ifShow(model)
|
||
return true
|
||
}
|
||
|
||
/*
|
||
* 接收两个参数
|
||
* formsProps 参数文档 https://uniapp.dcloud.net.cn/component/uniui/uni-forms.html#forms-props
|
||
* schema 表单配置配置如下
|
||
* field 数据库字段名,
|
||
* label 展示名称
|
||
* component 对应组建名称
|
||
* required 是否必填
|
||
* componentProps 对应组建参数,看各组建文档
|
||
* ifShow 是否显示
|
||
* slot 输入区域插槽
|
||
* colSlot 整排插槽
|
||
*
|
||
* itemProps 表单item参数,参数如下
|
||
* leftIcon 左边图标
|
||
* labelWidth label宽度
|
||
* labelPosition label位置 top,left
|
||
*
|
||
* 导出方法
|
||
* register register 用于注册 useForm
|
||
* getSchema 获取表单配置
|
||
* setSchema 修改表单配置
|
||
* getValue 获取表单内容
|
||
* setValue 设置表单内容
|
||
* */
|
||
export function useForm(options: FormOptions): UseForm {
|
||
const {setFormModel, validate, getModel, closeValue} = useFormModel()
|
||
options = reactive(options)
|
||
|
||
let objs = {}
|
||
forEach(options.schema, (value) => {
|
||
if (ifShow(value, getModel()) && value.field) {
|
||
// @ts-ignore
|
||
objs[value.field] = value['defaultValue'] ? value['defaultValue'] : undefined
|
||
}
|
||
})
|
||
setFormModel(objs)
|
||
|
||
options.formModel = getModel()
|
||
let formRefs: any
|
||
const register = (callback: (i: FormOptions) => void, formRef: any, model: any) => {
|
||
formRefs = formRef
|
||
callback(options)
|
||
setFormModel(model)
|
||
}
|
||
return [
|
||
register,
|
||
{
|
||
|
||
setDisabled: (e: boolean) => {
|
||
forEach(options.schema, (v) => {
|
||
if (v.componentProps && isObject(v.componentProps)) {
|
||
// @ts-ignore
|
||
v.componentProps['disabled'] = e
|
||
} else {
|
||
v.componentProps = {}
|
||
v.componentProps['disabled'] = e
|
||
}
|
||
})
|
||
if (formRefs && formRefs.value) {
|
||
formRefs.value.refreshProps()
|
||
}
|
||
},
|
||
getSchema: () => {
|
||
return options.schema
|
||
},
|
||
setSchema: (schema: FormsSchema[] | FormsSchema) => {
|
||
function algorithm(schemaItem: FormsSchema) {
|
||
let upSchemaList: FormsSchema = options.schema.filter(item => item.field === schemaItem.field)[0]
|
||
for (const key in schemaItem) {
|
||
//console.log(11,key,schemaItem[key],typeof schemaItem[key])
|
||
// @ts-ignore
|
||
if (typeof schemaItem[key] === 'object') {
|
||
// @ts-ignore
|
||
for (const ckey in schemaItem[key]) {
|
||
// @ts-ignore
|
||
upSchemaList[key][ckey] = schemaItem[key][ckey]
|
||
}
|
||
} else {
|
||
// @ts-ignore
|
||
upSchemaList[key] = schemaItem[key]
|
||
}
|
||
}
|
||
}
|
||
|
||
if (Array.isArray(schema)) {
|
||
schema.forEach((schemaItem: FormsSchema) => {
|
||
algorithm(schemaItem)
|
||
})
|
||
} else {
|
||
algorithm(schema)
|
||
}
|
||
},
|
||
getValue: async (isValidate: boolean = true) => {
|
||
if (formRefs.value) {
|
||
if (isValidate) {
|
||
try {
|
||
return await validate(toRaw(formRefs.value.formRules))
|
||
} catch (e) {
|
||
throw e
|
||
}
|
||
} else {
|
||
let newObj: any = {}
|
||
let value = getModel()
|
||
for (const key in value) {
|
||
if (value[key] != undefined || value[key] != null) {
|
||
newObj[key] = value[key]
|
||
}
|
||
}
|
||
return newObj
|
||
}
|
||
}
|
||
},
|
||
closeModel: () => {
|
||
let newValue = mapValues(formRefs.value.model, () => undefined)
|
||
if (formRefs && formRefs.value) {
|
||
formRefs.value.refreshModel(newValue)
|
||
}
|
||
options.formModel = newValue
|
||
closeValue(newValue)
|
||
},
|
||
setValue: (value: any) => {
|
||
let newValue: any = mapValues(value, (v, k) => {
|
||
if (k in objs) {
|
||
return value[k]
|
||
}
|
||
})
|
||
if (value['id']) {
|
||
newValue['id'] = value['id']
|
||
}
|
||
if (formRefs && formRefs.value) {
|
||
formRefs.value.refreshModel(newValue)
|
||
}
|
||
options.formModel = Object.assign({}, options.formModel, newValue)
|
||
setFormModel(newValue)
|
||
}
|
||
}
|
||
]
|
||
}
|