Skip to content

Commit

Permalink
feat: Form改造
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Jun 6, 2023
1 parent 1801c1b commit 9c724dc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/components/Form/index.ts
Expand Up @@ -27,7 +27,6 @@ export type {
TimeSelectComponentProps,
ColProps,
FormSetProps,
FormValueType,
FormItemProps,
FormSchema,
FormProps,
Expand Down
23 changes: 17 additions & 6 deletions src/components/Form/src/Form.vue
Expand Up @@ -182,11 +182,8 @@ export default defineComponent({
// 渲染formItem
const renderFormItem = (item: FormSchema) => {
const formItemSlots: Recordable = setFormItemSlots(slots, item.field)
return (
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label || ''}>
{{
default: () => {
const formItemSlots = {
default: () => {
if (slots[item.field]) {
return getSlot(slots, item.field, formModel.value)
} else {
Expand Down Expand Up @@ -257,7 +254,21 @@ export default defineComponent({
</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
}}
</ElFormItem>
Expand Down
22 changes: 0 additions & 22 deletions src/components/Form/src/helper/index.ts
@@ -1,6 +1,4 @@
import { useI18n } from '@/hooks/web/useI18n'
import { type Slots } from 'vue'
import { getSlot } from '@/utils/tsxHelper'
import { PlaceholderModel, FormSchema, ComponentNameEnum, ColProps } from '../types'
import { isFunction } from '@/utils/is'
import { firstUpperCase, humpToDash } from '@/utils'
Expand Down Expand Up @@ -156,23 +154,3 @@ export const initModel = (schema: FormSchema[], formModel: Recordable) => {
})
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
}
27 changes: 6 additions & 21 deletions src/components/Form/src/types/index.ts
Expand Up @@ -53,7 +53,6 @@ type CamelCaseComponentName = keyof typeof ComponentNameEnum extends infer K
export type ComponentName = CamelCaseComponentName

export interface InputComponentProps {
value?: string | number
maxlength?: number | string
minlength?: number | string
showWordLimit?: boolean
Expand Down Expand Up @@ -99,7 +98,6 @@ export interface InputComponentProps {
}

export interface AutocompleteComponentProps {
value?: string
placeholder?: string
clearable?: boolean
disabled?: boolean
Expand Down Expand Up @@ -132,7 +130,6 @@ export interface AutocompleteComponentProps {
}

export interface InputNumberComponentProps {
value?: number
min?: number
max?: number
step?: number
Expand Down Expand Up @@ -167,7 +164,6 @@ export interface SelectOption {
}

export interface SelectComponentProps {
value?: string | number | boolean | Object
multiple?: boolean
disabled?: boolean
valueKey?: string
Expand Down Expand Up @@ -245,7 +241,6 @@ export interface SelectComponentProps {
}

export interface SelectV2ComponentProps {
value?: string | number | boolean | Object
multiple?: boolean
disabled?: boolean
valueKey?: string
Expand Down Expand Up @@ -290,7 +285,6 @@ export interface SelectV2ComponentProps {
}

export interface CascaderComponentProps {
value?: string | number | string[] | number[] | any
options?: Record<string, unknown>[]
props?: CascaderProps
size?: ElementPlusSize
Expand Down Expand Up @@ -325,7 +319,6 @@ export interface CascaderComponentProps {
}

export interface SwitchComponentProps {
value?: boolean | string | number
disabled?: boolean
loading?: boolean
size?: ElementPlusSize
Expand All @@ -347,7 +340,6 @@ export interface SwitchComponentProps {
}

export interface RateComponentProps {
value?: number
max?: number
size?: ElementPlusSize
disabled?: boolean
Expand Down Expand Up @@ -375,7 +367,6 @@ export interface RateComponentProps {
}

export interface ColorPickerComponentProps {
value?: string
disabled?: boolean
size?: ElementPlusSize
showAlpha?: boolean
Expand All @@ -393,7 +384,6 @@ export interface ColorPickerComponentProps {
}

export interface TransferComponentProps {
value?: any[]
data?: any[]
filterable?: boolean
filterPlaceholder?: string
Expand Down Expand Up @@ -444,7 +434,6 @@ export interface RadioOption {
[key: string]: any
}
export interface RadioGroupComponentProps {
value?: string | number | boolean
size?: ElementPlusSize
disabled?: boolean
textColor?: string
Expand Down Expand Up @@ -472,7 +461,6 @@ export interface RadioGroupComponentProps {
}

export interface RadioButtonComponentProps {
value?: string | number | boolean
size?: ElementPlusSize
disabled?: boolean
textColor?: string
Expand Down Expand Up @@ -518,7 +506,6 @@ export interface CheckboxOption {
}

export interface CheckboxGroupComponentProps {
value?: string[] | number[]
size?: ElementPlusSize
disabled?: boolean
min?: number
Expand Down Expand Up @@ -547,7 +534,6 @@ export interface CheckboxGroupComponentProps {
}

export interface DividerComponentProps {
value?: number | Array<number>
min?: number
max?: number
disabled?: boolean
Expand Down Expand Up @@ -579,7 +565,6 @@ export interface DividerComponentProps {
}

export interface DatePickerComponentProps {
value?: string | Date | number | string[]
readonly?: boolean
disabled?: boolean
size?: ElementPlusSize
Expand Down Expand Up @@ -631,7 +616,6 @@ export interface DatePickerComponentProps {
}

export interface DateTimePickerComponentProps {
value?: string | Date | number | string[]
readonly?: boolean
disabled?: boolean
editable?: boolean
Expand Down Expand Up @@ -672,7 +656,6 @@ export interface DateTimePickerComponentProps {
}

export interface TimePickerComponentProps {
value?: string | Date | number | [Date, Date] | [number, number] | [string, string]
readonly?: boolean
disabled?: boolean
editable?: boolean
Expand Down Expand Up @@ -709,7 +692,6 @@ export interface TimePickerComponentProps {
}

export interface TimeSelectComponentProps {
value?: string
disabled?: boolean
editable?: boolean
clearable?: boolean
Expand Down Expand Up @@ -754,8 +736,6 @@ export interface FormSetProps {
value: any
}

export type FormValueType = string | number | string[] | number[] | boolean | undefined | null

export interface FormItemProps {
labelWidth?: string | number
required?: boolean
Expand All @@ -767,6 +747,11 @@ export interface FormItemProps {
for?: string
validateStatus?: '' | 'error' | 'validating' | 'success'
style?: CSSProperties
slots?: {
default?: (...args: any[]) => JSX.Element | null
label?: (...args: any[]) => JSX.Element | null
error?: (...args: any[]) => JSX.Element | null
}
}

export interface FormSchema {
Expand Down Expand Up @@ -825,7 +810,7 @@ export interface FormSchema {
/**
* 初始值
*/
value?: FormValueType
value?: any

/**
* 是否隐藏
Expand Down
9 changes: 3 additions & 6 deletions src/hooks/web/useForm.ts
@@ -1,10 +1,9 @@
import type { Form, FormExpose } from '@/components/Form'
import type { ElForm } from 'element-plus'
import { ref, unref, nextTick } from 'vue'
import type { FormProps } from '@/components/Form/src/types'
import { FormSchema, FormSetPropsType } from '@/types/form'
import { FormSchema, FormSetProps, FormProps } from '@/components/Form'

export const useForm = (props?: FormProps) => {
export const useForm = () => {
// From实例
const formRef = ref<typeof Form & FormExpose>()

Expand Down Expand Up @@ -47,7 +46,7 @@ export const useForm = (props?: FormProps) => {
/**
* @param schemaProps 需要设置的schemaProps
*/
setSchema: async (schemaProps: FormSetPropsType[]) => {
setSchema: async (schemaProps: FormSetProps[]) => {
const form = await getForm()
form?.setSchema(schemaProps)
},
Expand Down Expand Up @@ -78,8 +77,6 @@ export const useForm = (props?: FormProps) => {
}
}

props && methods.setProps(props)

return {
register,
formRef: elFormRef,
Expand Down
38 changes: 20 additions & 18 deletions src/views/Components/Form/DefaultForm.vue
Expand Up @@ -23,8 +23,6 @@ const { t } = useI18n()
const isMobile = computed(() => appStore.getMobile)
const toggle = ref(false)
const restaurants = ref<Recordable[]>([])
const querySearch = (queryString: string, cb: Fn) => {
const results = queryString
Expand Down Expand Up @@ -679,7 +677,6 @@ const schema = reactive<FormSchema[]>([
return (
<ElOption
key={v.value}
disabled={unref(toggle)}
label={v.label}
value={v.value}
/>
Expand Down Expand Up @@ -1379,32 +1376,37 @@ const schema = reactive<FormSchema[]>([
{
field: 'field68',
component: 'Divider',
label: `${t('formDemo.form')} t('formDemo.slot')}`,
label: `${t('formDemo.form')} ${t('formDemo.slot')}`,
},
{
field: 'field69',
component: 'Input',
value: 'test',
label: `default`,
label: `label`,
formItemProps: {
slots: {
label: ({ label }) => {
return (
<div class="custom-label">
<span class="label-text">custom {label}</span>
</div>
)
}
}
}
},
])
const { register, formRef, methods } = useForm({
schema,
labelWidth: 'auto',
labelPosition: isMobile.value ? 'top' : 'right'
})
const changeToggle = () => {
toggle.value = !unref(toggle)
}
const { register, formRef, methods } = useForm()
</script>
<template>
<button @click="changeToggle">测试</button>
<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>
</template>
Expand Down

0 comments on commit 9c724dc

Please sign in to comment.