feat: Form改造

This commit is contained in:
kailong321200875 2023-06-06 15:05:38 +08:00
parent 1801c1bc73
commit 9c724dc9aa
6 changed files with 46 additions and 74 deletions

View File

@ -27,7 +27,6 @@ export type {
TimeSelectComponentProps, TimeSelectComponentProps,
ColProps, ColProps,
FormSetProps, FormSetProps,
FormValueType,
FormItemProps, FormItemProps,
FormSchema, FormSchema,
FormProps, FormProps,

View File

@ -182,10 +182,7 @@ export default defineComponent({
// formItem // formItem
const renderFormItem = (item: FormSchema) => { const renderFormItem = (item: FormSchema) => {
const formItemSlots: Recordable = setFormItemSlots(slots, item.field) const formItemSlots = {
return (
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label || ''}>
{{
default: () => { default: () => {
if (slots[item.field]) { if (slots[item.field]) {
return getSlot(slots, item.field, formModel.value) return getSlot(slots, item.field, formModel.value)
@ -257,7 +254,21 @@ export default defineComponent({
</Com> </Com>
) )
} }
}, }
}
if (item?.formItemProps?.slots?.label) {
formItemSlots.label = (...args: any[]) => {
return item.formItemProps.slots.label(...args)
}
}
if (item?.formItemProps?.slots?.error) {
formItemSlots.error = (...args: any[]) => {
return item.formItemProps.slots.error(...args)
}
}
return (
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label || ''}>
{{
...formItemSlots ...formItemSlots
}} }}
</ElFormItem> </ElFormItem>

View File

@ -1,6 +1,4 @@
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { type Slots } from 'vue'
import { getSlot } from '@/utils/tsxHelper'
import { PlaceholderModel, FormSchema, ComponentNameEnum, ColProps } from '../types' import { PlaceholderModel, FormSchema, ComponentNameEnum, ColProps } from '../types'
import { isFunction } from '@/utils/is' import { isFunction } from '@/utils/is'
import { firstUpperCase, humpToDash } from '@/utils' import { firstUpperCase, humpToDash } from '@/utils'
@ -156,23 +154,3 @@ export const initModel = (schema: FormSchema[], formModel: Recordable) => {
}) })
return model return model
} }
/**
* @param slots
* @param field
* @returns FormIiem插槽
*/
export const setFormItemSlots = (slots: Slots, field: string): Recordable => {
const slotObj: Recordable = {}
if (slots[`${field}-error`]) {
slotObj['error'] = (...args: any[]) => {
return getSlot(slots, `${field}-error`, args)
}
}
if (slots[`${field}-label`]) {
slotObj['label'] = (...args: any[]) => {
return getSlot(slots, `${field}-label`, args)
}
}
return slotObj
}

View File

