@leiyin/v-form-renderer 0.1.3-alpha.2 → 0.1.3-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ declare var __VLS_2: `id:${string}`, __VLS_3: {};
2
+ type __VLS_Slots = {} & {
3
+ [K in NonNullable<typeof __VLS_2>]?: (props: typeof __VLS_3) => any;
4
+ };
5
+ declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
6
+ data: {
7
+ readonly type: PropType<import("../index.js").Content>;
8
+ readonly default: () => void;
9
+ };
10
+ value: {
11
+ readonly type: ObjectConstructor;
12
+ readonly default: () => void;
13
+ };
14
+ disabled: {
15
+ readonly type: BooleanConstructor;
16
+ readonly default: false;
17
+ };
18
+ readonly: {
19
+ readonly type: BooleanConstructor;
20
+ readonly default: false;
21
+ };
22
+ id: {
23
+ readonly type: StringConstructor;
24
+ readonly default: "";
25
+ };
26
+ itemValue: {
27
+ type: import("vue").PropType<any>;
28
+ };
29
+ }>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
30
+ updateValue: (...args: any[]) => void;
31
+ "update:itemValue": (value: any) => void;
32
+ }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
33
+ data: {
34
+ readonly type: PropType<import("../index.js").Content>;
35
+ readonly default: () => void;
36
+ };
37
+ value: {
38
+ readonly type: ObjectConstructor;
39
+ readonly default: () => void;
40
+ };
41
+ disabled: {
42
+ readonly type: BooleanConstructor;
43
+ readonly default: false;
44
+ };
45
+ readonly: {
46
+ readonly type: BooleanConstructor;
47
+ readonly default: false;
48
+ };
49
+ id: {
50
+ readonly type: StringConstructor;
51
+ readonly default: "";
52
+ };
53
+ itemValue: {
54
+ type: import("vue").PropType<any>;
55
+ };
56
+ }>> & Readonly<{
57
+ onUpdateValue?: ((...args: any[]) => any) | undefined;
58
+ "onUpdate:itemValue"?: ((value: any) => any) | undefined;
59
+ }>, {
60
+ disabled: boolean;
61
+ readonly: boolean;
62
+ id: string;
63
+ data: import("../index.js").Content;
64
+ value: Record<string, any>;
65
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
66
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
67
+ declare const _default: typeof __VLS_export;
68
+ export default _default;
69
+ type __VLS_WithSlots<T, S> = T & {
70
+ new (): {
71
+ $slots: S;
72
+ };
73
+ };
@@ -0,0 +1,63 @@
1
+ declare const __VLS_export: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
2
+ data: {
3
+ readonly type: PropType<import("..").Content>;
4
+ readonly default: () => void;
5
+ };
6
+ value: {
7
+ readonly type: ObjectConstructor;
8
+ readonly default: () => void;
9
+ };
10
+ disabled: {
11
+ readonly type: BooleanConstructor;
12
+ readonly default: false;
13
+ };
14
+ readonly: {
15
+ readonly type: BooleanConstructor;
16
+ readonly default: false;
17
+ };
18
+ id: {
19
+ readonly type: StringConstructor;
20
+ readonly default: "";
21
+ };
22
+ itemValue: {
23
+ type: import("vue").PropType<any>;
24
+ };
25
+ }>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
26
+ updateValue: (...args: any[]) => void;
27
+ "update:itemValue": (value: any) => void;
28
+ }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
29
+ data: {
30
+ readonly type: PropType<import("..").Content>;
31
+ readonly default: () => void;
32
+ };
33
+ value: {
34
+ readonly type: ObjectConstructor;
35
+ readonly default: () => void;
36
+ };
37
+ disabled: {
38
+ readonly type: BooleanConstructor;
39
+ readonly default: false;
40
+ };
41
+ readonly: {
42
+ readonly type: BooleanConstructor;
43
+ readonly default: false;
44
+ };
45
+ id: {
46
+ readonly type: StringConstructor;
47
+ readonly default: "";
48
+ };
49
+ itemValue: {
50
+ type: import("vue").PropType<any>;
51
+ };
52
+ }>> & Readonly<{
53
+ onUpdateValue?: ((...args: any[]) => any) | undefined;
54
+ "onUpdate:itemValue"?: ((value: any) => any) | undefined;
55
+ }>, {
56
+ disabled: boolean;
57
+ readonly: boolean;
58
+ id: string;
59
+ data: import("..").Content;
60
+ value: Record<string, any>;
61
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
62
+ declare const _default: typeof __VLS_export;
63
+ export default _default;
@@ -0,0 +1,4 @@
1
+ declare const _default: import("vue").DefineComponent<{}, (() => null) | (() => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
2
+ [key: string]: any;
3
+ }>), {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
4
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import type { Content } from '../types';
2
+ export declare const renderFormItemProps: {
3
+ readonly data: {
4
+ readonly type: PropType<Content>;
5
+ readonly default: () => void;
6
+ };
7
+ readonly value: {
8
+ readonly type: ObjectConstructor;
9
+ readonly default: () => void;
10
+ };
11
+ readonly disabled: {
12
+ readonly type: BooleanConstructor;
13
+ readonly default: false;
14
+ };
15
+ readonly readonly: {
16
+ readonly type: BooleanConstructor;
17
+ readonly default: false;
18
+ };
19
+ readonly id: {
20
+ readonly type: StringConstructor;
21
+ readonly default: "";
22
+ };
23
+ };
@@ -0,0 +1,8 @@
1
+ import type { FormState } from './useFormState';
2
+ import type { FormMethods } from './useFormMethods';
3
+ import type { FormEvents } from './useFormEvents';
4
+ export * from './useFormState';
5
+ export * from './useFormMethods';
6
+ export * from './useFormEvents';
7
+ export * from './useFormContext';
8
+ export type FormRendererType = FormState & FormMethods & FormEvents;
@@ -0,0 +1,3 @@
1
+ import type { FormRendererType } from './';
2
+ export declare function createFormContext(instance: FormRendererType): void;
3
+ export declare function useFormContext(formProps?: {}): FormRendererType;
@@ -0,0 +1,11 @@
1
+ import { FormState } from './useFormState';
2
+ type UseFormEventsContext = FormState;
3
+ export type FormEvents = ReturnType<typeof useFormEvents>;
4
+ export declare const useFormEvents: (useFormEventsContext: UseFormEventsContext) => {
5
+ getFormValue: () => Record<string, any>;
6
+ validate: () => Promise<boolean | undefined | void>;
7
+ resetFields: () => Promise<void>;
8
+ updateForm: (newValue: Record<string, any>) => void;
9
+ setOptions: (id: string, data: any[]) => void;
10
+ };
11
+ export {};
@@ -0,0 +1,8 @@
1
+ import { FormState } from './useFormState';
2
+ type UseFormMethodsContext = FormState;
3
+ export type FormMethods = ReturnType<typeof useFormMethods>;
4
+ export declare const useFormMethods: (formMethodsContext: UseFormMethodsContext) => {
5
+ updateFormValue: ({ id, val }: any) => void;
6
+ initFormValues: () => void;
7
+ };
8
+ export {};
@@ -0,0 +1,17 @@
1
+ import type { Ref, ComputedRef } from 'vue';
2
+ import type { FormInstance } from 'element-plus';
3
+ import type { VFormProps } from '../v-form-renderer';
4
+ import type { Content } from '../types';
5
+ export interface FormState {
6
+ elFormRef: Ref<FormInstance | undefined>;
7
+ formModel: Record<string, any>;
8
+ defaultFormValues: Record<string, any>;
9
+ options: Ref<Record<string, any[]>>;
10
+ formPropsRef: Ref<VFormProps>;
11
+ contentRef: ComputedRef<Content[]>;
12
+ }
13
+ type useFormStateParams = {
14
+ props: VFormProps;
15
+ };
16
+ export declare const useFormState: ({ props }: useFormStateParams) => FormState;
17
+ export {};
@@ -1,5 +1,4 @@
1
1
  import VFormRenderer from './v-form-renderer.vue';
2
- export interface TFormProp {
3
- content: any[];
4
- }
2
+ export * from './types/';
3
+ export * from './v-form-renderer';
5
4
  export default VFormRenderer;
@@ -0,0 +1,111 @@
1
+ import { Component } from 'vue';
2
+ import { type FormItemRule } from 'element-plus';
3
+ /** 表单项类型 */
4
+ export type Content = {
5
+ /**
6
+ * 每一个原子都存在 id,用于存储该原子的值,不能重复
7
+ */
8
+ id: string;
9
+ /**
10
+ * 对应el-form-item 的 label值
11
+ *
12
+ */
13
+ label?: string | Function;
14
+ /**
15
+ * 可以是element提供的所有表单组件类型,如传入'input',则渲染出'el-input'
16
+ * 当type="group"时,可以创造复杂对象类型的表单数据,配合items使用。此时getFormValue()返回的是对象类型的数据,对象的每个属性对应items里的每一项
17
+ */
18
+ type?: string;
19
+ /**
20
+ * 用于定义具体原子表单(如el-input)的属性,比如定义el-input的placeholder,
21
+ * tips: 原始属性,尽量不要传响应式数据
22
+ */
23
+ el?: Record<string, any>;
24
+ /**
25
+ * 当 type === 'input' 时展示文本值
26
+ * 当 type === 'select' 时展示对应 label
27
+ * 对于其他组件等同于 disabled = true
28
+ */
29
+ readonly?: boolean;
30
+ /**
31
+ * 输入时自动去掉前后空格
32
+ */
33
+ trim?: boolean;
34
+ /**
35
+ * 表单初始化时的默认值
36
+ */
37
+ default?: any;
38
+ /**
39
+ * 当type="group"时使用
40
+ * items内依然遵循同一层级的id不重复的原则
41
+ */
42
+ items?: Content[];
43
+ /**
44
+ * 传入一个方法,并返回 boolean,返回 true 时则隐藏该表单项
45
+ * formValue 为当前 form 的值,item 为当前表单项的定义
46
+ */
47
+ hidden?: (formValue: Record<string, any>, item: Content) => boolean;
48
+ /**
49
+ * 传入一个方法,并返回 boolean,返回 true 时则禁用该表单项
50
+ * formValue 为当前 form 的值,item 为当前表单项的定义
51
+ */
52
+ disabled?: (formValue: Record<string, any>, item: Content) => boolean;
53
+ /**
54
+ * 具有选择功能的原子表单可用此定义可选项
55
+ */
56
+ options?: {
57
+ label: string;
58
+ value?: any;
59
+ }[];
60
+ /**
61
+ * 使用自定义组件
62
+ * component适用于渲染局部注册组件和自定义组件,而type适用于带el-前缀的全局组件
63
+ */
64
+ component?: Component;
65
+ /**
66
+ * 用于处理输入值,输入的值包括:1. default;2. v-model;3. updateForm。参数为整个表单的值对象或 updateForm 传入的对象
67
+ * 如果 inputFormat 返回 undefined,则不会更新此表单项
68
+ */
69
+ inputFormat?: (obj: any) => any;
70
+ /**
71
+ * 用于处理输出值,参数为对应组件返回值
72
+ * 如果处理后的值是对象类型,会覆盖(Object.assign)到整个表单的值上
73
+ */
74
+ outputFormat?: (val: any, obj: any) => any;
75
+ /** 设置表单校验规则 */
76
+ rules?: FormItemRule[];
77
+ /**
78
+ * 配置remote.url,即可远程配置组件的某个prop!
79
+ * remote接受以下属性:
80
+ * url: 远程接口的地址
81
+ * prop: 要注入的 prop 的名称,默认为 options
82
+ * request: 可选,请求方法
83
+ * dataPath: 可选,data在响应体中的路径
84
+ * onResponse: 可选,处理请求回来的数据
85
+ * onError: 可选,处理请求出错的情况
86
+ * 另外,针对 select、radio-group、checkbox-group,远程数据能自动映射成 el-option 选项!以下属性仅在此情况使用
87
+ * label: 可选,可直接配置远程数据中用作 label 的key
88
+ * value: 可选,可直接配置远程数据中用作 value 的key
89
+ */
90
+ remote?: {
91
+ url?: (params: Record<string, any>) => Promise<any>;
92
+ request?: () => Promise<any>;
93
+ params?: Record<string, any>;
94
+ prop?: string;
95
+ dataPath?: string;
96
+ onResponse?: (resp: any) => any;
97
+ onError?: (error: any) => void;
98
+ label?: string | Function;
99
+ value?: string | Function;
100
+ children?: string;
101
+ };
102
+ /**
103
+ * 监听表单项发出的事件
104
+ * listen to any events emitted by component of form item
105
+ * @param {any[]} args - what that event emits
106
+ * @param {function} updateForm - same as methods.updateForm
107
+ */
108
+ on?: {
109
+ [eventName: string]: (args: any[], updateForm: Function, setOptions: Function) => void;
110
+ };
111
+ };
@@ -0,0 +1 @@
1
+ export type * from './form';
@@ -0,0 +1,37 @@
1
+ import type { Content } from '../types';
2
+ export declare function noop(): void;
3
+ export declare function collect(content: Content[], key: keyof Content): Record<string, any>;
4
+ export declare function correctValue(value: any, content: any[]): void;
5
+ /**
6
+ * 根据 content 中的 outputFormat 来处理 value;
7
+ * 如果 outputFormat 处理后的值是对象类型,会覆盖(Object.assign)到 value 上
8
+ */
9
+ export declare function transformOutputValue(value: Record<string, any>, content: Content[]): Record<string, any>;
10
+ /**
11
+ * 根据 content 中的 inputFormat 来处理 value
12
+ * inputFormat 接受的是当前层级的 value
13
+ * 复杂点在于,不管传入的 value 是否包含某表单项的 key,所有使用了 inputFormat 的项都有可能在这次 update 中被更新
14
+ */
15
+ export declare function transformInputValue(value: Record<string, any>, content: Content[]): Record<string, any>;
16
+ /**
17
+ * 递归合并 oldV & newV,策略如下:
18
+ * 1. 过滤掉 newV 中不存在于 content 中的项
19
+ * 2. 如果该项的 type 不是 GROUP,直接覆盖合并到 oldV
20
+ * 3. 如果是,则递归执行步骤 1 到 3
21
+ */
22
+ export declare function mergeValue(oldV: Record<string, any>, newV: Record<string, any>, content: Content[]): void;
23
+ /**
24
+ * 格式化树状结构数据
25
+ *
26
+ * @param {Array} orignData 数据
27
+ * @param {Object} params 配置项
28
+ * @param {String} params.label 按orignData,label取值设置label
29
+ * @param {String} params.value 按orignData,value取值设置value
30
+ * @param {String} params.children 按orignData,children取值设置children
31
+ * @returns {Array}
32
+ */
33
+ export declare function transformTreeValue(orignData: any[], params?: {
34
+ label: string | Function;
35
+ value: string | Function;
36
+ children: string;
37
+ }): any[];
@@ -0,0 +1,29 @@
1
+ import type { ExtractPublicPropTypes, PropType } from 'vue';
2
+ import VFormRenderer from './v-form-renderer.vue';
3
+ import type { Content } from './types';
4
+ export declare const vFormProps: {
5
+ readonly content: {
6
+ readonly type: PropType<Content[]>;
7
+ readonly required: true;
8
+ readonly default: () => never[];
9
+ };
10
+ /** 统一label宽度 */
11
+ readonly labelWidth: {
12
+ readonly type: StringConstructor;
13
+ readonly default: "98px";
14
+ };
15
+ readonly disabled: {
16
+ readonly type: BooleanConstructor;
17
+ readonly default: false;
18
+ };
19
+ readonly inline: {
20
+ readonly type: BooleanConstructor;
21
+ readonly default: true;
22
+ };
23
+ readonly readonly: {
24
+ readonly type: BooleanConstructor;
25
+ readonly default: false;
26
+ };
27
+ };
28
+ export type VFormProps = ExtractPublicPropTypes<typeof vFormProps>;
29
+ export type VFormRendererInstance = InstanceType<typeof VFormRenderer>;
@@ -1,3 +1,83 @@
1
- declare const __VLS_export: any;
1
+ declare var __VLS_13: `id:${string}`, __VLS_14: {}, __VLS_23: {};
2
+ type __VLS_Slots = {} & {
3
+ [K in NonNullable<typeof __VLS_13>]?: (props: typeof __VLS_14) => any;
4
+ } & {
5
+ default?: (props: typeof __VLS_23) => any;
6
+ };
7
+ declare const __VLS_base: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
8
+ content: {
9
+ readonly type: import("vue").PropType<import("./types").Content[]>;
10
+ readonly required: true;
11
+ readonly default: () => never[];
12
+ };
13
+ labelWidth: {
14
+ readonly type: StringConstructor;
15
+ readonly default: "98px";
16
+ };
17
+ disabled: {
18
+ readonly type: BooleanConstructor;
19
+ readonly default: false;
20
+ };
21
+ inline: {
22
+ readonly type: BooleanConstructor;
23
+ readonly default: true;
24
+ };
25
+ readonly: {
26
+ readonly type: BooleanConstructor;
27
+ readonly default: false;
28
+ };
29
+ model: {
30
+ type: import("vue").PropType<any>;
31
+ };
32
+ }>, {
33
+ getFormValue: () => Record<string, any>;
34
+ validate: () => Promise<boolean | undefined | void>;
35
+ resetFields: () => Promise<void>;
36
+ updateForm: (newValue: Record<string, any>) => void;
37
+ setOptions: (id: string, data: any[]) => void;
38
+ }, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
39
+ enter: (...args: any[]) => void;
40
+ "update:model": (value: any) => void;
41
+ }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
42
+ content: {
43
+ readonly type: import("vue").PropType<import("./types").Content[]>;
44
+ readonly required: true;
45
+ readonly default: () => never[];
46
+ };
47
+ labelWidth: {
48
+ readonly type: StringConstructor;
49
+ readonly default: "98px";
50
+ };
51
+ disabled: {
52
+ readonly type: BooleanConstructor;
53
+ readonly default: false;
54
+ };
55
+ inline: {
56
+ readonly type: BooleanConstructor;
57
+ readonly default: true;
58
+ };
59
+ readonly: {
60
+ readonly type: BooleanConstructor;
61
+ readonly default: false;
62
+ };
63
+ model: {
64
+ type: import("vue").PropType<any>;
65
+ };
66
+ }>> & Readonly<{
67
+ "onUpdate:model"?: ((value: any) => any) | undefined;
68
+ onEnter?: ((...args: any[]) => any) | undefined;
69
+ }>, {
70
+ content: import("./types").Content[];
71
+ labelWidth: string;
72
+ disabled: boolean;
73
+ inline: boolean;
74
+ readonly: boolean;
75
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
76
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
2
77
  declare const _default: typeof __VLS_export;
3
78
  export default _default;
79
+ type __VLS_WithSlots<T, S> = T & {
80
+ new (): {
81
+ $slots: S;
82
+ };
83
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leiyin/v-form-renderer",
3
- "version": "0.1.3-alpha.2",
3
+ "version": "0.1.3-alpha.4",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,14 +21,24 @@
21
21
  "clean": "rimraf dist node_modules/.cache",
22
22
  "build": "pnpm clean && vue-tsc --declaration --emitDeclarationOnly && rollup -c && rollup -c rollup.dts.config.mjs"
23
23
  },
24
+ "peerDependencies": {
25
+ "element-plus": "2.11.7",
26
+ "vue": "^3.2.0"
27
+ },
28
+ "dependencies": {
29
+ "lodash-es": "^4.17.21"
30
+ },
24
31
  "devDependencies": {
32
+ "@types/lodash-es": "4.17.12",
33
+ "element-plus": "2.11.7",
25
34
  "rimraf": "^6.1.2",
26
35
  "rollup-plugin-dts": "^6.3.0",
27
36
  "rollup-plugin-postcss": "^4.0.2",
28
37
  "rollup-plugin-typescript2": "^0.36.0",
29
38
  "rollup-plugin-vue": "^6.0.0",
30
39
  "typescript": "^5.9.3",
40
+ "vue": "^3.2.0",
31
41
  "vue-tsc": "^3.2.1"
32
42
  },
33
- "gitHead": "bcb0a10c4515521c7fd6250b0089a4f8813f84af"
43
+ "gitHead": "5bf19378238c1c642a9e001790f159ecbc7d1628"
34
44
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}