159 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-04-22 10:22:33 +08:00
//获取表单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位置 topleft
*
*
* 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) {
2025-06-22 18:36:25 +08:00
//console.log(11,key,schemaItem[key],typeof schemaItem[key])
2025-04-22 10:22:33 +08:00
// @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)
}
}
]
2025-06-22 18:36:25 +08:00
}