@carefrees/form-utils-vue-hooks 0.0.13
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.
- package/README.md +7 -0
- package/esm/hooks/attr/attr.FormItem.d.ts +94 -0
- package/esm/hooks/attr/attr.FormItem.js +104 -0
- package/esm/hooks/index.d.ts +13 -0
- package/esm/hooks/index.js +13 -0
- package/esm/hooks/register/register.FormHideItem.d.ts +9 -0
- package/esm/hooks/register/register.FormHideItem.js +26 -0
- package/esm/hooks/register/register.FormItem.d.ts +24 -0
- package/esm/hooks/register/register.FormItem.js +67 -0
- package/esm/hooks/register/register.FormList.d.ts +9 -0
- package/esm/hooks/register/register.FormList.js +42 -0
- package/esm/hooks/register/register.form.d.ts +4 -0
- package/esm/hooks/register/register.form.js +10 -0
- package/esm/hooks/useAttrs.d.ts +25 -0
- package/esm/hooks/useAttrs.js +26 -0
- package/esm/hooks/useEffect.d.ts +2 -0
- package/esm/hooks/useEffect.js +12 -0
- package/esm/hooks/useForm.d.ts +8 -0
- package/esm/hooks/useForm.js +21 -0
- package/esm/hooks/useFormItem.d.ts +8 -0
- package/esm/hooks/useFormItem.js +21 -0
- package/esm/hooks/useFormItemParentName.d.ts +18 -0
- package/esm/hooks/useFormItemParentName.js +47 -0
- package/esm/hooks/useFormList.d.ts +8 -0
- package/esm/hooks/useFormList.js +21 -0
- package/esm/hooks/useHtmlFor.d.ts +2 -0
- package/esm/hooks/useHtmlFor.js +7 -0
- package/esm/hooks/useMultipleForm.d.ts +8 -0
- package/esm/hooks/useMultipleForm.js +21 -0
- package/esm/hooks/useRule.d.ts +4 -0
- package/esm/hooks/useRule.js +12 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +4 -0
- package/esm/instance/formInstance.d.ts +58 -0
- package/esm/instance/formInstance.js +132 -0
- package/esm/instance/formItemBaseInstance.d.ts +23 -0
- package/esm/instance/formItemBaseInstance.js +6 -0
- package/esm/instance/formItemInstance.d.ts +18 -0
- package/esm/instance/formItemInstance.js +11 -0
- package/esm/instance/formListInstance.d.ts +43 -0
- package/esm/instance/formListInstance.js +95 -0
- package/esm/instance/index.d.ts +5 -0
- package/esm/instance/index.js +5 -0
- package/esm/instance/multipleInstance.d.ts +32 -0
- package/esm/instance/multipleInstance.js +77 -0
- package/esm/instance/ruleIntsnace.d.ts +45 -0
- package/esm/instance/ruleIntsnace.js +52 -0
- package/esm/interface/index.d.ts +31 -0
- package/esm/interface/index.js +0 -0
- package/esm/utils/cloneByNamePathList.d.ts +3 -0
- package/esm/utils/cloneByNamePathList.js +11 -0
- package/esm/utils/get.d.ts +26 -0
- package/esm/utils/get.js +13 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/esm/utils/interface.d.ts +3 -0
- package/esm/utils/interface.js +0 -0
- package/esm/utils/set.d.ts +32 -0
- package/esm/utils/set.js +29 -0
- package/esm/utils/utils.d.ts +172 -0
- package/esm/utils/utils.js +131 -0
- package/lib/hooks/attr/attr.FormItem.d.ts +94 -0
- package/lib/hooks/attr/attr.FormItem.js +138 -0
- package/lib/hooks/index.d.ts +13 -0
- package/lib/hooks/index.js +168 -0
- package/lib/hooks/register/register.FormHideItem.d.ts +9 -0
- package/lib/hooks/register/register.FormHideItem.js +60 -0
- package/lib/hooks/register/register.FormItem.d.ts +24 -0
- package/lib/hooks/register/register.FormItem.js +101 -0
- package/lib/hooks/register/register.FormList.d.ts +9 -0
- package/lib/hooks/register/register.FormList.js +76 -0
- package/lib/hooks/register/register.form.d.ts +4 -0
- package/lib/hooks/register/register.form.js +44 -0
- package/lib/hooks/useAttrs.d.ts +25 -0
- package/lib/hooks/useAttrs.js +63 -0
- package/lib/hooks/useEffect.d.ts +2 -0
- package/lib/hooks/useEffect.js +46 -0
- package/lib/hooks/useForm.d.ts +8 -0
- package/lib/hooks/useForm.js +61 -0
- package/lib/hooks/useFormItem.d.ts +8 -0
- package/lib/hooks/useFormItem.js +61 -0
- package/lib/hooks/useFormItemParentName.d.ts +18 -0
- package/lib/hooks/useFormItemParentName.js +84 -0
- package/lib/hooks/useFormList.d.ts +8 -0
- package/lib/hooks/useFormList.js +61 -0
- package/lib/hooks/useHtmlFor.d.ts +2 -0
- package/lib/hooks/useHtmlFor.js +41 -0
- package/lib/hooks/useMultipleForm.d.ts +8 -0
- package/lib/hooks/useMultipleForm.js +61 -0
- package/lib/hooks/useRule.d.ts +4 -0
- package/lib/hooks/useRule.js +46 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +87 -0
- package/lib/instance/formInstance.d.ts +58 -0
- package/lib/instance/formInstance.js +166 -0
- package/lib/instance/formItemBaseInstance.d.ts +23 -0
- package/lib/instance/formItemBaseInstance.js +40 -0
- package/lib/instance/formItemInstance.d.ts +18 -0
- package/lib/instance/formItemInstance.js +45 -0
- package/lib/instance/formListInstance.d.ts +43 -0
- package/lib/instance/formListInstance.js +129 -0
- package/lib/instance/index.d.ts +5 -0
- package/lib/instance/index.js +96 -0
- package/lib/instance/multipleInstance.d.ts +32 -0
- package/lib/instance/multipleInstance.js +111 -0
- package/lib/instance/ruleIntsnace.d.ts +45 -0
- package/lib/instance/ruleIntsnace.js +96 -0
- package/lib/interface/index.d.ts +31 -0
- package/lib/interface/index.js +18 -0
- package/lib/utils/cloneByNamePathList.d.ts +3 -0
- package/lib/utils/cloneByNamePathList.js +45 -0
- package/lib/utils/get.d.ts +26 -0
- package/lib/utils/get.js +47 -0
- package/lib/utils/index.d.ts +4 -0
- package/lib/utils/index.js +87 -0
- package/lib/utils/interface.d.ts +3 -0
- package/lib/utils/interface.js +18 -0
- package/lib/utils/set.d.ts +32 -0
- package/lib/utils/set.js +63 -0
- package/lib/utils/utils.d.ts +172 -0
- package/lib/utils/utils.js +198 -0
- package/package.json +30 -0
- package/src/hooks/attr/attr.FormItem.tsx +185 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/register/register.FormHideItem.ts +28 -0
- package/src/hooks/register/register.FormItem.ts +93 -0
- package/src/hooks/register/register.FormList.ts +49 -0
- package/src/hooks/register/register.form.ts +13 -0
- package/src/hooks/useAttrs.ts +64 -0
- package/src/hooks/useEffect.ts +13 -0
- package/src/hooks/useForm.ts +34 -0
- package/src/hooks/useFormItem.ts +33 -0
- package/src/hooks/useFormItemParentName.ts +49 -0
- package/src/hooks/useFormList.ts +33 -0
- package/src/hooks/useHtmlFor.ts +9 -0
- package/src/hooks/useMultipleForm.ts +30 -0
- package/src/hooks/useRule.ts +16 -0
- package/src/index.ts +4 -0
- package/src/instance/formInstance.ts +220 -0
- package/src/instance/formItemBaseInstance.ts +23 -0
- package/src/instance/formItemInstance.ts +23 -0
- package/src/instance/formListInstance.ts +108 -0
- package/src/instance/index.ts +5 -0
- package/src/instance/multipleInstance.ts +109 -0
- package/src/instance/ruleIntsnace.ts +89 -0
- package/src/interface/index.ts +38 -0
- package/src/utils/cloneByNamePathList.ts +13 -0
- package/src/utils/get.ts +53 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/interface.ts +4 -0
- package/src/utils/set.ts +85 -0
- package/src/utils/utils.ts +504 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FormListInstanceBase } from "../instance/formListInstance.js";
|
|
2
|
+
import { inject, provide, ref, toValue } from "vue";
|
|
3
|
+
const formListProvideSymbol = Symbol('carefrees-form-list');
|
|
4
|
+
function useFormListProvide(formList) {
|
|
5
|
+
const newFormList = useFormList(formList);
|
|
6
|
+
provide(formListProvideSymbol, newFormList);
|
|
7
|
+
}
|
|
8
|
+
function useFormListInject() {
|
|
9
|
+
const formList = inject(formListProvideSymbol, ref(new FormListInstanceBase()));
|
|
10
|
+
return formList;
|
|
11
|
+
}
|
|
12
|
+
function useFormList(formList) {
|
|
13
|
+
const refForm = ref();
|
|
14
|
+
if (!refForm.value) {
|
|
15
|
+
const value = toValue(formList);
|
|
16
|
+
if (value) refForm.value = value;
|
|
17
|
+
else refForm.value = new FormListInstanceBase();
|
|
18
|
+
}
|
|
19
|
+
return refForm;
|
|
20
|
+
}
|
|
21
|
+
export { useFormList, useFormListInject, useFormListProvide };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MultipleInstanceBase } from '../instance/multipleInstance';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
/**多表单收集 Context */
|
|
4
|
+
export declare function useMultipleFormProvide(multipleForm?: Ref<MultipleInstanceBase> | MultipleInstanceBase): void;
|
|
5
|
+
/**子项中获取 多表单收集 实例*/
|
|
6
|
+
export declare function useMultipleFormInject(): Ref<MultipleInstanceBase, MultipleInstanceBase>;
|
|
7
|
+
/**初始化 多表单收集 实例*/
|
|
8
|
+
export declare function useMultipleForm(multipleForm?: Ref<MultipleInstanceBase> | MultipleInstanceBase): Ref<MultipleInstanceBase | undefined, MultipleInstanceBase | undefined>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { MultipleInstanceBase } from "../instance/multipleInstance.js";
|
|
2
|
+
import { inject, provide, ref, toValue } from "vue";
|
|
3
|
+
const multipleFormProvideSymbol = Symbol('carefrees-multiple-form');
|
|
4
|
+
function useMultipleFormProvide(multipleForm) {
|
|
5
|
+
const newMultipleForm = useMultipleForm(multipleForm);
|
|
6
|
+
provide(multipleFormProvideSymbol, newMultipleForm);
|
|
7
|
+
}
|
|
8
|
+
function useMultipleFormInject() {
|
|
9
|
+
const multipleForm = inject(multipleFormProvideSymbol, ref(new MultipleInstanceBase()));
|
|
10
|
+
return multipleForm;
|
|
11
|
+
}
|
|
12
|
+
function useMultipleForm(multipleForm) {
|
|
13
|
+
const refForm = ref();
|
|
14
|
+
if (!refForm.value) {
|
|
15
|
+
const value = toValue(multipleForm);
|
|
16
|
+
if (value) refForm.value = value;
|
|
17
|
+
else refForm.value = new MultipleInstanceBase();
|
|
18
|
+
}
|
|
19
|
+
return refForm;
|
|
20
|
+
}
|
|
21
|
+
export { useMultipleForm, useMultipleFormInject, useMultipleFormProvide };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RuleInstanceBase } from "../instance/ruleIntsnace.js";
|
|
2
|
+
import { ref, toValue } from "vue";
|
|
3
|
+
function useRules(rules) {
|
|
4
|
+
const refRules = ref();
|
|
5
|
+
if (!refRules.value) {
|
|
6
|
+
const value = toValue(rules);
|
|
7
|
+
if (value) refRules.value = value;
|
|
8
|
+
else refRules.value = new RuleInstanceBase();
|
|
9
|
+
}
|
|
10
|
+
return refRules;
|
|
11
|
+
}
|
|
12
|
+
export { useRules };
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { FormListInstanceBase } from './formListInstance';
|
|
2
|
+
import { FormItemInstanceBase } from './formItemInstance';
|
|
3
|
+
import { ValidateErrorEntity } from '../interface';
|
|
4
|
+
import { Ref } from 'vue';
|
|
5
|
+
/**基础实例*/
|
|
6
|
+
export declare class FormInstanceBase<T = any> {
|
|
7
|
+
/**表单数据*/
|
|
8
|
+
formData: Ref<Partial<T>>;
|
|
9
|
+
/**表单每一项实例*/
|
|
10
|
+
formItemInstances: Ref<FormItemInstanceBase>[];
|
|
11
|
+
/**表单中List实例集合*/
|
|
12
|
+
formListInstances: Map<string, Ref<FormListInstanceBase>>;
|
|
13
|
+
/**隐藏组件字段对应的值*/
|
|
14
|
+
hideState: Ref<Record<string, boolean>>;
|
|
15
|
+
/**是否保护值(不进行表单项组件卸载重置初始值)*/
|
|
16
|
+
preserve?: boolean;
|
|
17
|
+
/**值更新触发*/
|
|
18
|
+
onValuesChange?: (changedValues: any, values: T) => void;
|
|
19
|
+
/**提交保存 验证成功*/
|
|
20
|
+
onFinish?: (values: T) => void;
|
|
21
|
+
/**提交保存 验证失败*/
|
|
22
|
+
onFinishFailed?: (errorInfo: ValidateErrorEntity<T>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* 重置字段数据值
|
|
25
|
+
*/
|
|
26
|
+
resetFieldsValue: (initial?: Partial<T>) => this;
|
|
27
|
+
/**注册一个 formIList 实例*/
|
|
28
|
+
registerFormList: (name: string, itemInstance: Ref<FormListInstanceBase>) => () => void;
|
|
29
|
+
/**注册一个 formItem 实例*/
|
|
30
|
+
registerFormItem: (itemInstance: Ref<FormItemInstanceBase>) => () => void;
|
|
31
|
+
/**更新字段是否隐藏*/
|
|
32
|
+
updatedFieldHideValue: (value: Record<string, boolean>) => this;
|
|
33
|
+
/**更新字段value值
|
|
34
|
+
*
|
|
35
|
+
* @param name 字段
|
|
36
|
+
* @param value 字段值
|
|
37
|
+
* @param validateType 校验规则处理
|
|
38
|
+
* @param isOnlySave 仅用于存储
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
updatedFieldValue: (name: string, value: any, validateType?: "validate" | "clear" | "none") => this;
|
|
42
|
+
/**获取 formList 实例或者集合*/
|
|
43
|
+
getFormListInstance: (name: string) => Ref<FormListInstanceBase, FormListInstanceBase> | undefined;
|
|
44
|
+
/**获取字段值*/
|
|
45
|
+
getFieldValue: (name?: string) => any;
|
|
46
|
+
/**获取字段隐藏值*/
|
|
47
|
+
getFieldHideValue: (name?: string) => any;
|
|
48
|
+
/**
|
|
49
|
+
* 只进行验证,没有返回值
|
|
50
|
+
* */
|
|
51
|
+
onlyValidate: (name: string | string[]) => Promise<this>;
|
|
52
|
+
/**规则验证 ,默认不传递验证所有 */
|
|
53
|
+
validate: (names?: string[], isGetAllData?: boolean) => Promise<T>;
|
|
54
|
+
/**
|
|
55
|
+
* 提交
|
|
56
|
+
*/
|
|
57
|
+
submit: () => Promise<void>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { cloneByNamePathList, get, has, set } from "../utils/index.js";
|
|
2
|
+
import { ref, toValue } from "vue";
|
|
3
|
+
class FormInstanceBase {
|
|
4
|
+
formData = ref({});
|
|
5
|
+
formItemInstances = [];
|
|
6
|
+
formListInstances = new Map([]);
|
|
7
|
+
hideState = ref({});
|
|
8
|
+
preserve = true;
|
|
9
|
+
onValuesChange;
|
|
10
|
+
onFinish;
|
|
11
|
+
onFinishFailed;
|
|
12
|
+
resetFieldsValue = (initial = {})=>{
|
|
13
|
+
this.formData.value = Object.assign(this.formData.value, initial);
|
|
14
|
+
return this;
|
|
15
|
+
};
|
|
16
|
+
registerFormList = (name, itemInstance)=>{
|
|
17
|
+
this.formListInstances.set(name, itemInstance);
|
|
18
|
+
return ()=>{
|
|
19
|
+
this.formListInstances.delete(name);
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
registerFormItem = (itemInstance)=>{
|
|
23
|
+
this.formItemInstances.push(itemInstance);
|
|
24
|
+
return ()=>{
|
|
25
|
+
const instanceItem = toValue(itemInstance);
|
|
26
|
+
this.formItemInstances = this.formItemInstances.filter((ite)=>ite !== itemInstance);
|
|
27
|
+
let preserve = this.preserve;
|
|
28
|
+
if (instanceItem?.preserve === false) preserve = instanceItem.preserve;
|
|
29
|
+
const name = `${instanceItem?.name}`;
|
|
30
|
+
const formData = toValue(this.formData);
|
|
31
|
+
if (name && has(formData, name) && !preserve) this.formData.value = set(formData, instanceItem.name, void 0);
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
updatedFieldHideValue = (value)=>{
|
|
35
|
+
const names = Object.keys(value || {});
|
|
36
|
+
names.forEach((key)=>{
|
|
37
|
+
this.hideState.value = set(this.hideState.value, key, value[key]);
|
|
38
|
+
});
|
|
39
|
+
return this;
|
|
40
|
+
};
|
|
41
|
+
updatedFieldValue = (name, value, validateType = 'validate')=>{
|
|
42
|
+
this.formData.value = set(this.formData.value, name, value);
|
|
43
|
+
if ('validate' === validateType) this.onlyValidate(name);
|
|
44
|
+
else if ('clear' === validateType) {
|
|
45
|
+
const formItemInstance = this.formItemInstances.find((ite)=>ite.value.name === name);
|
|
46
|
+
const instanceItem = toValue(formItemInstance);
|
|
47
|
+
const instanceItemRule = toValue(instanceItem?.rule);
|
|
48
|
+
if (instanceItemRule) instanceItemRule?.updatedMessages?.([]);
|
|
49
|
+
}
|
|
50
|
+
return this;
|
|
51
|
+
};
|
|
52
|
+
getFormListInstance = (name)=>this.formListInstances.get(name);
|
|
53
|
+
getFieldValue = (name)=>{
|
|
54
|
+
const fromData = toValue(this.formData);
|
|
55
|
+
if (name) {
|
|
56
|
+
if (has(fromData, name)) return get(fromData, name);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
return fromData;
|
|
60
|
+
};
|
|
61
|
+
getFieldHideValue = (name)=>{
|
|
62
|
+
const hideData = toValue(this.hideState);
|
|
63
|
+
if (name) return get(hideData, name);
|
|
64
|
+
return hideData;
|
|
65
|
+
};
|
|
66
|
+
onlyValidate = async (name)=>{
|
|
67
|
+
try {
|
|
68
|
+
if (Array.isArray(name)) await this.validate(name, false);
|
|
69
|
+
else await this.validate([
|
|
70
|
+
name
|
|
71
|
+
], false);
|
|
72
|
+
} catch (err) {
|
|
73
|
+
console.log(err);
|
|
74
|
+
}
|
|
75
|
+
return this;
|
|
76
|
+
};
|
|
77
|
+
validate = (names, isGetAllData = true)=>new Promise(async (resolve, reject)=>{
|
|
78
|
+
const errorFields = [];
|
|
79
|
+
const notErrorFields = [];
|
|
80
|
+
const newFormItemInstances = this.formItemInstances;
|
|
81
|
+
const nameListPath = newFormItemInstances.map((item)=>item.value.name);
|
|
82
|
+
const lg = newFormItemInstances.length;
|
|
83
|
+
const isNames = Array.isArray(names) && names.length;
|
|
84
|
+
for(let index = 0; index < lg; index++){
|
|
85
|
+
const instanceItemRef = newFormItemInstances[index];
|
|
86
|
+
const instanceItem = toValue(instanceItemRef);
|
|
87
|
+
const rule = toValue(instanceItem.rule);
|
|
88
|
+
if (rule && rule.isValidate()) {
|
|
89
|
+
let isValidate = true;
|
|
90
|
+
if (isNames) {
|
|
91
|
+
const findx = names.find((name)=>name === instanceItem.name);
|
|
92
|
+
if (!findx) isValidate = false;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
if (isValidate) {
|
|
96
|
+
await rule.validate();
|
|
97
|
+
notErrorFields.push({
|
|
98
|
+
errors: [],
|
|
99
|
+
sort: instanceItem.sort,
|
|
100
|
+
name: instanceItem.name
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
} catch (errors) {
|
|
104
|
+
if (errors) errorFields.push({
|
|
105
|
+
errors,
|
|
106
|
+
sort: instanceItem.sort,
|
|
107
|
+
name: instanceItem.name
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} else notErrorFields.push({
|
|
111
|
+
errors: [],
|
|
112
|
+
sort: instanceItem.sort,
|
|
113
|
+
name: instanceItem.name
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const values = isGetAllData ? cloneByNamePathList(this.getFieldValue(), nameListPath) : {};
|
|
117
|
+
if (errorFields.length) reject({
|
|
118
|
+
errorFields,
|
|
119
|
+
values: values
|
|
120
|
+
});
|
|
121
|
+
else resolve(values);
|
|
122
|
+
});
|
|
123
|
+
submit = async ()=>{
|
|
124
|
+
try {
|
|
125
|
+
const result = await this.validate();
|
|
126
|
+
if (result) this?.onFinish?.(result);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
this.onFinishFailed?.(error);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export { FormInstanceBase };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
import { FormInstanceBase } from './formInstance';
|
|
3
|
+
export declare class FormItemBaseInstance {
|
|
4
|
+
/**
|
|
5
|
+
* 顺序
|
|
6
|
+
* @example
|
|
7
|
+
* "0"
|
|
8
|
+
* "0-0"
|
|
9
|
+
* "0-0-0"
|
|
10
|
+
*/
|
|
11
|
+
sort?: string;
|
|
12
|
+
/**
|
|
13
|
+
* 字段 ,分割方式与lodash的get和set方法值更新或设置路径一致
|
|
14
|
+
* @example
|
|
15
|
+
* 默认:"name"
|
|
16
|
+
* 嵌套字段:"name.a.doc"
|
|
17
|
+
* 嵌套字段:"name[1].a.doc"
|
|
18
|
+
* 嵌套字段:"name.a[2].doc"
|
|
19
|
+
*/
|
|
20
|
+
name: string;
|
|
21
|
+
/**表单实例*/
|
|
22
|
+
instance?: Ref<FormInstanceBase>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RuleInstanceBase } from './ruleIntsnace';
|
|
2
|
+
import { FormInstanceBase } from './formInstance';
|
|
3
|
+
import { FormItemBaseInstance } from './formItemBaseInstance';
|
|
4
|
+
import { Ref } from 'vue';
|
|
5
|
+
export declare class FormItemInstanceBase extends FormItemBaseInstance {
|
|
6
|
+
/**父级字段*/
|
|
7
|
+
parentDataField?: string;
|
|
8
|
+
/**通知 只用于校验规则提示 字段 */
|
|
9
|
+
noticeOnlyRuleDataField?: string[];
|
|
10
|
+
htmlFor?: string;
|
|
11
|
+
control?: Ref<any>;
|
|
12
|
+
/**规则*/
|
|
13
|
+
rule?: Ref<RuleInstanceBase>;
|
|
14
|
+
/**是否保护值(不进行表单项组件卸载重置初始值)*/
|
|
15
|
+
preserve?: boolean;
|
|
16
|
+
/**触发数据更新之后触发(用于数据联动之类的)*/
|
|
17
|
+
onAfterUpdate?: (value: any, instance: Ref<FormInstanceBase>, instanceAttr: Ref<FormItemInstanceBase>, event: any) => void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FormItemBaseInstance } from "./formItemBaseInstance.js";
|
|
2
|
+
class FormItemInstanceBase extends FormItemBaseInstance {
|
|
3
|
+
parentDataField;
|
|
4
|
+
noticeOnlyRuleDataField;
|
|
5
|
+
htmlFor;
|
|
6
|
+
control;
|
|
7
|
+
rule;
|
|
8
|
+
preserve = true;
|
|
9
|
+
onAfterUpdate;
|
|
10
|
+
}
|
|
11
|
+
export { FormItemInstanceBase };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { FormItemBaseInstance } from './formItemBaseInstance';
|
|
2
|
+
import { RuleInstanceBase } from './ruleIntsnace';
|
|
3
|
+
import { FormItemInstanceBase } from './formItemInstance';
|
|
4
|
+
import { Ref } from 'vue';
|
|
5
|
+
export declare class FormListInstanceBase extends FormItemBaseInstance {
|
|
6
|
+
/**规则*/
|
|
7
|
+
rule?: Ref<RuleInstanceBase>;
|
|
8
|
+
/**表单实例*/
|
|
9
|
+
formItemInstance?: Ref<FormItemInstanceBase>;
|
|
10
|
+
/**父级字段*/
|
|
11
|
+
parentDataField?: string;
|
|
12
|
+
/**记录key值*/
|
|
13
|
+
keys: number[];
|
|
14
|
+
/**累加数据,唯一性*/
|
|
15
|
+
id: number;
|
|
16
|
+
/**
|
|
17
|
+
* 初始化
|
|
18
|
+
* @param name 字段
|
|
19
|
+
*/
|
|
20
|
+
ctor: (name: string) => this;
|
|
21
|
+
/**获取值*/
|
|
22
|
+
getLastValue: () => any[];
|
|
23
|
+
/**
|
|
24
|
+
* 添加一条
|
|
25
|
+
* @param initialValue 初始值
|
|
26
|
+
* @param unshift 是否加入数组前面
|
|
27
|
+
*/
|
|
28
|
+
onAdd: (initialValue?: Object, unshift?: boolean) => void;
|
|
29
|
+
/**
|
|
30
|
+
* 删除
|
|
31
|
+
* @param index 删除数据下标
|
|
32
|
+
*/
|
|
33
|
+
onDelete: (index: number | number[]) => void;
|
|
34
|
+
/**移动*/
|
|
35
|
+
onMove: (from: number, to: number) => void;
|
|
36
|
+
/**更新某个item数据*/
|
|
37
|
+
updatedItem: (index: number, item: any) => void;
|
|
38
|
+
/**获取渲染 list 字段拼接*/
|
|
39
|
+
getFields: () => {
|
|
40
|
+
name: number;
|
|
41
|
+
key: number;
|
|
42
|
+
}[];
|
|
43
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { FormItemBaseInstance } from "./formItemBaseInstance.js";
|
|
2
|
+
import { toValue } from "vue";
|
|
3
|
+
class FormListInstanceBase extends FormItemBaseInstance {
|
|
4
|
+
rule;
|
|
5
|
+
formItemInstance;
|
|
6
|
+
parentDataField;
|
|
7
|
+
keys = [];
|
|
8
|
+
id = 0;
|
|
9
|
+
ctor = (name)=>{
|
|
10
|
+
this.name = name;
|
|
11
|
+
return this;
|
|
12
|
+
};
|
|
13
|
+
getLastValue = ()=>{
|
|
14
|
+
const form = toValue(this.instance);
|
|
15
|
+
const value = form?.getFieldValue?.(this.name);
|
|
16
|
+
const lastValue = Array.isArray(value) ? value : [];
|
|
17
|
+
return lastValue;
|
|
18
|
+
};
|
|
19
|
+
onAdd = (initialValue = {}, unshift)=>{
|
|
20
|
+
const form = toValue(this.instance);
|
|
21
|
+
const value = this.getLastValue();
|
|
22
|
+
if (unshift) {
|
|
23
|
+
const listData = [
|
|
24
|
+
initialValue || {},
|
|
25
|
+
...value
|
|
26
|
+
];
|
|
27
|
+
this.keys = [
|
|
28
|
+
this.id,
|
|
29
|
+
...this.keys
|
|
30
|
+
];
|
|
31
|
+
this.id++;
|
|
32
|
+
form?.updatedFieldValue?.(this.name, listData);
|
|
33
|
+
} else {
|
|
34
|
+
const listData = [
|
|
35
|
+
...value,
|
|
36
|
+
initialValue || {}
|
|
37
|
+
];
|
|
38
|
+
this.keys = [
|
|
39
|
+
...this.keys,
|
|
40
|
+
this.id
|
|
41
|
+
];
|
|
42
|
+
this.id++;
|
|
43
|
+
form?.updatedFieldValue?.(this.name, listData);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
onDelete = (index)=>{
|
|
47
|
+
const form = toValue(this.instance);
|
|
48
|
+
const value = this.getLastValue();
|
|
49
|
+
const newIndexs = Array.isArray(index) ? index : [
|
|
50
|
+
index
|
|
51
|
+
];
|
|
52
|
+
this.keys = this.keys.filter((_, index)=>!newIndexs.includes(index));
|
|
53
|
+
const listData = value.filter((_, index)=>!newIndexs.includes(index));
|
|
54
|
+
form?.updatedFieldValue?.(this.name, listData);
|
|
55
|
+
};
|
|
56
|
+
onMove = (from, to)=>{
|
|
57
|
+
const form = toValue(this.instance);
|
|
58
|
+
const newList = this.getLastValue();
|
|
59
|
+
const fromItem = newList[from];
|
|
60
|
+
const toItem = newList[to];
|
|
61
|
+
newList[from] = toItem;
|
|
62
|
+
newList[to] = fromItem;
|
|
63
|
+
form?.updatedFieldValue?.(this.name, [
|
|
64
|
+
...newList
|
|
65
|
+
]);
|
|
66
|
+
};
|
|
67
|
+
updatedItem = (index, item)=>{
|
|
68
|
+
const form = toValue(this.instance);
|
|
69
|
+
const newList = this.getLastValue();
|
|
70
|
+
const newItem = newList[index];
|
|
71
|
+
newList[index] = {
|
|
72
|
+
...newItem,
|
|
73
|
+
...item
|
|
74
|
+
};
|
|
75
|
+
form?.updatedFieldValue?.(this.name, [
|
|
76
|
+
...newList
|
|
77
|
+
]);
|
|
78
|
+
};
|
|
79
|
+
getFields = ()=>{
|
|
80
|
+
const values = this.getLastValue();
|
|
81
|
+
return values.map((__, index)=>{
|
|
82
|
+
let key = this.keys[index];
|
|
83
|
+
if (void 0 === key) {
|
|
84
|
+
this.keys[index] = this.id;
|
|
85
|
+
key = this.keys[index];
|
|
86
|
+
this.id++;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
name: index,
|
|
90
|
+
key
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export { FormListInstanceBase };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { FormInstanceBase } from './formInstance';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
export declare class MultipleInstanceBase {
|
|
4
|
+
instanceMap: Map<string, Ref<FormInstanceBase>>;
|
|
5
|
+
/**
|
|
6
|
+
* 注册表单实例
|
|
7
|
+
* @param name 表单名称
|
|
8
|
+
* @param form 表单实例
|
|
9
|
+
*/
|
|
10
|
+
ctor: (name: string, form: Ref<FormInstanceBase>) => () => void;
|
|
11
|
+
/**
|
|
12
|
+
* 获取表单实例
|
|
13
|
+
* @param name 表单名称
|
|
14
|
+
*/
|
|
15
|
+
getInstance: (name?: string) => Ref<FormInstanceBase<any>, FormInstanceBase<any>> | Map<string, Ref<FormInstanceBase<any>, FormInstanceBase<any>>> | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* 验证表单规则
|
|
18
|
+
* @param namePath 表单名称(如果不传递表单名称,则验证所有表单)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
*/
|
|
22
|
+
validate: (namePath?: string | string[] | Record<string, string[]>) => Promise<unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* 获取表单中值
|
|
25
|
+
* @param name 表单名称 (不存在时,获取所有表单值)
|
|
26
|
+
* @param path 字段路径 (不存在的时候直接获取对应表单所有值)
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
getFormFieldValue: (name?: string, dataField?: string | string[]) => {
|
|
30
|
+
[x: string]: any;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { cloneByNamePathList } from "../utils/index.js";
|
|
2
|
+
import { toValue } from "vue";
|
|
3
|
+
class MultipleInstanceBase {
|
|
4
|
+
instanceMap = new Map([]);
|
|
5
|
+
ctor = (name, form)=>{
|
|
6
|
+
this.instanceMap.set(name, form);
|
|
7
|
+
return ()=>{
|
|
8
|
+
this.instanceMap.delete(name);
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
getInstance = (name)=>{
|
|
12
|
+
if (name) return this.instanceMap.get(name);
|
|
13
|
+
return this.instanceMap;
|
|
14
|
+
};
|
|
15
|
+
validate = (namePath)=>new Promise(async (resolve, reject)=>{
|
|
16
|
+
const listFormErrors = {};
|
|
17
|
+
let isSuccess = true;
|
|
18
|
+
let nameKeys = [];
|
|
19
|
+
let isObject = false;
|
|
20
|
+
if (namePath) if (Array.isArray(namePath)) nameKeys = namePath;
|
|
21
|
+
else if ('[object Object]' === Object.prototype.toString.call(namePath)) {
|
|
22
|
+
isObject = true;
|
|
23
|
+
nameKeys = Object.keys(namePath);
|
|
24
|
+
} else nameKeys = [
|
|
25
|
+
namePath
|
|
26
|
+
];
|
|
27
|
+
else nameKeys = Array.from(this.instanceMap.keys());
|
|
28
|
+
const lg = nameKeys.length;
|
|
29
|
+
for(let index = 0; index < lg; index++){
|
|
30
|
+
const name = nameKeys[index];
|
|
31
|
+
const form = this.instanceMap.get(name);
|
|
32
|
+
const formInstacne = toValue(form);
|
|
33
|
+
try {
|
|
34
|
+
if (formInstacne) {
|
|
35
|
+
const paths = isObject ? namePath[name] : void 0;
|
|
36
|
+
const result = await formInstacne.validate(paths);
|
|
37
|
+
listFormErrors[name] = {
|
|
38
|
+
errorFields: [],
|
|
39
|
+
values: result
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
} catch (errs) {
|
|
43
|
+
isSuccess = false;
|
|
44
|
+
listFormErrors[name] = errs;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (isSuccess) resolve(listFormErrors);
|
|
48
|
+
else reject(listFormErrors);
|
|
49
|
+
});
|
|
50
|
+
getFormFieldValue = (name, dataField)=>{
|
|
51
|
+
if (!name) {
|
|
52
|
+
const data = {};
|
|
53
|
+
this.instanceMap.forEach((form, key)=>{
|
|
54
|
+
data[key] = form.value.getFieldValue();
|
|
55
|
+
});
|
|
56
|
+
return data;
|
|
57
|
+
}
|
|
58
|
+
const form = this.instanceMap.get(name);
|
|
59
|
+
const formInstacne = toValue(form);
|
|
60
|
+
if (formInstacne) {
|
|
61
|
+
if ('string' == typeof dataField) return {
|
|
62
|
+
[name]: formInstacne.getFieldValue(dataField)
|
|
63
|
+
};
|
|
64
|
+
if (dataField) {
|
|
65
|
+
const formData = formInstacne.getFieldValue();
|
|
66
|
+
const data = cloneByNamePathList(formData, dataField);
|
|
67
|
+
return {
|
|
68
|
+
[name]: data
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
[name]: {}
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export { MultipleInstanceBase };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RuleItem } from 'async-validator';
|
|
2
|
+
import { FormInstanceBase } from './formInstance';
|
|
3
|
+
import { MessageType } from '../interface';
|
|
4
|
+
import { Ref } from 'vue';
|
|
5
|
+
export declare class RuleInstanceBase {
|
|
6
|
+
/**
|
|
7
|
+
* 顺序
|
|
8
|
+
* @example
|
|
9
|
+
* "0"
|
|
10
|
+
* "0-0"
|
|
11
|
+
* "0-0-0"
|
|
12
|
+
*/
|
|
13
|
+
sort?: string;
|
|
14
|
+
/**表单实例*/
|
|
15
|
+
instance?: Ref<FormInstanceBase<any>>;
|
|
16
|
+
/**
|
|
17
|
+
* 字段 ,分割方式与lodash的get和set方法值更新或设置路径一致
|
|
18
|
+
* @example
|
|
19
|
+
* 默认:"name"
|
|
20
|
+
* 嵌套字段:"name.a.doc"
|
|
21
|
+
* 嵌套字段:"name[1].a.doc"
|
|
22
|
+
* 嵌套字段:"name.a[2].doc"
|
|
23
|
+
*/
|
|
24
|
+
name: string;
|
|
25
|
+
/**规则*/
|
|
26
|
+
rules: Ref<RuleItem[]>;
|
|
27
|
+
/**错误提示内容*/
|
|
28
|
+
messages: Ref<MessageType[] | undefined>;
|
|
29
|
+
/**判断是否必填*/
|
|
30
|
+
isRequired: () => boolean;
|
|
31
|
+
/**判断是否需要验证*/
|
|
32
|
+
isValidate: () => number | false;
|
|
33
|
+
/**更新提示信息*/
|
|
34
|
+
updatedMessages: (messages?: MessageType[]) => void;
|
|
35
|
+
/**更新规则*/
|
|
36
|
+
updatedRules: (rules: RuleItem[]) => void;
|
|
37
|
+
/**验证规则
|
|
38
|
+
*/
|
|
39
|
+
validate: () => Promise<unknown>;
|
|
40
|
+
/**获取校验结果*/
|
|
41
|
+
getValidateResult: () => {
|
|
42
|
+
tip: string | (string | undefined)[];
|
|
43
|
+
isInvalid: boolean;
|
|
44
|
+
};
|
|
45
|
+
}
|