UNPKG

@chl1860/dynamic-form-vue3

Version:

Vue3 + TypeScript + Ant Design Vue 动态表单组件

620 lines (554 loc) 19.4 kB
import { App } from 'vue'; import { Component } from 'vue'; import { ComponentOptionsMixin } from 'vue'; import { ComponentProvideOptions } from 'vue'; import { ComputedRef } from 'vue'; import { DefineComponent } from 'vue'; import { ExtractPropTypes } from 'vue'; import { PropType } from 'vue'; import { PublicProps } from 'vue'; declare type __VLS_NonUndefinedable<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_2<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_3<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_4<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_5<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_6<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_7<T> = T extends undefined ? never : T; declare type __VLS_NonUndefinedable_8<T> = T extends undefined ? never : T; declare type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {}; declare type __VLS_Prettify_2<T> = { [K in keyof T]: T[K]; } & {}; declare type __VLS_Prettify_3<T> = { [K in keyof T]: T[K]; } & {}; declare type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_2<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_2<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_3<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_3<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_4<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_4<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_5<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_5<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_6<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_6<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_7<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_7<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_TypePropsToRuntimeProps_8<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: PropType<__VLS_NonUndefinedable_8<T[K]>>; } : { type: PropType<T[K]>; required: true; }; }; declare type __VLS_WithDefaults<P, D> = { [K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & { default: D[K]; }> : P[K]; }; declare type __VLS_WithDefaults_2<P, D> = { [K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify_2<P[K] & { default: D[K]; }> : P[K]; }; declare type __VLS_WithDefaults_3<P, D> = { [K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify_3<P[K] & { default: D[K]; }> : P[K]; }; export declare interface AsyncResult<T = any> { success: boolean; data?: T; error?: string; } export declare interface AsyncState { loading: boolean; error?: string; } /** * 组件注册接口 */ export declare interface ComponentRegistry { register(type: string, component: Component): void; get(type: string): Component | undefined; has(type: string): boolean; unregister(type: string): void; clear(): void; getAll(): Record<string, Component>; } /** * 创建组件注册表 */ export declare function createComponentRegistry(): ComponentRegistry; export declare const createFieldConfig: (config: any) => any; export declare const createFormSchema: (fields: any[], options?: any) => any; export declare const CustomColorPicker: DefineComponent<ExtractPropTypes<__VLS_WithDefaults_3<__VLS_TypePropsToRuntimeProps_8<Props_8>, { value: string; disabled: boolean; showInput: boolean; showPresetColors: boolean; presetColors: () => string[]; }>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, { "update:value": (value: string) => void; blur: () => void; focus: () => void; }, string, PublicProps, Readonly<ExtractPropTypes<__VLS_WithDefaults_3<__VLS_TypePropsToRuntimeProps_8<Props_8>, { value: string; disabled: boolean; showInput: boolean; showPresetColors: boolean; presetColors: () => string[]; }>>> & Readonly<{ onBlur?: (() => any) | undefined; onFocus?: (() => any) | undefined; "onUpdate:value"?: ((value: string) => any) | undefined; }>, { value: string; disabled: boolean; showInput: boolean; showPresetColors: boolean; presetColors: string[]; }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; export declare const CustomRatingField: DefineComponent<ExtractPropTypes<__VLS_WithDefaults_2<__VLS_TypePropsToRuntimeProps_7<Props_7>, { value: number; disabled: boolean; maxStars: number; showText: boolean; textLabels: () => string[]; }>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, { "update:value": (value: number) => void; blur: () => void; focus: () => void; }, string, PublicProps, Readonly<ExtractPropTypes<__VLS_WithDefaults_2<__VLS_TypePropsToRuntimeProps_7<Props_7>, { value: number; disabled: boolean; maxStars: number; showText: boolean; textLabels: () => string[]; }>>> & Readonly<{ onBlur?: (() => any) | undefined; onFocus?: (() => any) | undefined; "onUpdate:value"?: ((value: number) => any) | undefined; }>, { value: number; disabled: boolean; maxStars: number; showText: boolean; textLabels: string[]; }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; declare const _default: { install: (app: App<any>, options?: DynamicFormOptions) => void; version: string; architecture: string; }; export default _default; /** * 根据路径删除对象中的值 * @param obj 目标对象 * @param path 路径字符串 */ export declare function deleteByPath(obj: any, path: string): void; export declare interface DynamicFormOptions { componentPrefix?: string; debug?: boolean; customComponents?: Record<string, Component>; } declare interface FieldLayoutConfig { span?: number; offset?: number; colSpan?: number; labelCol?: { span?: number; offset?: number; }; wrapperCol?: { span?: number; offset?: number; }; style?: Record<string, any>; className?: string; } export declare interface FieldLinkage { dependsOn: string; optionsMap?: Record<string, SelectOption[]>; asyncOptionsLoader?: (value: any, formData: any) => Promise<SelectOption[]>; visibleWhen?: (value: any, formData: any) => boolean; disabledWhen?: (value: any, formData: any) => boolean; resetOnChange?: boolean; } export declare interface FormGroup { name: string; type: 'group'; label: string; placeholder?: string; required?: boolean; options?: SelectOption[]; props?: Record<string, any>; rules?: ValidationRule[]; linkage?: FieldLinkage; tooltip?: string | { title: string; placement?: 'top' | 'left' | 'right' | 'bottom' | 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight' | 'leftTop' | 'leftBottom' | 'rightTop' | 'rightBottom'; color?: string; overlayClassName?: string; overlayStyle?: Record<string, any>; }; component?: any; componentProps?: Record<string, any>; children: SimpleFieldConfig[]; layout?: FieldLayoutConfig | 'vertical' | 'horizontal'; bordered?: boolean; } declare interface FormLayoutConfig { type?: 'vertical' | 'horizontal' | 'inline' | 'grid' | 'horizontal-fields'; gutter?: [number, number] | number; columns?: number; cols?: number; labelAlign?: 'left' | 'right'; labelCol?: { span?: number; offset?: number; }; wrapperCol?: { span?: number; offset?: number; }; breakpoints?: { xs?: number; sm?: number; md?: number; lg?: number; xl?: number; xxl?: number; }; } /** * 获取所有路径及其值 * @param obj 目标对象 * @param prefix 路径前缀 * @returns 路径值对象 */ export declare function getAllPaths(obj: any, prefix?: string): Record<string, any>; /** * 根据路径获取对象中的值 * @param obj 目标对象 * @param path 路径字符串,如 'basicInfo.projectName' * @returns 对应路径的值 */ export declare function getByPath(obj: any, path: string): any; /** * 全局组件注册器 */ export declare const globalComponentRegistry: { /** * 注册全局自定义字段组件 * @param type 字段类型 * @param component Vue 组件 */ register(type: string, component: Component): void; /** * 获取全局注册的组件 * @param type 字段类型 */ get(type: string): Component | undefined; /** * 检查是否存在指定类型的组件 * @param type 字段类型 */ has(type: string): boolean; /** * 取消注册全局组件 * @param type 字段类型 */ unregister(type: string): void; /** * 清空所有全局组件 */ clear(): void; /** * 获取所有全局注册的组件 */ getAll(): Record<string, Component>; /** * 批量注册组件 * @param components 组件映射表 */ registerBatch(components: Record<string, Component>): void; }; /** * 检查路径是否存在 * @param obj 目标对象 * @param path 路径字符串 * @returns 是否存在该路径 */ export declare function hasPath(obj: any, path: string): boolean; export declare const install: (app: App, options?: DynamicFormOptions) => void; declare interface Props { schema: SimpleFormSchema; modelValue?: any; showActions?: boolean; showReset?: boolean; submitText?: string; resetText?: string; submitting?: boolean; validateOnChange?: boolean; } declare interface Props_2 { field: SimpleFieldConfig | FormGroup; path?: string; } declare interface Props_3 { field: SimpleFieldConfig; value?: string; disabled?: boolean; } declare interface Props_4 { field: SimpleFieldConfig; value?: any; disabled?: boolean; loading?: boolean; } declare interface Props_5 { field: SimpleFieldConfig; value?: any; disabled?: boolean; } declare interface Props_6 { field: FormGroup; path: string; } declare interface Props_7 { value?: number; field: SimpleFieldConfig; disabled?: boolean; maxStars?: number; showText?: boolean; textLabels?: string[]; } declare interface Props_8 { value?: string; field: SimpleFieldConfig; disabled?: boolean; showInput?: boolean; showPresetColors?: boolean; presetColors?: string[]; } export declare interface SelectOption { label: string; value: any; disabled?: boolean; } /** * 根据路径设置对象中的值 * @param obj 目标对象 * @param path 路径字符串,如 'basicInfo.projectName' * @param value 要设置的值 */ export declare function setByPath(obj: any, path: string, value: any): void; export declare interface SimpleFieldConfig { name: string; type: string; label: string; placeholder?: string; required?: boolean; options?: SelectOption[]; props?: Record<string, any>; rules?: ValidationRule[]; linkage?: FieldLinkage; tooltip?: string | { title: string; placement?: 'top' | 'left' | 'right' | 'bottom' | 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight' | 'leftTop' | 'leftBottom' | 'rightTop' | 'rightBottom'; color?: string; overlayClassName?: string; overlayStyle?: Record<string, any>; }; component?: any; componentProps?: Record<string, any>; layout?: FieldLayoutConfig | 'vertical' | 'horizontal'; children?: SimpleFieldConfig[]; bordered?: boolean; } export declare const SimpleForm: DefineComponent<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { showActions: boolean; showReset: boolean; submitText: string; resetText: string; submitting: boolean; validateOnChange: boolean; }>>, { validate: () => boolean; validateAsync: (path: string) => Promise<boolean>; reset: () => void; setFieldValue: (path: string, value: any) => void; getFieldValue: (path: string) => any; formData: ComputedRef<any>; getAsyncState: (key: string) => AsyncState; initializeAsync: () => Promise<void>; registerComponent: (type: string, component: Component) => void; unregisterComponent: (type: string) => void; getComponent: (type: string) => Component | undefined; hasComponent: (type: string) => boolean; getAllComponents: () => Record<string, Component>; clearComponents: () => void; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, { "update:modelValue": (value: any) => void; submit: (data: any) => void; reset: () => void; change: (data: any) => void; validate: (isValid: boolean, errors: Record<string, string>) => void; }, string, PublicProps, Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, { showActions: boolean; showReset: boolean; submitText: string; resetText: string; submitting: boolean; validateOnChange: boolean; }>>> & Readonly<{ onSubmit?: ((data: any) => any) | undefined; onReset?: (() => any) | undefined; onChange?: ((data: any) => any) | undefined; "onUpdate:modelValue"?: ((value: any) => any) | undefined; onValidate?: ((isValid: boolean, errors: Record<string, string>) => any) | undefined; }>, { showActions: boolean; showReset: boolean; submitText: string; resetText: string; submitting: boolean; validateOnChange: boolean; }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; export declare const SimpleFormItem: DefineComponent<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_2<Props_2>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_2<Props_2>>> & Readonly<{}>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; /** * Vue 插件形式的全局注册器 */ export declare const SimpleFormPlugin: { install(app: any, options?: { components?: Record<string, Component>; }): void; }; export declare interface SimpleFormSchema { fields: SimpleFieldConfig[]; asyncInitializer?: () => Promise<any>; layout?: FormLayoutConfig; submitButton?: { text?: string; type?: 'primary' | 'default' | 'dashed' | 'text' | 'link'; size?: 'large' | 'middle' | 'small'; loading?: boolean; disabled?: boolean; htmlType?: 'submit' | 'button' | 'reset'; }; extraButtons?: Array<{ text: string; type?: 'primary' | 'default' | 'dashed' | 'text' | 'link'; size?: 'large' | 'middle' | 'small'; icon?: string; loading?: boolean; disabled?: boolean; onClick?: () => void; }>; } export declare const SimpleGroup: DefineComponent<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_6<Props_6>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_6<Props_6>>> & Readonly<{}>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; export declare const SimpleInput: DefineComponent<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_3<Props_3>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, { "update:value": (value: string) => void; blur: () => void; focus: () => void; }, string, PublicProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_3<Props_3>>> & Readonly<{ onBlur?: (() => any) | undefined; onFocus?: (() => any) | undefined; "onUpdate:value"?: ((value: string) => any) | undefined; }>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; export declare const SimpleRadio: DefineComponent<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_5<Props_5>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, { "update:value": (value: any) => void; }, string, PublicProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_5<Props_5>>> & Readonly<{ "onUpdate:value"?: ((value: any) => any) | undefined; }>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; export declare const SimpleSelect: DefineComponent<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_4<Props_4>>, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, { "update:value": (value: any) => void; blur: () => void; focus: () => void; }, string, PublicProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps_4<Props_4>>> & Readonly<{ onBlur?: (() => any) | undefined; onFocus?: (() => any) | undefined; "onUpdate:value"?: ((value: any) => any) | undefined; }>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>; export declare function useSimpleForm(schema: SimpleFormSchema, initialData?: any): { formData: any; formValues: ComputedRef<any>; errors: ComputedRef<Record<string, string>>; hasErrors: ComputedRef<boolean>; getValue: (path: string) => any; setValue: (path: string, value: any) => void; getFieldConfig: (fieldName: string) => any; validateField: (path: string) => boolean; validateFieldAsync: (path: string) => Promise<boolean>; validateForm: () => boolean; resetForm: () => void; getFieldError: (path: string) => string; initializeAsync: () => Promise<void>; loadFieldOptions: (fieldName: string, dependentValue: any) => Promise<any>; getFieldOptions: (fieldName: string, dependentValue?: any) => any; getAsyncState: (key: string) => AsyncState; asyncStates: ComputedRef<Record<string, AsyncState>>; fieldOptionsCache: ComputedRef<Record<string, any[]>>; componentRegistry: ComponentRegistry; schema: SimpleFormSchema; }; export declare interface ValidationRule { required?: boolean; message?: string; validator?: (value: any, formData: any) => boolean | string; asyncValidator?: (value: any, formData: any) => Promise<boolean | string>; } export declare const VERSION_INFO: { current: string; architecture: string; }; export { }