@ -53,7 +53,6 @@ type CamelCaseComponentName = keyof typeof ComponentNameEnum extends infer K
export type ComponentName = CamelCaseComponentName export type ComponentName = CamelCaseComponentName
export interface InputComponentProps { export interface InputComponentProps {
value?: string | number
maxlength?: number | string maxlength?: number | string
minlength?: number | string minlength?: number | string
showWordLimit?: boolean showWordLimit?: boolean
@ -99,7 +98,6 @@ export interface InputComponentProps {
} }
export interface AutocompleteComponentProps { export interface AutocompleteComponentProps {
value?: string
placeholder?: string placeholder?: string
clearable?: boolean clearable?: boolean
disabled?: boolean disabled?: boolean
@ -132,7 +130,6 @@ export interface AutocompleteComponentProps {
} }
export interface InputNumberComponentProps { export interface InputNumberComponentProps {
value?: number
min?: number min?: number
max?: number max?: number
step?: number step?: number
@ -167,7 +164,6 @@ export interface SelectOption {
} }
export interface SelectComponentProps { export interface SelectComponentProps {
value?: string | number | boolean | Object
multiple?: boolean multiple?: boolean
disabled?: boolean disabled?: boolean
valueKey?: string valueKey?: string
@ -245,7 +241,6 @@ export interface SelectComponentProps {
} }
export interface SelectV2ComponentProps { export interface SelectV2ComponentProps {
value?: string | number | boolean | Object
multiple?: boolean multiple?: boolean
disabled?: boolean disabled?: boolean
valueKey?: string valueKey?: string
@ -290,7 +285,6 @@ export interface SelectV2ComponentProps {
} }
export interface CascaderComponentProps { export interface CascaderComponentProps {
value?: string | number | string[] | number[] | any
options?: Record<string, unknown>[] options?: Record<string, unknown>[]
props?: CascaderProps props?: CascaderProps
size?: ElementPlusSize size?: ElementPlusSize
@ -325,7 +319,6 @@ export interface CascaderComponentProps {
} }
export interface SwitchComponentProps { export interface SwitchComponentProps {
value?: boolean | string | number
disabled?: boolean disabled?: boolean
loading?: boolean loading?: boolean
size?: ElementPlusSize size?: ElementPlusSize
@ -347,7 +340,6 @@ export interface SwitchComponentProps {
} }
export interface RateComponentProps { export interface RateComponentProps {
value?: number
max?: number max?: number
size?: ElementPlusSize size?: ElementPlusSize
disabled?: boolean disabled?: boolean
@ -375,7 +367,6 @@ export interface RateComponentProps {
} }
export interface ColorPickerComponentProps { export interface ColorPickerComponentProps {
value?: string
disabled?: boolean disabled?: boolean
size?: ElementPlusSize size?: ElementPlusSize
showAlpha?: boolean showAlpha?: boolean
@ -393,7 +384,6 @@ export interface ColorPickerComponentProps {
} }
export interface TransferComponentProps { export interface TransferComponentProps {
value?: any[]
data?: any[] data?: any[]
filterable?: boolean filterable?: boolean
filterPlaceholder?: string filterPlaceholder?: string
@ -444,7 +434,6 @@ export interface RadioOption {
[key: string]: any [key: string]: any
} }
export interface RadioGroupComponentProps { export interface RadioGroupComponentProps {
value?: string | number | boolean
size?: ElementPlusSize size?: ElementPlusSize
disabled?: boolean disabled?: boolean
textColor?: string textColor?: string
@ -472,7 +461,6 @@ export interface RadioGroupComponentProps {
} }
export interface RadioButtonComponentProps { export interface RadioButtonComponentProps {
value?: string | number | boolean
size?: ElementPlusSize size?: ElementPlusSize
disabled?: boolean disabled?: boolean
textColor?: string textColor?: string
@ -518,7 +506,6 @@ export interface CheckboxOption {
} }
export interface CheckboxGroupComponentProps { export interface CheckboxGroupComponentProps {
value?: string[] | number[]
size?: ElementPlusSize size?: ElementPlusSize
disabled?: boolean disabled?: boolean
min?: number min?: number
@ -547,7 +534,6 @@ export interface CheckboxGroupComponentProps {
} }
export interface DividerComponentProps { export interface DividerComponentProps {
value?: number | Array<number>
min?: number min?: number
max?: number max?: number
disabled?: boolean disabled?: boolean
@ -579,7 +565,6 @@ export interface DividerComponentProps {
} }
export interface DatePickerComponentProps { export interface DatePickerComponentProps {
value?: string | Date | number | string[]
readonly?: boolean readonly?: boolean
disabled?: boolean disabled?: boolean
size?: ElementPlusSize size?: ElementPlusSize
@ -631,7 +616,6 @@ export interface DatePickerComponentProps {
} }
export interface DateTimePickerComponentProps { export interface DateTimePickerComponentProps {
value?: string | Date | number | string[]
readonly?: boolean readonly?: boolean
disabled?: boolean disabled?: boolean
editable?: boolean editable?: boolean
@ -672,7 +656,6 @@ export interface DateTimePickerComponentProps {
} }
export interface TimePickerComponentProps { export interface TimePickerComponentProps {
value?: string | Date | number | [Date, Date] | [number, number] | [string, string]
readonly?: boolean readonly?: boolean
disabled?: boolean disabled?: boolean
editable?: boolean editable?: boolean
@ -709,7 +692,6 @@ export interface TimePickerComponentProps {
} }
export interface TimeSelectComponentProps { export interface TimeSelectComponentProps {
value?: string
disabled?: boolean disabled?: boolean
editable?: boolean editable?: boolean
clearable?: boolean clearable?: boolean
@ -754,8 +736,6 @@ export interface FormSetProps {
value: any value: any
} }
export type FormValueType = string | number | string[] | number[] | boolean | undefined | null
export interface FormItemProps { export interface FormItemProps {
labelWidth?: string | number labelWidth?: string | number
required?: boolean required?: boolean
@ -767,6 +747,11 @@ export interface FormItemProps {
for?: string for?: string
validateStatus?: '' | 'error' | 'validating' | 'success' validateStatus?: '' | 'error' | 'validating' | 'success'
style?: CSSProperties style?: CSSProperties
slots?: {
default?: (...args: any[]) => JSX.Element | null
label?: (...args: any[]) => JSX.Element | null
error?: (...args: any[]) => JSX.Element | null
}
} }
export interface FormSchema { export interface FormSchema {
@ -825,7 +810,7 @@ export interface FormSchema {
/** /**
* *
*/ */
value?: FormValueType value?: any
/** /**
* *

View File

@ -1,10 +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' import { FormSchema, FormSetProps, FormProps } from '@/components/Form'
import { FormSchema, FormSetPropsType } from '@/types/form'
export const useForm = (props?: FormProps) => { export const useForm = () => {
// From实例 // From实例
const formRef = ref<typeof Form & FormExpose>() const formRef = ref<typeof Form & FormExpose>()
@ -47,7 +46,7 @@ export const useForm = (props?: FormProps) => {
/** /**
* @param schemaProps schemaProps * @param schemaProps schemaProps
*/ */
setSchema: async (schemaProps: FormSetPropsType[]) => { setSchema: async (schemaProps: FormSetProps[]) => {
const form = await getForm() const form = await getForm()
form?.setSchema(schemaProps) form?.setSchema(schemaProps)
}, },
@ -78,8 +77,6 @@ export const useForm = (props?: FormProps) => {
} }
} }
props && methods.setProps(props)
return { return {
register, register,
formRef: elFormRef, formRef: elFormRef,

View File

@ -23,8 +23,6 @@ const { t } = useI18n()
const isMobile = computed(() => appStore.getMobile) const isMobile = computed(() => appStore.getMobile)
const toggle = ref(false)
const restaurants = ref<Recordable[]>([]) const restaurants = ref<Recordable[]>([])
const querySearch = (queryString: string, cb: Fn) => { const querySearch = (queryString: string, cb: Fn) => {
const results = queryString const results = queryString
@ -679,7 +677,6 @@ const schema = reactive<FormSchema[]>([
return ( return (
<ElOption <ElOption
key={v.value} key={v.value}
disabled={unref(toggle)}
label={v.label} label={v.label}
value={v.value} value={v.value}
/> />
@ -1379,32 +1376,37 @@ const schema = reactive<FormSchema[]>([
{ {
field: 'field68', field: 'field68',
component: 'Divider', component: 'Divider',
label: `${t('formDemo.form')} t('formDemo.slot')}`, label: `${t('formDemo.form')} ${t('formDemo.slot')}`,
}, },
{ {
field: 'field69', field: 'field69',
component: 'Input', component: 'Input',
value: 'test', label: `label`,
label: `default`, formItemProps: {
slots: {
label: ({ label }) => {
return (
<div class="custom-label">
<span class="label-text">custom {label}</span>
</div>
)
}
}
}
}, },
]) ])
const { register, formRef, methods } = useForm({ const { register, formRef, methods } = useForm()
schema,
labelWidth: 'auto',
labelPosition: isMobile.value ? 'top' : 'right'
})
const changeToggle = () => {
toggle.value = !unref(toggle)
}
</script> </script>
<template> <template>
<button @click="changeToggle">测试</button>
<ContentWrap :title="t('formDemo.defaultForm')" :message="t('formDemo.formDes')"> <ContentWrap :title="t('formDemo.defaultForm')" :message="t('formDemo.formDes')">
<Form @register="register" label-width="auto" :label-position="isMobile ? 'top' : 'right'"> <Form
</Form> @register="register"
:schema="schema"
label-width="auto"
:label-position="isMobile ? 'top' : 'right'"
/>
</ContentWrap> </ContentWrap>
</template> </template>