perf: update useForm hook
This commit is contained in:
parent
472f574f42
commit
8a958cd71d
|
@ -18,6 +18,7 @@ import { useRenderChcekbox } from './components/useRenderChcekbox'
|
||||||
import { useDesign } from '@/hooks/web/useDesign'
|
import { useDesign } from '@/hooks/web/useDesign'
|
||||||
import { findIndex } from '@/utils'
|
import { findIndex } from '@/utils'
|
||||||
import { set } from 'lodash-es'
|
import { set } from 'lodash-es'
|
||||||
|
import { FormProps } from './types'
|
||||||
|
|
||||||
const { getPrefixCls } = useDesign()
|
const { getPrefixCls } = useDesign()
|
||||||
|
|
||||||
|
@ -51,11 +52,11 @@ export default defineComponent({
|
||||||
const elFormRef = ref<ComponentRef<typeof ElForm>>()
|
const elFormRef = ref<ComponentRef<typeof ElForm>>()
|
||||||
|
|
||||||
// useForm传入的props
|
// useForm传入的props
|
||||||
const outsideProps = ref<Recordable>({})
|
const outsideProps = ref<FormProps>({ ...props })
|
||||||
|
|
||||||
const getProps = computed(() => Object.assign({ ...props }, unref(outsideProps)))
|
const getProps = computed(() => {
|
||||||
|
return { ...props, ...unref(outsideProps) }
|
||||||
const { schema, isCol, isCustom, autoSetPlaceholder } = unref(getProps)
|
})
|
||||||
|
|
||||||
// 表单数据
|
// 表单数据
|
||||||
const formModel = ref<Recordable>({})
|
const formModel = ref<Recordable>({})
|
||||||
|
@ -69,11 +70,13 @@ export default defineComponent({
|
||||||
formModel.value = Object.assign(unref(formModel), data)
|
formModel.value = Object.assign(unref(formModel), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
const setProps = (props: Recordable) => {
|
const setProps = (props: FormProps = {}) => {
|
||||||
outsideProps.value = props
|
outsideProps.value = Object.assign(unref(formModel), props)
|
||||||
}
|
}
|
||||||
|
|
||||||
const delSchema = (field: string) => {
|
const delSchema = (field: string) => {
|
||||||
|
const { schema } = unref(getProps)
|
||||||
|
|
||||||
const index = findIndex(schema, (v: FormSchema) => v.field === field)
|
const index = findIndex(schema, (v: FormSchema) => v.field === field)
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
schema.splice(index, 1)
|
schema.splice(index, 1)
|
||||||
|
@ -81,6 +84,7 @@ export default defineComponent({
|
||||||
}
|
}
|
||||||
|
|
||||||
const addSchema = (formSchema: FormSchema, index?: number) => {
|
const addSchema = (formSchema: FormSchema, index?: number) => {
|
||||||
|
const { schema } = unref(getProps)
|
||||||
if (index !== void 0) {
|
if (index !== void 0) {
|
||||||
schema.splice(index, 0, formSchema)
|
schema.splice(index, 0, formSchema)
|
||||||
return
|
return
|
||||||
|
@ -89,6 +93,7 @@ export default defineComponent({
|
||||||
}
|
}
|
||||||
|
|
||||||
const setSchema = (schemaProps: FormSetPropsType[]) => {
|
const setSchema = (schemaProps: FormSetPropsType[]) => {
|
||||||
|
const { schema } = unref(getProps)
|
||||||
for (const v of schema) {
|
for (const v of schema) {
|
||||||
for (const item of schemaProps) {
|
for (const item of schemaProps) {
|
||||||
if (v.field === item.field) {
|
if (v.field === item.field) {
|
||||||
|
@ -114,8 +119,8 @@ export default defineComponent({
|
||||||
|
|
||||||
// 监听表单结构化数组,重新生成formModel
|
// 监听表单结构化数组,重新生成formModel
|
||||||
watch(
|
watch(
|
||||||
() => schema,
|
() => unref(getProps).schema,
|
||||||
(schema) => {
|
(schema = []) => {
|
||||||
formModel.value = initModel(schema, unref(formModel))
|
formModel.value = initModel(schema, unref(formModel))
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -126,6 +131,7 @@ export default defineComponent({
|
||||||
|
|
||||||
// 渲染包裹标签,是否使用栅格布局
|
// 渲染包裹标签,是否使用栅格布局
|
||||||
const renderWrap = () => {
|
const renderWrap = () => {
|
||||||
|
const { isCol } = unref(getProps)
|
||||||
const content = isCol ? (
|
const content = isCol ? (
|
||||||
<ElRow gutter={20}>{renderFormItemWrap()}</ElRow>
|
<ElRow gutter={20}>{renderFormItemWrap()}</ElRow>
|
||||||
) : (
|
) : (
|
||||||
|
@ -137,6 +143,8 @@ export default defineComponent({
|
||||||
// 是否要渲染el-col
|
// 是否要渲染el-col
|
||||||
const renderFormItemWrap = () => {
|
const renderFormItemWrap = () => {
|
||||||
// hidden属性表示隐藏,不做渲染
|
// hidden属性表示隐藏,不做渲染
|
||||||
|
const { schema = [], isCol } = unref(getProps)
|
||||||
|
|
||||||
return schema
|
return schema
|
||||||
.filter((v) => !v.hidden)
|
.filter((v) => !v.hidden)
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
|
@ -176,6 +184,9 @@ export default defineComponent({
|
||||||
const Com = componentMap[item.component as string] as ReturnType<
|
const Com = componentMap[item.component as string] as ReturnType<
|
||||||
typeof defineComponent
|
typeof defineComponent
|
||||||
>
|
>
|
||||||
|
|
||||||
|
const { autoSetPlaceholder } = unref(getProps)
|
||||||
|
|
||||||
return slots[item.field] ? (
|
return slots[item.field] ? (
|
||||||
getSlot(slots, item.field, { item })
|
getSlot(slots, item.field, { item })
|
||||||
) : (
|
) : (
|
||||||
|
@ -233,7 +244,10 @@ export default defineComponent({
|
||||||
<ElForm ref={elFormRef} {...getFormBindValue()} model={formModel} class={prefixCls}>
|
<ElForm ref={elFormRef} {...getFormBindValue()} model={formModel} class={prefixCls}>
|
||||||
{{
|
{{
|
||||||
// 如果需要自定义,就什么都不渲染,而是提供默认插槽
|
// 如果需要自定义,就什么都不渲染,而是提供默认插槽
|
||||||
default: () => (isCustom ? getSlot(slots, 'default') : renderWrap())
|
default: () => {
|
||||||
|
const { isCustom } = unref(getProps)
|
||||||
|
return isCustom ? getSlot(slots, 'default') : renderWrap()
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
</ElForm>
|
</ElForm>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
import { useI18n } from '@/hooks/web/useI18n'
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
import type { Slots } from 'vue'
|
import type { Slots } from 'vue'
|
||||||
import { getSlot } from '@/utils/tsxHelper'
|
import { getSlot } from '@/utils/tsxHelper'
|
||||||
|
import { PlaceholderMoel } from './types'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
|
|
||||||
interface PlaceholderMoel {
|
|
||||||
placeholder?: string
|
|
||||||
startPlaceholder?: string
|
|
||||||
endPlaceholder?: string
|
|
||||||
rangeSeparator?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param schema 对应组件数据
|
* @param schema 对应组件数据
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
export interface PlaceholderMoel {
|
||||||
|
placeholder?: string
|
||||||
|
startPlaceholder?: string
|
||||||
|
endPlaceholder?: string
|
||||||
|
rangeSeparator?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FormProps = {
|
||||||
|
schema?: FormSchema[]
|
||||||
|
isCol?: boolean
|
||||||
|
model?: Recordable
|
||||||
|
autoSetPlaceholder?: boolean
|
||||||
|
isCustom?: boolean
|
||||||
|
labelWidth?: string | number
|
||||||
|
} & Recordable
|
|
@ -1,8 +1,9 @@
|
||||||
import type { Form, FormExpose } from '@/components/Form'
|
import type { Form, FormExpose } from '@/components/Form'
|
||||||
import type { ElForm } from 'element-plus'
|
import type { ElForm } from 'element-plus'
|
||||||
import { ref, unref, nextTick } from 'vue'
|
import { ref, unref, nextTick } from 'vue'
|
||||||
|
import type { FormProps } from '@/components/Form/src/types'
|
||||||
|
|
||||||
export const useForm = () => {
|
export const useForm = (props?: FormProps) => {
|
||||||
// From实例
|
// From实例
|
||||||
const formRef = ref<typeof Form & FormExpose>()
|
const formRef = ref<typeof Form & FormExpose>()
|
||||||
|
|
||||||
|
@ -19,11 +20,11 @@ export const useForm = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const getForm = async () => {
|
const getForm = async () => {
|
||||||
|
await nextTick()
|
||||||
const form = unref(formRef)
|
const form = unref(formRef)
|
||||||
if (!form) {
|
if (!form) {
|
||||||
console.error('The form is not registered. Please use the register method to register')
|
console.error('The form is not registered. Please use the register method to register')
|
||||||
}
|
}
|
||||||
await nextTick()
|
|
||||||
return form
|
return form
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +37,9 @@ export const useForm = () => {
|
||||||
addSchema: (formSchema: FormSchema, index?: number) => void
|
addSchema: (formSchema: FormSchema, index?: number) => void
|
||||||
delSchema: (field: string) => void
|
delSchema: (field: string) => void
|
||||||
} = {
|
} = {
|
||||||
setProps: async (props: Recordable = {}) => {
|
setProps: async (props: FormProps = {}) => {
|
||||||
const form = await getForm()
|
const form = await getForm()
|
||||||
|
console.log(form)
|
||||||
form?.setProps(props)
|
form?.setProps(props)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -80,6 +82,8 @@ export const useForm = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
props && methods.setProps(props)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
register,
|
register,
|
||||||
elFormRef,
|
elFormRef,
|
||||||
|
|
|
@ -89,7 +89,9 @@ const schema = reactive<FormSchema[]>([
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
||||||
const { register, methods, elFormRef } = useForm()
|
const { register, methods, elFormRef } = useForm({
|
||||||
|
schema
|
||||||
|
})
|
||||||
|
|
||||||
const changeLabelWidth = (width: number | string) => {
|
const changeLabelWidth = (width: number | string) => {
|
||||||
const { setProps } = methods
|
const { setProps } = methods
|
||||||
|
@ -251,6 +253,6 @@ const verifyReset = () => {
|
||||||
<ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
|
<ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
|
||||||
</ContentWrap>
|
</ContentWrap>
|
||||||
<ContentWrap :title="`UseForm ${t('formDemo.example')}`">
|
<ContentWrap :title="`UseForm ${t('formDemo.example')}`">
|
||||||
<Form :schema="schema" @register="register" />
|
<Form @register="register" />
|
||||||
</ContentWrap>
|
</ContentWrap>
|
||||||
</template>
|
</template>
|
||||||
|
|
Loading…
Reference in New Issue