types: 修改类型

This commit is contained in:
kailong321200875 2023-05-22 17:26:43 +08:00
parent c5b545d2c7
commit 7d0476f47c
5 changed files with 61 additions and 54 deletions

View File

@ -176,7 +176,7 @@ export default defineComponent({
// const notRenderOptions = ['SelectV2', 'Cascader', 'Transfer'] // const notRenderOptions = ['SelectV2', 'Cascader', 'Transfer']
const componentSlots = (item?.componentProps as any)?.slots || {} const componentSlots = (item?.componentProps as any)?.slots || {}
const slotsMap: Recordable = { const slotsMap: Recordable = {
...setItemComponentSlots(unref(formModel), componentSlots) ...setItemComponentSlots(componentSlots)
} }
// selectoptions // selectoptions
if (item.component === ComponentNameEnum.SELECT) { if (item.component === ComponentNameEnum.SELECT) {

View File

@ -118,13 +118,13 @@ export const setComponentProps = (item: FormSchema): Recordable => {
* @param formModel * @param formModel
* @param slotsProps * @param slotsProps
*/ */
export const setItemComponentSlots = (formModel: any, slotsProps: Recordable = {}): Recordable => { export const setItemComponentSlots = (slotsProps: Recordable = {}): Recordable => {
const slotObj: Recordable = {} const slotObj: Recordable = {}
for (const key in slotsProps) { for (const key in slotsProps) {
if (slotsProps[key]) { if (slotsProps[key]) {
if (isFunction(slotsProps[key])) { if (isFunction(slotsProps[key])) {
slotObj[key] = (...args: any[]) => { slotObj[key] = (...args: any[]) => {
return slotsProps[key]?.(formModel, ...args) return slotsProps[key]?.(...args)
} }
} else { } else {
slotObj[key] = () => { slotObj[key] = () => {

View File

@ -55,8 +55,8 @@ export interface InputComponentProps {
showPassword?: boolean showPassword?: boolean
disabled?: boolean disabled?: boolean
size?: ElementPlusSize size?: ElementPlusSize
prefixIcon?: string | JSX.Element | ((formData: any) => string | JSX.Element) prefixIcon?: string | JSX.Element
suffixIcon?: string | JSX.Element | ((formData: any) => string | JSX.Element) suffixIcon?: string | JSX.Element
type?: InputProps['type'] type?: InputProps['type']
rows?: number rows?: number
autosize?: boolean | { Pows?: numer; maxRows?: number } autosize?: boolean | { Pows?: numer; maxRows?: number }
@ -81,10 +81,10 @@ export interface InputComponentProps {
input?: (value: string | number) => void input?: (value: string | number) => void
} }
slots?: { slots?: {
prefix?: JSX.Element | null | ((formData: any) => JSX.Element | null) prefix?: (...args: any[]) => JSX.Element | null
suffix?: JSX.Element | null | ((formData: any) => JSX.Element | null) suffix?: (...args: any[]) => JSX.Element | null
prepend?: JSX.Element | null | ((formData: any) => JSX.Element | null) prepend?: (...args: any[]) => JSX.Element | null
append?: JSX.Element | null | ((formData: any) => JSX.Element | null) append?: (...args: any[]) => JSX.Element | null
} }
style?: CSSProperties style?: CSSProperties
} }
@ -113,11 +113,11 @@ export interface AutocompleteComponentProps {
change?: (value: string | number) => void change?: (value: string | number) => void
} }
slots?: { slots?: {
default?: JSX.Element | null | ((formData: any, ...args: any[]) => JSX.Element | null) default?: (...args: any[]) => JSX.Element | null
prefix?: JSX.Element | null | ((formData: any) => JSX.Element | null) prefix?: (...args: any[]) => JSX.Element | null
suffix?: JSX.Element | null | ((formData: any) => JSX.Element | null) suffix?: (...args: any[]) => JSX.Element | null
prepend?: JSX.Element | null | ((formData: any) => JSX.Element | null) prepend?: (...args: any[]) => JSX.Element | null
append?: JSX.Element | null | ((formData: any) => JSX.Element | null) append?: (...args: any[]) => JSX.Element | null
} }
style?: CSSProperties style?: CSSProperties
} }
@ -188,9 +188,9 @@ export interface SelectComponentProps {
teleported?: boolean teleported?: boolean
persistent?: boolean persistent?: boolean
automaticDropdown?: boolean automaticDropdown?: boolean
clearIcon?: string | JSX.Element | ((formData: any) => string | JSX.Element) clearIcon?: string | JSX.Element | null
fitInputWidth?: boolean fitInputWidth?: boolean
suffixIcon?: string | JSX.Element | ((formData: any) => string | JSX.Element) suffixIcon?: string | JSX.Element | null
tagType?: 'success' | 'info' | 'warning' | 'danger' tagType?: 'success' | 'info' | 'warning' | 'danger'
validateEvent?: boolean validateEvent?: boolean
placement?: placement?:
@ -233,8 +233,8 @@ export interface SelectComponentProps {
default?: (options: SelectOption[]) => JSX.Element[] | null default?: (options: SelectOption[]) => JSX.Element[] | null
optionGroupDefault?: (item: SelectOption) => JSX.Element optionGroupDefault?: (item: SelectOption) => JSX.Element
optionDefault?: (option: SelectOption) => JSX.Element | null optionDefault?: (option: SelectOption) => JSX.Element | null
prefix?: JSX.Element | null | ((formData: any) => JSX.Element | null) prefix?: (...args: any[]) => JSX.Element | null
empty?: JSX.Element | null | ((formData: any) => JSX.Element | null) empty?: (...args: any[]) => JSX.Element | null
} }
options?: SelectOption[] options?: SelectOption[]
style?: CSSProperties style?: CSSProperties
@ -247,7 +247,7 @@ export interface SelectV2ComponentProps {
valueKey?: string valueKey?: string
size?: ElementPlusSize size?: ElementPlusSize
clearable?: boolean clearable?: boolean
clearIcon?: string | JSX.Element | ((formData: any) => string | JSX.Element) clearIcon?: string | JSX.Element | null
collapseTags?: boolean collapseTags?: boolean
multipleLimit?: number multipleLimit?: number
name?: string name?: string
@ -314,8 +314,8 @@ export interface CascaderComponentProps {
removeTag?: (value: CascaderNode['valueByOption']) => void removeTag?: (value: CascaderNode['valueByOption']) => void
} }
slots?: { slots?: {
default?: (formData: any, { node: any, data: any }) => JSX.Element | null default?: (...args: any[]) => JSX.Element | null
empty?: JSX.Element | null | ((formData: any) => JSX.Element | null) empty?: (...args: any[]) => JSX.Element | null
} }
style?: CSSProperties style?: CSSProperties
} }
@ -327,8 +327,8 @@ export interface SwitchComponentProps {
size?: ElementPlusSize size?: ElementPlusSize
width?: number | string width?: number | string
inlinePrompt?: boolean inlinePrompt?: boolean
activeIcon?: string | JSX.Element | null | ((formData: any) => string | JSX.Element | null) activeIcon?: string | JSX.Element | null
inactiveIcon?: string | JSX.Element | null | ((formData: any) => string | JSX.Element | null) inactiveIcon?: string | JSX.Element | null
activeText?: string activeText?: string
inactiveText?: string inactiveText?: string
activeValue?: boolean | string | number activeValue?: boolean | string | number
@ -416,9 +416,9 @@ export interface TransferComponentProps {
rightCheckChange?: (value: any[]) => void rightCheckChange?: (value: any[]) => void
} }
slots?: { slots?: {
default?: (formData: any, data: { option: any }) => JSX.Element | null default?: (...args: any[]) => JSX.Element | null
leftFooter?: (formData: any) => JSX.Element | null leftFooter?: (...args: any[]) => JSX.Element | null
rightFooter?: (formData: any) => JSX.Element | null rightFooter?: (...args: any[]) => JSX.Element | null
} }
style?: CSSProperties style?: CSSProperties
} }

35
src/types/form.d.ts vendored
View File

@ -35,6 +35,29 @@ export type FormItemProps = {
style?: CSSProperties style?: CSSProperties
} }
// 定义联合类型和条件类型
type ComponentPropsForComponent<T extends ComponentName> = T extends 'input'
? InputComponentProps
: T extends 'autocomplete'
? AutocompleteComponentProps
: T extends 'inputNumber'
? InputNumberComponentProps
: T extends 'select'
? SelectComponentProps
: T extends 'selectV2'
? SelectV2ComponentProps
: T extends 'cascader'
? CascaderComponentProps
: T extends 'switch'
? SwitchComponentProps
: T extends 'rate'
? RateComponentProps
: T extends 'colorPicker'
? ColorPickerComponentProps
: T extends 'transfer'
? TransferComponentProps
: any
export interface FormSchema { export interface FormSchema {
/** /**
* *
@ -59,17 +82,7 @@ export interface FormSchema {
/** /**
* element-plus文档 * element-plus文档
*/ */
componentProps?: componentProps?: ComponentPropsForComponent<ComponentName>
| InputComponentProps
| AutocompleteComponentProps
| InputNumberComponentProps
| SelectComponentProps
| SelectV2ComponentProps
| CascaderComponentProps
| SwitchComponentProps
| RateComponentProps
| ColorPickerComponentProps
| TransferComponentProps
/** /**
* formItem组件属性element-plus文档 * formItem组件属性element-plus文档

View File

@ -383,9 +383,7 @@ const schema = reactive<FormSchema[]>([
component: 'Input', component: 'Input',
componentProps: { componentProps: {
suffixIcon: useIcon({ icon: 'ep:calendar' }), suffixIcon: useIcon({ icon: 'ep:calendar' }),
prefixIcon: () => { prefixIcon: useIcon({ icon: 'ep:share' })
return unref(toggle) ? useIcon({ icon: 'ep:calendar' }) : useIcon({ icon: 'ep:share' })
}
} }
}, },
{ {
@ -394,12 +392,10 @@ const schema = reactive<FormSchema[]>([
component: 'Input', component: 'Input',
componentProps: { componentProps: {
slots: { slots: {
suffix: (data: any) => { suffix: () => {
return unref(toggle) && data.field4 return useIcon({ icon: 'ep:share' })
? useIcon({ icon: 'ep:calendar' })
: useIcon({ icon: 'ep:share' })
}, },
prefix: useIcon({ icon: 'ep:calendar' }) prefix: () => useIcon({ icon: 'ep:calendar' })
} }
} }
}, },
@ -409,10 +405,8 @@ const schema = reactive<FormSchema[]>([
component: 'Input', component: 'Input',
componentProps: { componentProps: {
slots: { slots: {
prepend: useIcon({ icon: 'ep:calendar' }), prepend: () => useIcon({ icon: 'ep:calendar' }),
append: (data: any) => { append: () => useIcon({ icon: 'ep:share' })
return data.field5 ? useIcon({ icon: 'ep:calendar' }) : useIcon({ icon: 'ep:share' })
}
} }
} }
}, },
@ -459,7 +453,7 @@ const schema = reactive<FormSchema[]>([
select: handleSelect select: handleSelect
}, },
slots: { slots: {
default: (_, { item }) => { default: ({ item }) => {
return ( return (
<> <>
<div class="value">{item?.value}</div> <div class="value">{item?.value}</div>
@ -549,9 +543,9 @@ const schema = reactive<FormSchema[]>([
return null return null
} }
}, },
prefix: useIcon({ icon: 'ep:calendar' }), prefix: () => useIcon({ icon: 'ep:calendar' }),
empty: (data: any) => { empty: () => {
return data.field5 ? useIcon({ icon: 'ep:calendar' }) : useIcon({ icon: 'ep:share' }) return useIcon({ icon: 'ep:share' })
} }
} }
} }
@ -775,7 +769,7 @@ const schema = reactive<FormSchema[]>([
componentProps: { componentProps: {
options: options3, options: options3,
slots: { slots: {
default: (_, { data, node }) => { default: ({ data, node }) => {
return ( return (
<> <>
<span>{data.label}</span> <span>{data.label}</span>